Results 1 to 2 of 2
- 02-17-2010, 06:08 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
problems with executing unix commands
Hello ppl,
I'm trying to execute unix commands from java, but with weird results. Basically my unix command is a pipe and so I wrote a shell script and i'm trying to invoke it from java. This is the piece of code i use to invoke it
cmd = new String[]{ "sh","./shell_script" }
Runtime r = Runtime.getRuntime();
Process p = r.exec(cmd);
PrintWriter out = new PrintWriter(p.getOutputStream());
InputStream error = p.getErrorStream();
Now, my shell script contains a pipeline of commands and one of the command is that of an installed program, say mql. For instance., cat file_name | perl script1 | mql arg1 arg2
My shell is able to recognize the mql command and when I run "sh./shell_script" from shell it runs fine. But when I invoke cmd from java, it gives an error, mql command not found. I tried to set the path in the shell configuration file and even added the whole path of the command in the shell script.
I have a strong feeling that this is a problem with java. Could anyone throw some light on this?
Thanks in advance,Last edited by shanky; 02-17-2010 at 06:11 PM.
- 02-18-2010, 05:36 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The problem is not the mql command, the problem is the pipes. When you exec a command in java (or in c for that matter), you are executing it without a shell. Symbols such as "|", "&", ">", etc. that you are used to when running commands in a shell will not work.
To do what you are trying to do, you need to create a simple script that *does* use a shell, and exec that script. For example, create a script called 'example.csh" containing:
...and let 'cmd' be "example.csh".Java Code:#!/bin/csh -f cat file_name | perl script1 | mql arg1 arg2
Similar Threads
-
Problems executing jar file outside of netbeans
By 2potatocakes in forum New To JavaReplies: 4Last Post: 04-29-2009, 09:53 PM -
Execute the commands in Jsp Program
By swetha_2008 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-17-2008, 06:04 AM -
problems in running d command prompt commands
By postaholic066 in forum Advanced JavaReplies: 1Last Post: 09-10-2008, 03:47 PM -
Problem while executing unix command having \&\& symbol
By suryahota in forum New To JavaReplies: 0Last Post: 06-20-2008, 09:02 AM -
Windows Runtime Commands
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks