Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Unix Diff From Java Program Error

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()));
like image 846
Tim S Avatar asked Feb 26 '26 06:02

Tim S


1 Answers

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);
like image 83
Tim S Avatar answered Mar 01 '26 00:03

Tim S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!