2012-03-05 62 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

相关问题