Linux: Running Transmission through Java Runtime.exec() gives error

147 Views Asked by At

I have a bit of a weird situation. I'm writing a Java program to communicate automatically with Transmission on my Raspberry Pi (Linux).

I have the following code (simplified and edited for convenience/security):

String s;
String fullCommand = "transmission-remote -n username:password -a /location/to/file.torrent";
String[] cmd = fullCommand.split(" ");
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null) {
   System.out.println("line: " + s);
}
p.waitFor();
System.out.println("exit: " + p.exitValue());
p.destroy();

The output I get is "non-existing or invalid torrent file". If I run the exact same script on the command line, I get success (so the file exists and is valid).

I've read that Runtime.exec() has problems with spaces. Hence the split(" "). It does not work without it either.

I know Runtime.exec() isn't the same as command line, but is there any way I could get this working?

I have a workaround, but I'd like to do it in one step. If anyone is interested: the workaround is writing the command to a .sh file, make it executable and run that using Runtime.exec(). This does work.

0

There are 0 best solutions below