2012-03-05 102 views
0

我的WebApplication中運行.exe程序有奇怪的問題。它在控制檯模式下工作正常。Runtime.exec()在tomcat/web應用程序上無法正常工作

我的EXE應用程序代碼:

  ... 
      Console.WriteLine("before getEnvirement"); 
      IDictionary environmentVariables = Environment.GetEnvironmentVariables(); 
      foreach (DictionaryEntry de in environmentVariables) 
      { 
       Console.WriteLine(" {0} = {1}", de.Key, de.Value); 
      } 
      Console.WriteLine("before new Application"); 
      this.application = new App(); 
      Console.WriteLine("after new Application"); 
      ... 

其中,app()是COM庫類(我說當然是參考)。

我的Java控制檯/ WebApplication的代碼:

before getEnvirement 
    <all my envirements> 
before new Application 
after new Application 

在 「Web應用模式」(壞)輸出:

before getEnvirement 
    Path = C:\Windows\system32; <...> 
    TEMP = C:\Windows\TEMP 
在 「控制檯模式」(正確)

try { 
     String[] callAndArgs = {"C:\\temp\\program.exe", "arg1", "arg2"}; 
     Process p = Runtime.getRuntime().exec(callAndArgs); 
     p.waitFor(); 
    } catch (IOException e) { 
     System.out.println("IOException Error: " + e.getMessage()); 
    } catch (Exception e) { 
     System.out.println("Exception: " + e.getMessage()); 
    } 

輸出

或者當我刪除getEnvirement代碼(也是壞的):

before getEnvirement 
before new Application 

EXE應用程序將不會關閉本身在Tomcat(我必須使用任務管理器殺吧)

而且我的問題:爲什麼不到風度工作在Tomcat是否正確?爲什麼exe程序在使用tomcat運行時遇到系統環境問題?最後:爲什麼它在控制檯模式下工作? :)

回答

1

我不知道你的產卵過程是否阻止試圖寫出它的輸出。從文檔Process

創建的子流程沒有自己的終端或控制檯。所有 其標準io(即標準輸入,標準輸出,標準錯誤)操作將通過三個流 (getOutputStream(),getInputStream(),getErrorStream())重定向到父進程 。父進程 使用這些流向 子進程輸入並獲取輸出。由於某些本機平臺僅提供有限的緩衝 大小爲標準輸入和輸出流,未能及時寫 輸入流或讀出的子過程的輸出流可能會導致 子進程阻塞,甚至死鎖

您應該在流程運行時消耗流程的標準輸出/錯誤,理想情況下在單獨的線程中使用,以避免阻塞。有關更多信息,請參閱this answer

相關問題