aspose file tools*
The moose likes Linux / UNIX and the fly likes Java ProcessBuilder ignoring flags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Java ProcessBuilder ignoring flags" Watch "Java ProcessBuilder ignoring flags" New topic
Author

Java ProcessBuilder ignoring flags

Jess LeMar
Greenhorn

Joined: Apr 03, 2009
Posts: 6
Howdy,

I have a puzzling issue on my hands. I am attempting to run a tar command by using a ProcessBuilder basically as follows.

String cmd = "tar cvf my.tar --exclude='*t' --exclude='*s' dird";
String[] arr = cmd.split(" ");

ProcessBuilder builder = new ProcessBuilder(arr);
System.out.println("Running "+cmd);
Process p = builder.start();

InputStream stream = p.getInputStream();

byte[] b = new byte[1024];

while(stream.read(b) != -1){
System.out.println(new String(b));
}

p.waitFor();

System.out.println("done");


while(stream.read(b) != -1){
System.out.println(new String(b));
}

It appears that the exclude flags are getting ignored. But If I run ls -al as the command it runs as expected. Also running the following find command appears to ignore the -regex flag. find . -regex ".*txt".

Any ideas?

Thank you
J
Jess LeMar
Greenhorn

Joined: Apr 03, 2009
Posts: 6
I forgot to mention this was using the Sun JVM on Redhat 5
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Hi. That's because the * characters are normally interpreted by the shell (bash / sh / other), whereas your Java-created process just calls tar, and neither Java process builder nor tar understand *. If you change the command to invoke bash, which in turn invokes tar, this should work as you expect. Sth like:
Jess LeMar
Greenhorn

Joined: Apr 03, 2009
Posts: 6
Hi Raf,

Thanks for your response. I had figured something wasn't getting resolved correctly and I never do much shell scripting so I was unsure whether the application or the shell resolved the *.

However, I tried your suggestion and it is not working for example here is a simple ls command I attempted to run

String cmd = "/bin/sh -c \"ls -Ral *\"";

I tried make the array passed to the ProcessBuilder two ways
String[] arr = new String[3];
arr[0] = "/bin/sh";
arr[1] = "-c"
arr[2] = "\"ls -Ral *\"";

and simply
String[] arr = cmd.split(" ");

I am still unsure what else I am missing
Jess LeMar
Greenhorn

Joined: Apr 03, 2009
Posts: 6
Ahh okay I figured it out

String[] arr = {"/bin/bash","-c", "ls -Ral *"}; Without the extra quotes does the trick

Thank you for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java ProcessBuilder ignoring flags