简体   繁体   English

关于Linux问题的带有ssh的Java Runtime.exec()

[英]Java Runtime.exec() with ssh on Linux Issues

I am trying to start a new process using Runtime.exec(), but my problem lies within using ssh to remote in and then run a java program there. 我正在尝试使用Runtime.exec()启动一个新进程,但是我的问题出在使用ssh远程进入然后在其中运行Java程序。 Code: 码:

test = "ssh -t username@host java packageName.ClassName portNumber (Other command line args for this class)"
Process proc = Runtime.getRuntime().exec(new String[] {"/bin/bash", "-c", test});

this doesn't fail or catch, but I need to be able to see the stdout for the newly running process and I don't. 这不会失败或失败,但是我需要能够看到新运行的进程的标准输出,而我没有。

Note: if I run ssh -t username@host java packageName.ClassName portNumber (Other command line args for this class) from the command line it works fine. 注意:如果我从命令行运行ssh -t username@host java packageName.ClassName portNumber (Other command line args for this class) ,则可以正常运行。 I have the host setup to not require a password by using ssh keys. 我已将主机设置为不需要通过使用ssh键输入密码。

Any ideas? 有任何想法吗?

You need to use Process.getInputStream to obtain the output from the sub-process being created. 您需要使用Process.getInputStream从正在创建的子流程中获取输出。

See this article for a good discussion on Runtime.exec . 有关Runtime.exec讨论,请参见本文

I think you can ask for an input stream that corresponds to the stdout of the process and then print it on your standard output. 我认为您可以要求输入与该流程的标准输出相对应的输入流,然后将其打印在标准输出上。 If you need to see it after it executes, just call waitFor() method on the process so it finishes before you start printing. 如果您需要在执行后看到它,只需在进程上调用waitFor()方法,使其在开始打印之前完成。

Use getInputStream() to access returned process's stdout. 使用getInputStream()访问返回的进程的标准输出。 You can also use facilities provided by ProcessBuilder.Redirect . 您还可以使用ProcessBuilder.Redirect提供的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM