Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

Command works in terminal, but not with Runtime.exec

I'm trying to run some commands from a Java application using Runtime.getRuntime().exec(command). However, certain commands that work from a command line tool like Terminal fail when executed like this.

Example:

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 COMMAND_2 does the same as COMMAND 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.

Upvotes: 3

Views: 3066

Answers (2)

fge
fge

Reputation: 121840

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.

Upvotes: 4

lance-java
lance-java

Reputation: 28099

The command and each of it's arguments must be separate items in a String array. Eg:

private static final String[] COMMAND = { "cp", "-n", "/home/me/Downloads/a.png", "/home/me/Downloads/b.png" };

....

int result = Runtime.getRuntime().exec(COMMAND).waitFor();

Upvotes: -1

Related Questions