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

Runtime.getRuntime().exec() fails intermittently.

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I have a program which needs to call exec every 15 seconds.
It works fine for a while but then starts failing. It could be a few minutes or could be a few hours before this happens.

Its also fails with

Some of the time but the majority of the time its CreateProcess error=1

From this point on the exec method will keep failing for me.

I have read the article http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html and implemented all the steps there.

The system I am running exec() on is Windows 2008

Any suggestions?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

John Reilly wrote:
Any suggestions?


Post your code and if you can post the code of the executable or batch file you are executing.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63



calls


The proc = Runtime.getRuntime().exec(cmdArgs.toArray(new String[cmdArgs.size()]));
line runs fine for some random period of time but then starts returning the error below.
Runtime.getRuntime().exec() IOException Cannot run program "cmd.exe": CreateProcess error=1, Incorrect function

Could it be something to do with the input and output streams using up resources by been set up a number of times?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I don't see any code showing how you handle the process stdout, stderr or return code. Is there any?

P.S. . The only time you will need to invoke using cmd.exe is if you need to pipe the to 'findstr'. If you write Java code to do the selection rather than using 'findstr' then' you don't need to invoke tasklist with cmd.exe. One of my examples for the article I am writing invokes it using

and it runs happily all day as long as I process stdout and stderr correctly
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63



James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I can see nothing obviously wrong and, apart from not using 'findstr' to select lines, the only thing I might change are :-

1) I would construct both threads before start()ing either of them. This should not make any difference but I don't know.

2) I would do the join() after the waitFor(). Though it makes me uncomfortable doing the join() before the waitFor() I cannot say that what you are doing is wrong.

Sorry I can't be of more help.

P.S. Consider doing the filtering in Java. I try to do as little as I can get away with in the exec().


------------> Am I missing something? I do not see how you use the result of the exec() since you discard all stdout and stderr! <-----------

Also, you silently discard all Exceptions in StreamGobbler so you don't know if one occurs.

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I pass up the result of

to determine if the command found the process I was looking for.

Thanks for you help.
I tried putting the joing after the wairFor to no avail.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

John Reilly wrote:I pass up the result of

to determine if the command found the process I was looking for.


:-)))))))))) I know I keep harping on about this but you could very simply parse the stdout yourself looking for the process name on each line. That way you won't need to use cmd.exe .

I still think you should use more informative exception handling in the StreamGobbler.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Why does cmd.exe cause such problems?
I would have to change a lot of stuff around to parse the stdout as I run other commands as well with this exec method.

Could I use ProcessBuilder to run the commands?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I'm not saying avoiding cmd.exe will get rid of your problems; when working with Windows my experience is that the simpler you make the exec() the better and the more you process in Java the better. In *nix using a sensible shell this is much much less of a problem.

The parsing of stdout is easy. Instead of StreamGobbler I use this abstract Runnable

and implement the template abstract method processLine(). This allows me to make the line processing fairly complex. Currently I'm working on a Linux example that uses JFreeChart to chart a running memory usage. I process each line of the command "free -s 1.0" and add the output to the chart model. The concrete processLine() method is


Using String.indexOf() you could look for the process name and set a flag if found.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I think I solved the problem I took out the join on the input and error gobbler threads.
What may of been happening is that when exec messed up the join's were never going to finish and so the thread that called exec was just left there.

I’ll leave it running over the weekend to see if it keeps working correctly.


Should I put a time out on the Process waitFor method, as it something happened when exec was running could the calling method be left waiting indefinitely for wairFor to return?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

John Reilly wrote:I think I solved the problem I took out the join on the input and error gobbler threads.


I'm not keen on that solution.


What may of been happening is that when exec messed up the join's were never going to finish and so the thread that called exec was just left there.


I'm not convinced of that but I can't suggest anything better.


I’ll leave it running over the weekend to see if it keeps working correctly.


Should I put a time out on the Process waitFor method, as it something happened when exec was running could the calling method be left waiting indefinitely for wairFor to return?


Since there is no provision for a timeout on the waitFor() method you will have to create your own timeout and call the destroy() method.

This morning I did a couple of experiments on Windows XP (I don't have access to Windows Server). Emulating your approach using my utility classes the following code locks up most of the time.
while (true)
It does not matter whether or not I close() the streams or run process stdin and/or stderr in a separate threads or join() any threads I create. It may not lockup immediately but it does lock up fairly soon after I start it running.

Processing the tasklist in Java (I know I know I know - you don't like the idea) using the following code :-

it has run now for more than 5 hours without locking up once!

This just re-enforces my belief that the less clever one is using cmd.exe the better.

Currently I'm looking at providing your script to the cmd.exe stdin. Current results are not promising - I seems to lock up frequently.

Update : Writing the command to the Process stdin the first exec() works OK but after than it usually locks up.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

It failed again after a number of hours.
I cant find what the error code means below.

I may have to implement your suggestion.

 
Consider Paul's rocket mass heater.
 
subject: Runtime.getRuntime().exec() fails intermittently.
 
Similar Threads
Compile and create instance of a new java program from existing java program
command execution error
Very basic Ant stuff <exec>
problem to rin openoffice
How to run simple windows commands through java program?