java 从java类运行的jar文件获取输入和输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/548505/
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
Getting input and output from a jar file run from java class?
提问by Hyman L.
I have a jar file that runs this code:
我有一个运行此代码的 jar 文件:
public class InputOutput {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
boolean cont = true;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (cont) {
System.out.print("Input something: ");
String temp = in.readLine();
if (temp.equals("end")) {
cont = false;
System.out.println("Terminated.");
}
else
System.out.println(temp);
}
}
}
I want to program another java class that executes this jar file and can get the input and send output to it. Is it possible? The current code I have is this but it is not working:
我想编写另一个 java 类来执行这个 jar 文件,并且可以获取输入并将输出发送给它。是否可以?我目前的代码是这样的,但它不起作用:
public class JarTest {
/**
* Test input and output of jar files
* @author Hyman
*/
public static void main(String[] args) {
try {
Process io = Runtime.getRuntime().exec("java -jar InputOutput.jar");
BufferedReader in = new BufferedReader(new InputStreamReader(io.getInputStream()));
OutputStreamWriter out = new OutputStreamWriter(io.getOutputStream());
boolean cont = true;
BufferedReader consolein = new BufferedReader(new InputStreamReader(System.in));
while (cont) {
String temp = consolein.readLine();
out.write(temp);
System.out.println(in.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks for your help
谢谢你的帮助
采纳答案by OscarRyz
Using Runtime.exec is platform dependent.
使用 Runtime.exec 取决于平台。
If you're using windows try prepending:
如果您使用的是 Windows,请尝试添加:
cmd /c
to
到
java -jar .... etc. et
Something like:
就像是:
...getRuntime().exec("cmd /c java -jar InputOutput....
See this also: Make system call and return stdout output.
另请参阅:进行系统调用并返回标准输出。
回答by Itay Maman
Do you need to run the jar file in a different process? If not, you can write a Java program that invokes InputOutput.main(). Alternatively, if the name of the jar/class is only known at run-time, you can create a new class-loader, load the said class and invoke main() via reflection.
您是否需要在不同的进程中运行 jar 文件?如果没有,您可以编写一个调用 InputOutput.main() 的 Java 程序。或者,如果 jar/类的名称仅在运行时已知,您可以创建一个新的类加载器,加载所述类并通过反射调用 main()。
As for the redirection of input/output streams you can use System.setOut, setIn, setErr.
至于输入/输出流的重定向,您可以使用 System.setOut、setIn、setErr。
回答by PhiLho
回答by Itay Maman
Now that you said that you need it to run in another process, here's the problem with your original code: You have a deadlock.
既然您说需要它在另一个进程中运行,那么您的原始代码存在问题:您遇到了死锁。
the process that you launch starts running and quickly fills its output and error streams. JarTest needs to regulary read their contents. Since you're doing it on a single thread sooner or later this thread will find itself waiting for data to be available on one the streams. At this point there's no one to collect data from the other stream.
您启动的进程开始运行并快速填充其输出和错误流。JarTest 需要定期阅读它们的内容。由于您迟早会在单个线程上执行此操作,因此该线程会发现自己正在等待数据在其中一个流上可用。此时没有人从另一个流中收集数据。
Hence, you should launch dedicated threads to collect data from the standard streams of the other process.
因此,您应该启动专用线程来从其他进程的标准流中收集数据。

