Anonymous Anonymous - 1 year ago 193
Bash Question

Command works in terminal, but not with Runtime.exec

I'm trying to run some commands from a Java application using

. However, certain commands that work from a command line tool like Terminal fail when executed like this.


private static final String COMMAND = "cp -n /home/me/Downloads/a.png /home/me/Downloads/b.png";
private static final String COMMAND_2 = "cp -n /home/me/Downloads/a.png /home/me/Downloads/b.png && cp -n /home/me/Downloads/a.png /home/me/Downloads/b.png";

public static void main(String[] args) throws Exception {
int result = Runtime.getRuntime().exec(COMMAND).waitFor();
System.out.println(result); // prints 0
int result2 = Runtime.getRuntime().exec(COMMAND_2).waitFor();
System.out.println(result2); // prints 1

Note that
does the same as
twice, separated by
. Why does one succeed, but the other fail? Both work just fine in Terminal.

I'm using Oracle-Java 1.7.0 on Red Hat Enterprise Linux 6.

Answer Source

This is the most common mistake of all times when it comes to a Process.

A process is not a shell interpreter. As such, any special shell "keywords" will not be interpreted.

If you try and exec cmd1 && cmd2, what happens is that the arguments of the process are literally cmd1, &&, cmd2. Don't do that.

What is more, don't use Runtime.exec(). Use a ProcessBuilder instead. Sample code:

final Process p = new ProcessBuilder("cmd1", "arg1", "arg2").start();
final int retval = p.waitFor();

See the javadoc for ProcessBuilder, it has a lot of niceties.

Oh, and if you use Java 7, don't even bother using external commands. Java 7 has Files.copy().

And also, man execve.