wood burning stoves 2.0*
The moose likes Java in General and the fly likes Launching multiple instances of Windows app fails using Runtime.getRuntime().exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Launching multiple instances of Windows app fails using Runtime.getRuntime().exec()" Watch "Launching multiple instances of Windows app fails using Runtime.getRuntime().exec()" New topic
Author

Launching multiple instances of Windows app fails using Runtime.getRuntime().exec()

Rune Stilling
Greenhorn

Joined: Nov 10, 2011
Posts: 5
Hi there

I have a problem concerning launching multiple instances of a Altova MapForce (an exe-file) giving each instance it's own thread. The thread class looks like this:



This works fine as long as I only launch one instance. Whenever I launch multiple instances the result becomes unpredictable. What most often happens is that all threads terminates almost right away, and it looks like that the mapforce.exe simply chrashes because the logfiles are empty (which they are usually not).

Running MapForce.exe from a Windows command prompt returns immediately, which for me indicates that the program launches itself in a som sort of thread and handles back control to the cmd-prompt. This allows me to also start multiple instances from the command prompt and this works without problems.

What is the difference between the two scenarios: Executing multiple MapForce.exe from Java vs. launching them from the cmd-prompt? There must be some difference, since the former simply doesn't work.

Thanks,
Rune




Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Try putting a println statement after the p.waitFor call on line 20. If mapforce.exe returns immediately when you run it from a command prompt, I suspect that waitFor will also return immediately, which means your thread will also terminate. Not sure why this would cause the mapforce.exe instances to crash though.


Joanne
Rune Stilling
Greenhorn

Joined: Nov 10, 2011
Posts: 5
I debugged that part already. p.waitFor() actually waits for the MapForce-process to complete and doesn't return right away (as it would do in a command prompt). I don't know what technique MapForce.exe is using that makes this difference. So when p.waitFor() returns it's because MapForce.exe has either completed it's task or chrashed. The latter being the case when I launch multiple threads as described above.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You should definitely read When Runtime.exec() won't. When using java.lang.Process and especially its waitFor method that article will be enlightening.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rune Stilling
Greenhorn

Joined: Nov 10, 2011
Posts: 5
Well I did, and I don't see that I have any problems with waitFor(). As I wrote it works perferctly as long as I only launche one instance of MapForce.exe.

Where is that you see in that article anything that helps here?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

The call to p.waitFor() could cause your program to wait indefinitely if you do not clear the process' output and error streams. Not following the instructions in the article may not be the cause of your problems, but it's better to follow them anyway.
Rune Stilling
Greenhorn

Joined: Nov 10, 2011
Posts: 5
Ok - I'll handle that. But it's not the cause of my problem, and I still don't see anything in that article that explains what is going on.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You could also look at the ProcessBuilder class which makes it easier to work with processes.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rune Stilling
Greenhorn

Joined: Nov 10, 2011
Posts: 5
Thanks - I tried and it didn't make any difference which shouldn't be surprising since:

JDK 5.0 added a new class, ProcessBuilder to do the equivalent of the exec() method of Runtime.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I didn't say it would be an magic solution, it's just a wrapper around Runtime.exec(). But using the inheritIO() method makes it a lot easier to clear out/err streams.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

And calling redirectErrorStream(true) will require you to handle only one stream instead of two.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Launching multiple instances of Windows app fails using Runtime.getRuntime().exec()
 
Similar Threads
Connecting to a router
How to open command prompt from a java program
click and go to website
Runtime .getRuntime(). exec(cmd)
HttpURLConnection - Luanch Browser