简体   繁体   中英

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

I have strange problem with run .exe program in my WebApplication. It works fine in console mode.

My exe application code:

            ...
            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).

My Java console / WebApplication code:

    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):

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

or when I delete getEnvirement code (also bad):

before getEnvirement
before new Application

Exe application won't close itself on tomcat (I must use task manager to kill it)

And my questions: Why it dosn't work correct on tomcat? Why exe program have problems with getting system envirements when I run it on tomcat? 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()). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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