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.

Results 1 to 4 of 4

Thread: How to kill proceses

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to kill proceses

    I'm looking for a way to kill proceses on my machine from my java program just like task manager


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to kill proceses

    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.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to kill proceses

    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

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: How to kill proceses

    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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales