java 运行 processbuilder 并进入和输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10657097/
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
Run processbuilder and get in and output
提问by Anthony Byron
I'm trying to get my program to launch Enchanter to SSH into my server, but can't seem to figure out how to get in and output to go to stdin
and stdout
, or anywhere for that matter. I just get a blank output window in Netbeans. How to I get the Jar to run, and get input/output?
我试图让我的程序启动 Enchanter 以通过 SSH 进入我的服务器,但似乎无法弄清楚如何进入和输出到stdin
和stdout
,或任何地方。我只是在 Netbeans 中得到一个空白的输出窗口。如何让 Jar 运行,并获得输入/输出?
public class openShell {
public void openShell() throws IOException {
String line;
Scanner scan = new Scanner(System.in);
ProcessBuilder builder = new ProcessBuilder ("C:\Program Files\Java\lib\enchanter-beanshell-0.6.jar", "myscript.bsh");
builder.redirectErrorStream(true);
Process process = builder.start();
OutputStream stdin = process.getOutputStream ();
InputStream stderr = process.getErrorStream ();
InputStream stdout = process.getInputStream ();
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
while (scan.hasNext()) {
String input = scan.nextLine();
if (input.trim().equals("exit")) {
// Putting 'exit' amongst the echo --EOF--s below doesn't work.
writer.write("exit\n");
} else {
writer.write("((" + input + ") && echo --EOF--) || echo --EOF--\n");
}
writer.flush();
line = reader.readLine();
while (line != null && ! line.trim().equals("--EOF--")) {
System.out.println ("Stdout: " + line);
line = reader.readLine();
}
if (line == null) {
break;
}
}
}
}
private void LaunchButtonActionPerformed(ActionEvent evt) {
//openShell open = new openShell(); //RUNS BUT NO IN OR OUTPUT
//BELOW CODE IS FOR TESTING, JUST TRYING TO GET PROCESSBUILDER TO CONNECT
// TO MY JAR
try {
ProcessBuilder builder = new ProcessBuilder(
"Java -jar C:\Program Files\Java\lib\enchanter-beanshell-0.6.jar");
builder.redirectErrorStream(true);
Process process = builder.start();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
回答by Smagin
The method ProcessBuilder.inheritIO
will redirect your command streams in your stdin, stdout and stderr. This applies to Java 7.
该方法ProcessBuilder.inheritIO
将重定向标准输入、标准输出和标准错误中的命令流。这适用于 Java 7。
回答by Andrew Thompson
String[] args = {
"java",
"-jar",
"C:\Program Files\Java\lib\enchanter-beanshell-0.6.jar"
};
ProcessBuilder builder = new ProcessBuilder(args);
Start with breaking up the arguments as above. Then implement all the recommendations of When Runtime.exec() won't.
从分解上述参数开始。然后执行When Runtime.exec() will not 的所有建议。
回答by Taymon
The methods Process.getInputStream
and Process.getOutputStream
will get you streams that you can then read from and write to.
方法Process.getInputStream
和Process.getOutputStream
将为您提供流,然后您可以从中读取和写入。