1

is this the right approach?

 public void doSomething()
 {
    Process p;

    String[] cmd = {"/usr/bin/ssh", "someRemoteMachine", "/absPathToMyProg/myProg"};
    String[] envp = {"PATH=path_needed_toRun_myProg"};

    try
    {
        p = Runtime.getRuntime().exec(cmd,envp);

    }
    catch (IOException e)
    {
        System.err.println("Epic Fail");
    }
 }
1
  • It looks like the right general idea, but I'm not convinced that the PATH will propagate through the SSH connection; why not invoke myProg based on its fully qualified path, if you know what this is? Commented Jun 6, 2011 at 17:19

2 Answers 2

3

Have you tried JSch. Its released under BSD type license. Its pure java and easy to use.

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

2 Comments

Yes, this is a much better solution than messing about with Runtime.getRuntime().exec(..)
By the way, it is even easier to use with documentation.
1

Apart from using JSch (or any other Java SSH implementation), like Suraj said, passing the Path via environment variables is likely not to work, since most SSH deamons only accept a small set of variables from the other side (mostly related to localization or terminal type).

As the argument to ssh (or the "command", if using JSch with an ChannelExec) is passed to the remote shell for execution, you could try to define the path in this command (if your default shell is something compatible to the POSIX sh):

PATH=path_needed_toRun_myProg /absPathToMyProg/myProg

Your array for Runtime.exex thus would look like this:

String[] cmd = {"/usr/bin/ssh", "someRemoteMachine",
                "PATH=path_needed_toRun_myProg /absPathToMyProg/myProg"};

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.