• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java ProcessBuilder ignoring flags

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention this was using the Sun JVM on Redhat 5
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic