I'm looking for a way to kill proceses on my machine from my java program just like task manager
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I'm looking for a way to kill proceses on my machine from my java program just like task manager
Java is not great for this type of application. All things that are Operating System specific are hidden from the Java program by the JVM. To a Java program, the JVM is the Operating System. This is great for cross-platform possibilities, but it's a problem if you want to do OS specific tasks.
I'm sure Chris will point you in this direction - Java Native Interface - Wikipedia, the free encyclopedia
I'm not sure if this code will help you. It's using the Runtime.exec destroy() method. It opens IE, then 10 seconds later, kills it.
public class ExecTest { public static void main(String[] args) throws Exception { String execStr = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE"; Process proc = Runtime.getRuntime().exec(execStr); System.out.println("proc: " + proc); Thread.sleep(10000); System.out.println("destroying"); proc.destroy(); System.out.println("destroyed"); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
You could possibly do this by using Home - Sigar - Confluence
Here is a snippet for you to get started as well.
final Sigar sigar = new Sigar(); final long[] processes = sigar.getProcList(); for (final long processId : processes) { System.out.println(processId + " = " + ProcUtil.getDescription(sigar, processId)); final String processDescription = ProcUtil.getDescription(sigar, processId); if(processDescription.contains("notepad.exe")){ System.out.println("Found notepad.exe with id [" + processId + "] - KILLING IT!"); sigar.kill(processId, -9); } }
This code gets a list of all the processId's and then loops through them and get the description for each one, if the description contains "notepad.exe" it will kill that process.
Note
Sigar requires you to copy some native libraries onto the machine you intend to run this on. I tested this using Windows XP 32-bit OS.
Enjoy!
// Json
I would indeed point towards JNI for a true control over what you are doing as appose to destroy(), but that is a quick fix.
Json's solution using Sigar, is infact a JNI example. However they have taken the time to develop the C code for a large array of operating systems, so that this API is portable. Providing you move files, which of course will always be the case if you use JNI....Hey that's why they invented installers.
So if you wish to use JNI, you can either:
1) Implement your own, (you will need to know C or C++) and spend time making it platform independant if it requires it. But you will get FULL control over how everything is done.
2) Use Sigar, which does all the hard work for you and is the lazy mans option, providing it has all the features you require!
Regards,
Chris