This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Kill a process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Kill a process" Watch "Kill a process" New topic

Kill a process

Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
I'm starting a new window by using
Process p = Runtime.getRuntime().exec(cmdarray);
i.e by a new process.
The problem is each time I do this, a new window opens. But I want only a single window to be open. If a window is already open, I don't need to open it again.
I tried something like this:
Process p = null;
if (p != null)
p = Runtime.getRuntime().exec(cmdarray);
This doesn't work. The process is not killed when I call destroy().
Can anyone suggest me how to have only one window open?
- Raja.

Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
I'm really sorry. Actually, this one works. I was testing it on a different copy.
- Raja.
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Allow me to follow up with a similar question, even though you were testing on a different copy, and your problem seemed solved.
Working on UNIX, I use Java to get a UNIX process and run a script. I can monitor the processes by UNIX command ps -Af -U $LOGNAME. I can see this java process shown in shell.
The UNIX process runs and exit with exit value of 0, seems working fine. ps command doesn't show the java process.
But sometimes, the process exits normally and shows exit value of 0, which I monitor by using the Process.exitValue(). When I use the ps command, that process is still shown in shell. 1st Why?
Then in java code, I use Process.destroy() after the process exits, hoping to clean up the process. It doesn't work, i.e. the process can't be destroyed. 2nd Why?
Any ideas about the two Why's?

Tony Yan<br /> <br />IBM Certified Developer XML and Related Technology<br />Sun Certified Web Component Developer For J2EE Platform<br />Sun Certified Programmer For Java 2 Platform
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
Did you try calling destroy() without setting the process to null first?
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
I tried to call destroy() before setting process=null. Didn't help. I found from the JavaDoc the following, and I think this is likely the problem coming from.
java.lang.Process class
"The Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.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.
The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously. "
It fits my case since my external cmd is a UNIX script. And subprocess still runs (SOMETIMES) even after java code shows it exited. I used some methods to clean up the three Streams, didn't help either.
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Since I can't control over UNIX's IO status (it return exit of 0 for success, even before IO finishes), I bypassed this problem from Java side by doing the following.
I make the class Runnable and each time a create a Thread to exec the external command. I set up a timeout limit. And when ever the thread waits for more than the timeout, I assume it has got into deadlock and let the thread to quit using join(). I will try this same command again and the second time always worked. Not an elegant solution as I wanted, but working.
Ayman Mahfouz

Joined: Jul 23, 2004
Posts: 1
I have a problem too.
I launch a batch file that invokes a java program on Windows. I have the java program deliberately go into an infinite loop. When I try to use process.destroy() to kill the process it does not work. You can see the process still running in the task manager, and its output keeps pouring in.

Any ideas?

Joined: Dec 07, 2004
Posts: 1
Hi Tony,

I tried to understand your solution that seporating the runtime exec into different thread and wanted to knwo what do you mean by using joint()...

Can you show how you did that exactly? please

jQuery in Action, 2nd edition
subject: Kill a process
Similar Threads
Open a command prompt using Runtime.getRuntime().exec() and run a jar file
Help needed for running for this runtime code
run unix commands inside Java Servlet
running oggenc from a java app