[英]how to use java runtime.exec() and ssh to another machine to start a process
这是正确的方法吗?
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");
}
}
你试过JSch吗? 它在 BSD 类型许可下发布。 其纯 java 且易于使用。
除了使用 JSch (或任何其他 Java SSH 实现),就像 Suraj 所说,通过环境变量传递路径可能不起作用,因为大多数 SSH 只接受来自另一侧的小本地化或变量集终端类型)。
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
因此,您的 Runtime.exex 数组将如下所示:
String[] cmd = {"/usr/bin/ssh", "someRemoteMachine",
"PATH=path_needed_toRun_myProg /absPathToMyProg/myProg"};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.