这是我在其他java应用程序中运行java文件的代码,但我不知道如果程序只需要缓冲输入该怎么办?
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 for run a java file in other java application but i dont knoow what to do if the program takes only bufferedinputs ??
要附加到另一个进程的输入和输出流,请查看API for the Process class您可以在其中找到并使用 getErrorStream()
、getInputStream()
和 getOutputStream()
方法。然后,您可以将输入、输出和错误流包装在各自的缓冲流中。
但请注意,您应该警惕常见的陷阱,这些陷阱在稍微过时的文章 When Runtime Exec Won't 中有很好的解释。
话虽如此,使用 Java 类本身比在另一个 JVM 中运行它要好得多。有什么理由让你不能这样做吗? “缓冲”输入是什么意思?
关于java - 在java中为运行时exec提供缓冲输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20768747/