首頁>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!");
     }
     }
最新回復
  • 5月前
    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()));
    

  • 5月前
    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中傳遞物件