簡體   English   中英

Runtime.exec()在tomcat / Web應用程序上無法正常工作

[英]Runtime.exec() doesn't work correct on tomcat / web application

我在WebApplication中運行.exe程序時遇到奇怪的問題。 它在控制台模式下工作正常。

我的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代碼:

    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());
    }

在“控制台模式”中輸出(正確):

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

在“ Web應用程序模式”下輸出(錯誤):

before getEnvirement
  Path = C:\Windows\system32; <...>
  TEMP = C:\Windows\TEMP

或刪除getEnvirement代碼(也很糟糕)時:

before getEnvirement
before new Application

exe應用程序不會在tomcat上自行關閉(我必須使用任務管理器將其殺死)

我的問題是:為什么它在tomcat上無法正常工作? 為什么在Tomcat上運行exe程序時會遇到獲取系統環境的問題? 最后:為什么它可以在控制台模式下工作? :)

我想知道您生成的進程是否阻止嘗試寫入其輸出。 從文檔的過程

創建的子進程沒有自己的終端或控制台。 它的所有標准io(即stdin,stdout,stderr)操作都將通過三個流(getOutputStream(),getInputStream(),getErrorStream())重定向到父進程。 父流程使用這些流將輸入饋入子流程並從子流程獲取輸出。 由於某些本機平台僅為標准輸入和輸出流提供了有限的緩沖區大小,因此未能及時寫入子流程的輸入流或讀取子流程的輸出流可能導致子流程阻塞甚至死鎖

您應該在進程運行時使用進程的標准輸出/錯誤,並且最好在單獨的線程中使用以避免阻塞。 有關更多信息,請參見此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM