Java Runtime.exec() [英] Java Runtime.exec()

查看:93
本文介绍了Java Runtime.exec()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从命令行运行此命令而没有任何问题(验证脚本执行):

I can run this command from the command line without any problem (the validation script executes):

c:/Python27/python ../feedvalidator/feedvalidator/src/demo.py https://das.dynalias.org:8080/das_core/das/2.16.840.1.113883.4.349/1012581676V377802/otherAdminData/careCoordinators 

如果我不使用URL参数,那么从java开始:

and from java if I leave off the URL parameter and just do:

String[] args1 = {"c:/Python27/python", "../feedvalidator/feedvalidator/src/demo.py" };
Runtime r = Runtime.getRuntime();
Process p = r.exec(args1);

一切正常。如果我对参数使用某些URL,例如:

it works fine. If I use certain URLs for a parameter such as:

String[] args1 = {"c:/Python27/python", "../feedvalidator/feedvalidator/src/demo.py" , "http://www.intertwingly.net/blog/index.atom"};
// or 
String[] args1 = {"c:/Python27/python", "../feedvalidator/feedvalidator/src/demo.py" , "http://www.cnn.com"};

它也可以正常工作。

但是如果我使用此特定URL https://das.dynalias。 org:8080 / das_core / das / 2.16.840.1.113883.4.349 / 1012581676V377802 / otherAdminData / careCoordinators ,然后脚本挂起(java等待进程完成)。我不确定为什么它可以从该URL的命令行运行,但不能从java程序运行。我尝试添加引号以包围URL参数,但这也不起作用。我认为我认为需要转义的网址中没有任何字符。

But if I use this particular URL https://das.dynalias.org:8080/das_core/das/2.16.840.1.113883.4.349/1012581676V377802/otherAdminData/careCoordinators, then the script just hangs (java waits for the process to finish). I’m not sure why it works from the command line for that URL but not from a java program. I tried adding quotes to surround the URL parameter but that didn’t work either. I don’t see any character in the URL that I think need to be escaped.

完整代码:

String urlToValidate = "https://das.dynalias.org:8080/das_core/das/2.16.840.1.113883.4.349/1012581676V377802/otherAdminData/careCoordinators";

String[] args1 = {"c:/Python27/python", "C:/Documents and Settings/vhaiswcaldej/DAS_Workspace/feedvalidator/feedvalidator/src/demo.py", urlToValidate };
System.out.println(args1[0] + " " + args1[1] + " " + args1[2]);

Runtime r = Runtime.getRuntime();
Process p = r.exec(args1);
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
int returnCode = p.waitFor();
 System.out.println("Python Script or OS Return Code: " + Integer.toString(returnCode));
if (returnCode >= 2) {
    .out.println("OS Error: Unable to Find File or other OS error.");
    }

String line = "";
while (br.ready()) {
     String str = br.readLine();
     System.out.println(str);
     if (str.startsWith("line")) {
     //TODO: Report this error back to test tool.
     //System.out.println("Error!");
     }
     }


推荐答案

你需要消耗进程的输出和错误流,否则它将在执行的程序产生输出时阻塞。

You need to drain the output and error streams of the process, or else it will block when the executed program produces output.

来自过程文档


因为某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,所以无法及时写入输入流或读取子进程的输出流可能导致子进程阻塞,甚至死锁。

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

这篇关于Java Runtime.exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆