2

I have a program that needs to execute commands on several servers. I did some local tests with Runtime.getRuntime().exec(); and it worked just fine.

But once I want to execute remote commands it doesn't work. For example if I do

ssh [email protected] 'touch /tmp/hello.txt'

it works in my terminal. But it doesn't work in my Java program with:

Runtime.getRuntime().exec("ssh [email protected] 'touch /tmp/hello.txt'");

Do you have a solution to execute remote commands in a simple main.java for test purposes (no need for logs and production security and all) ?

Thank you!

Edit: The goal is to execute a JAR with parameters like this (logs don't really matter but would be nicer):

ssh [email protected] 'java -jar -Dbroker-ip=10.20.30.41 -Dtopic=topic -Dpartition-number=32 -Dthread=8 -Dsource=/tmp/hour_moves /opt/Produce.jar  > /tmp/logThread1 &'
4
  • Have you tried wrapping the command into a bash script and call that? Commented Apr 10, 2017 at 13:42
  • Yes and it works. But I would like to do it without writing a file on local server if possible. Commented Apr 10, 2017 at 13:53
  • I meant running something like Runtime.getRuntime().exec("myscript.sh'");? exec() is wierd on Java. I always go back to this article: javaworld.com/article/2071275/core-java/… Commented Apr 10, 2017 at 14:19
  • Well, to run this command you need to write the "myscript.sh" localy. I agree that exec() seems weird... Commented Apr 10, 2017 at 14:35

1 Answer 1

1

Your command string must be split into a command array before it can be executed. At the terminal this will be done by a shell. Single quote characters have special meaning for the shell. The command array will look like this:

cmdarray[0] = "ssh";
cmdarray[1] = "[email protected]";
cmdarray[2] = "touch /tmp/hello.txt";

Your version of exec() uses another string tokenizer, splitting the string at whitespace characters. Now the command array looks like this:

cmdarray[0] = "ssh";
cmdarray[1] = "[email protected]";
cmdarray[2] = "'touch";
cmdarray[3] = "/tmp/hello.txt'";

So the executable 'touch can't be found on the remote server.

BTW: there is no need to use single quotes. This should work too (in both cases):

ssh [email protected] touch /tmp/hello.txt

Other special shell characters like > or & must be escaped at the terminal to be passed to the remote site (not interpreted by the shell but passed as argument of the ssh command). If you use exec() you don't have to escape this characters.

Be aware of the return type of exec(): it is a Process object. It is better to use

Runtime.getRuntime().exec(...).waitFor();

to be sure the command has been finished.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.