problem with Runtime.getRuntime().exec command

Hi,

I am running a command in my envrionment using:

Runtime.getRuntime().exec(commandArray, null, tmpDir)

The command itself is something like this:

prog.sh 0 Y DOWNLOAD PRINTER_TYPE=APPLE

When I run my java program this works fine.

Next I try:

prog.sh 0 Y DOWNLOAD PRINTER_TYPE="APPLE"

this time my java program fails, eventhough when I run the same command from the operating system, it works.

I am using java 5 on Solaris environment.

I have also tried the new ProcessBuilder command with no success.

Seems like the exec command has a problem when the parameters have double quotes in them.

I have looked at the Runtime source code, but it runs a native function which I don't have access, so I can't tell what it is doing.

Any ideas how I can get around this.

Thanks,

[865 byte] By [lfoggya] at [2007-11-27 2:25:38]
# 1
What was the actual argument to exec() method when it didn't work?
hiwaa at 2007-7-12 2:34:11 > top of Java-index,Java Essentials,Java Programming...
# 2
I have used both exec(String[], null, tempDir)andexec(String, null, tempDir)In case of String array, the array enties are:String[0] => prog.shString[1] => 0...String[4] => PRINTER_TYPE="APPLE"
lfoggya at 2007-7-12 2:34:11 > top of Java-index,Java Essentials,Java Programming...
# 3

You need

final String[] command = {"sh","-c", "prog.sh 0 Y DOWNLOAD PRINTER_TYPE=APPLEl" };

but you also need to implement the stdout and stderr handling recommendations in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html .

Also, make sure that prog.sh is in a directory that is on your PATH or specify the complete path.

Message was edited by:

sabre150

sabre150a at 2007-7-12 2:34:11 > top of Java-index,Java Essentials,Java Programming...
# 4

I guess you are missing my point a little here.

Running this command works:

prog.sh 0 Y DOWNLOAD PRINTER_TYPE=APPLE

Running the same command with double-quotes in it fails

prog.sh 0 Y DOWNLOAD PRINTER_TYPE="APPLE"

for some reason exec command can not handle double-quotes properly.

lfoggya at 2007-7-12 2:34:11 > top of Java-index,Java Essentials,Java Programming...
# 5

> I guess you are missing my point a little here.

Possibly!

>

> Running this command works:

> prog.sh 0 Y DOWNLOAD PRINTER_TYPE=APPLE

>

> Running the same command with double-quotes in it

> fails

> prog.sh 0 Y DOWNLOAD PRINTER_TYPE="APPLE"

>

> for some reason exec command can not handle

> double-quotes properly.

Using Linux, it works for me on String[] command = {"sh","-c","openssl genrsa -out \"rsapriv.pem\" 2048"};

sabre150a at 2007-7-12 2:34:11 > top of Java-index,Java Essentials,Java Programming...
# 6
Yes. This does work. Thanks you.
lfoggya at 2007-7-12 2:34:11 > top of Java-index,Java Essentials,Java Programming...