Can anyone help me figure out why when shelling out from a java program, I'm getting this odd error:
diff: extra operand `file1'
diff: Try `diff --help' for more information.
My java code looks like this:
Process p = Runtime.getRuntime().exec("diff --brief -I 'REGEXPATTERN' " + file1 + " " + file2);
p.waitFor();
Where file1 and file2 are valid files. When I execute the command in a regular shell, it works fine. When I execute it from a java program, I get that error. The command works fine when I remove the -I 'REGEXPATTERN' so I'm thinking it may have to do with the -I flag.
To get the error mentioned above, I'm checking the error stream like so:
BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
UPDATE: I solved it by using the overloaded version of exec and passing it a String[] of commands.
String[] command = new String[]{"diff","--brief","-I","'REGEXPATTERN'",file1,file2};
Process p = Runtime.getRuntime().exec(command);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With