Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java ProcessBuilder ignoring flags

 
Jess LeMar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention this was using the Sun JVM on Redhat 5
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic