Solved

End a Process.

Posted on 2004-08-07
12
790 Views
Last Modified: 2008-02-01
Hi everyone,
is it possible to end a process in Java? I know how to start one, however, ending one doesn't seem the most easiest task.
Is there a simple method to do this? Or will it involve JNI? :o\

Thanks!
[r.D]
0
Comment
Question by:DrWarezz
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 7

Expert Comment

by:JugglerW
ID: 11743508
Not possible as far as I know without JNI
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 11743533
Hmm.. I was dreading that answer. lol :o\
Okay; do you JugglerW (or anyone else) no how to do this using JNI then??).

Thanks,
[r.D]
0
 
LVL 2

Expert Comment

by:timbauer
ID: 11743605
Check out the exec methods in java.lang.Runtime
Process proc = Runtime.getRuntime().exec(...)
proc.destroy(); //rudely ends process (maps to TerminateProcess on win32)

If you want fine grain control, you will probably want JNI, but this might work for you too.

Regards,
- Tim
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 2

Expert Comment

by:timbauer
ID: 11743623
Sorry, I didn't correctly read the question, but some of what I mentioned is still
applicable.

The crippling disadvantage of this is that you had to be the one to start the process
so you have a Process object.

When you get into JNI land, check out TerminateProcess (for win32),
If you are on Unix you can send a signal via:
kill(pid, SIGTERM);
   or
kill(pid, SIGKILL);
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 11743655
Okay; thanks, Tim.
Ermm.. Although I'm quite comfortable with Java as a whole, I haven't tackled JNI yet. Could you recommend a good JNI tutorial??

Thanks,
[r.D]
0
 
LVL 35

Expert Comment

by:girionis
ID: 11743940
Well how do you usually kill a process in your os? The best way would be to use JNI but as timbauer pointed out you could kill a process using the runtime class. On Linux for example you could do

Process proc = Runtime.getRuntime().exec("kill 1857"); // kill the process with id 1857
0
 
LVL 35

Expert Comment

by:girionis
ID: 11743947
Of course if you started a process by using a Process class it is far easier :)
0
 
LVL 35

Accepted Solution

by:
girionis earned 30 total points
ID: 11744010
Maybe this will help: http://www.codeproject.com/useritems/jnisample.asp (you need to register to download the source code).
0
 
LVL 92

Expert Comment

by:objects
ID: 11744738
0
 

Expert Comment

by:ganeshmoorthy
ID: 11750387
You can't terminate or end process in a nice away in Java. You need to
either write something in JNI that calls either SendMessage() to end the
process nicely (will most likely require user interaction)
or
calls TerminateProcess() to KILL the process;
either install a program that does that and takes the process's name on the command line and call it via Runtime.exec().

To start a new process, call Runtime.exec().
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 11750812
Okay, thanks everyone.
I'm going on holiday tomorrow, therefore I'm going to end this question now.
I think that girionis' answer would serve most useful to myself, but also, thanks for the JNI tutorial link, objects, and also, thanks to everyone else that took part.

Thanks,
[r.D]
0
 
LVL 35

Expert Comment

by:girionis
ID: 11750936
Thank you for accepting :)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 84
How to convert String matching to regex in java 4 32
Java exception bubble up 2 18
hibernate example using maven 12 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question