Trying to execute a Java jar with Runtime.getRuntime().exec()

若如初见. 提交于 2019-11-29 12:24:11
Ravi Thapliyal

Use the Process instance returned by exec()

Process cat = Runtime.getRuntime().exec("java -jar C:/cat.jar C:/test.txt");
BufferedInputStream catOutput= new BufferedInputStream(cat.getInputStream());
int read = 0;
byte[] output = new byte[1024];
while ((read = catOutput.read(output)) != -1) {
    System.out.println(output[read]);
}


References:
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

By default, the created subprocess does not have its own terminal or console. All its standard I/O (stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream().

http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getInputStream()

getInputStream() returns the input stream connected to the normal output of the subprocess.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!