如何使用 Java exec

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12430018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:51:58  来源:igfitidea点击:

How to use Java exec

javaexec

提问by user1672190

I am fairly new to java and now I want to use java to run SSH over windows command.

我对java相当陌生,现在我想使用java通过windows命令运行SSH。

Here is the code i created,

这是我创建的代码,

Process pr1 = Runtime.getRuntime().exec("cmd /k" + "ssh root@host" + "&&" + "passwd" );
Process pr = Runtime.getRuntime().exec("ls");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;

while((line=input.readLine()) != null)
    System.out.println(line);

I was always given the error :

我总是得到错误:

java.io.IOException: Cannot run program "ls": CreateProcess error=2, The system cannot find the file specified

java.io.IOException:无法运行程序“ls”:CreateProcess error=2,系统找不到指定的文件

Can anybody help me on this?

有人可以帮我吗?

采纳答案by Kumar Vivek Mitra

Apart from using JSch(or any other Java SSH implementation), 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).

除了使用JSch(或任何其他 Java SSH 实现)之外,通过环境变量传递 Path 可能不起作用,因为大多数 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):

由于 ssh 的参数(或“命令”,如果将 JSch 与 ChannelExec 一起使用)传递给远程 shell 以执行,您可以尝试在此命令中定义路径(如果您的默认 shell 与 POSIX sh 兼容):

PATH=path_needed_toRun_myProg /absPathToMyProg/myProg

PATH=path_needed_toRun_myProg /absPathToMyProg/myProg

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

因此,您的 Runtime.exec 数组将如下所示:

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

If its not hard and strict rule to use Runtime.exec, then try Apache's Exec library...

如果使用 Runtime.exec 不是硬性和严格的规则,那么试试Apache 的 Exec 库......

See this link:

请参阅此链接:

http://commons.apache.org/exec/

http://commons.apache.org/exec/

回答by Hyman

Actually answer maybe quite easy: the problem is that you are executing SSH command and then execute a separate command lswhich is sent to Windows console (and not through SSH) so, as you know Windows doesn't have a ls command.

实际上答案可能很简单:问题在于您正在执行 SSH 命令,然后执行一个单独的命令ls,该命令发送到 Windows 控制台(而不是通过 SSH),因此,正如您所知,Windows 没有 ls 命令。

You have to send it to the Processreturned by the exec of the SSH command, you can do it by storing the resulting process, retrieve its OutputStreamand write commads there. Of course you will have to use its InputStreamto fetch the result. The second exec()shouldn't exist at all.

您必须将其发送到ProcessSSH 命令的 exec 返回的对象,您可以通过存储结果进程、检索其OutputStream并在那里写入命令来实现。当然,您必须使用它InputStream来获取结果。第二个exec()根本不应该存在。

回答by Jason Sperske

Don't bother with Runtime.exec, use Apache Commons Exec. To apply it to your question it would look like this:

不要打扰 Runtime.exec,使用Apache Commons Exec。要将其应用于您的问题,它看起来像这样:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
CommandLine pr1 = CommandLine.parse("cmd /k" + "ssh root@host" + "&&" + "passwd");
CommandLine pr = CommandLine.parse("ls");
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(streamHandler);

int exitValue = executor.execute(pr1);
exitValue = executor.execute(pr);

回答by John Watts

You want to written to the process's stdin.

您想写入进程的标准输入。

pr.getOutputStream().write("ls\n".getBytes());

回答by Martin Vysny

Please use https://github.com/zeroturnaround/zt-exec. Apache Commons Exec has many shortcomings and you need fairly lot of code to get it right. Everything is explained here: https://zeroturnaround.com/rebellabs/why-we-created-yaplj-yet-another-process-library-for-java/

请使用https://github.com/zeroturnaround/zt-exec。Apache Commons Exec 有很多缺点,您需要大量代码才能正确使用。一切都在这里解释:https: //zeroturnaround.com/rebellabs/why-we-created-yaplj-yet-another-process-library-for-java/