Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rune Stilling
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rune Stilling
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should definitely read When Runtime.exec() won't. When using java.lang.Process and especially its waitFor method that article will be enlightening.
 
Rune Stilling
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also look at the ProcessBuilder class which makes it easier to work with processes.
 
Rune Stilling
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And calling redirectErrorStream(true) will require you to handle only one stream instead of two.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic