首页>Program>source

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

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中获取:

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

工作正常.如果我将某些网址用于参数,例如:

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"};

它也可以正常工作。

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

完整代码:

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!");
     }
     }
最新回答
  • 2021-1-11
    1 #

    您需要清除进程的输出和错误流,否则当执行的程序产生输出时,它将阻塞。

    从"流程"文档中:

    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.

    p

    阅读(并关闭) p.getInputStream()p.getErrorStream()

    例如:

    // com.google.common.io.CharStreams
    CharStreams.toString(new InputStreamReader(p.getInputStream()));
    CharStreams.toString(new InputStreamReader(p.getErrorStream()));
    

  • 2021-1-11
    2 #

    通常被exec例程捕获的人挂在Java中.我也曾经被那件事困扰过.问题是您尝试执行的进程可能(取决于很多事情)可能首先写入stdOut或stdErr.如果您以错误的顺序处理它们,则exec将挂起.为了始终正确处理此问题,您必须创建2个线程来读取stdErr和stdOut simulteneously .像这样:

    Process proc = Runtime.getRuntime().exec( cmd );
    // handle process' stdout stream
    Thread out = new StreamHandlerThread( stdOut, proc.getInputStream() );
    out.start();
    // handle process' stderr stream
    Thread err = new StreamHandlerThread( stdErr, proc.getErrorStream() );
    err.start();
    exitVal = proc.waitFor(); // InterruptedException
    ...
    out.join();
    err.join();
    

  • java:如何在Swing中为JTable提供分页支持?
  • c#:在RedirectToAction中传递对象