This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Kill a process

 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.destroy();
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?
Thanks,
- Raja.
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really sorry. Actually, this one works. I was testing it on a different copy.
- Raja.
 
Tony Yan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
TIA
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try calling destroy() without setting the process to null first?
 
Tony Yan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim:
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.
Regards,
 
Tony Yan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bakhtiar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic