简体   繁体   English

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

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

I have strange problem with run .exe program in my WebApplication. 我在WebApplication中运行.exe程序时遇到奇怪的问题。 It works fine in console mode. 它在控制台模式下工作正常。

My exe application code: 我的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");
            ...

Where App() is class from COM library (I added reference of course). 其中App()是COM库中的类(我当然添加了引用)。

My Java console / WebApplication code: 我的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());
    }

Output in "console mode" (correct): 在“控制台模式”中输出(正确):

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

Output in "web application mode" (bad): 在“ Web应用程序模式”下输出(错误):

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

or when I delete getEnvirement code (also bad): 或删除getEnvirement代码(也很糟糕)时:

before getEnvirement
before new Application

Exe application won't close itself on tomcat (I must use task manager to kill it) exe应用程序不会在tomcat上自行关闭(我必须使用任务管理器将其杀死)

And my questions: Why it dosn't work correct on tomcat? 我的问题是:为什么它在tomcat上无法正常工作? Why exe program have problems with getting system envirements when I run it on tomcat? 为什么在Tomcat上运行exe程序时会遇到获取系统环境的问题? And finally: why it works in console mode? 最后:为什么它可以在控制台模式下工作? :) :)

I wonder if your spawned process is blocking trying to write its output. 我想知道您生成的进程是否阻止尝试写入其输出。 From the doc for Process : 从文档的过程

The created subprocess does not have its own terminal or console. 创建的子进程没有自己的终端或控制台。 All its standard io (ie stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). 它的所有标准io(即stdin,stdout,stderr)操作都将通过三个流(getOutputStream(),getInputStream(),getErrorStream())重定向到父进程。 The parent process uses these streams to feed input to and get output from the subprocess. 父流程使用这些流将输入馈入子流程并从子流程获取输出。 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 由于某些本机平台仅为标准输入和输出流提供了有限的缓冲区大小,因此未能及时写入子流程的输入流或读取子流程的输出流可能导致子流程阻塞甚至死锁

You should be consuming the process' standard out/err as the process runs, and ideally in separate threads in order to avoid blocking. 您应该在进程运行时使用进程的标准输出/错误,并且最好在单独的线程中使用以避免阻塞。 See this answer for more info. 有关更多信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM