简体   繁体   English

在Java中将缓冲的输入提供给运行时exec

[英]give buffered input to runtime exec in java

This is my code for run a java file in other java application but i dont knoow what to do if the program takes only bufferedinputs ?? 这是我在其他Java应用程序中运行Java文件的代码,但我不知道如果程序仅使用bufferedinputs怎么办?

try {

Runtime rt = Runtime.getRuntime();

// compile the java file
Process pr = rt.exec("javac Main.java");
pr.waitFor();

// run the java file
pr = rt.exec("java Main " + inputs.toString()); // using this i can give command line arguments 
pr.waitFor();
}

This is my code i can give command line arguments at run time but what if i want to give bufferedinput to the program ? 这是我的代码,我可以在运行时给命令行参数,但是如果我想给程序提供bufferedinput呢?

Thanks in advance 提前致谢

You state: 您声明:

This is my code for run a java file in other java application but i dont knoow what to do if the program takes only bufferedinputs ?? 这是我在其他Java应用程序中运行Java文件的代码,但我不知道如果程序仅使用bufferedinputs怎么办?

To attach to another processes input and output streams, look at the API for the Process class where you'll find and use the getErrorStream() , getInputStream() and getOutputStream() methods. 要附加到另一个流程的输入和输出流,请查看Process类API ,您将在其中找到并使用getErrorStream()getInputStream()getOutputStream()方法。 You can then wrap your Input and Output and Error Streams in their respective Buffered Streams. 然后,您可以将输入流,输出流和错误流包装在它们各自的缓冲流中。

Note however that you should be wary of common pitfalls which are well explained in the slightly dated article, When Runtime Exec Won't 但是请注意,您应该警惕常见的陷阱,该文章在过时的文章《 Runtime Exec不会》中作了很好的解释。

Having said this, you're far better off using the Java classes themsevels rather than running it in another JVM. 话虽如此,您最好使用它们的Java类而不是在另一个JVM中运行它们。 Is there a reason that you can't do this? 您有理由不能这样做吗? And what do you mean by "buffered" input? 您所说的“缓冲”输入是什么意思?

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

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