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 6 of 6

Thread: Kill a process

  1. #1
    Member
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Kill a process

    Hi all,
    I want to know how to close the application through process id(like kill command in unix)through java code.Is it possible in java?Please do me a favor.Thanks in advance.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Kill a process

    There is the System.exit() method that will force your program to stop execution, but you need to be careful using this, especially if you have either an Event driven model or a multi-threaded program.

    System.exit(0); // 0 is the return value, any int is acceptable

  3. #3
    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: Kill a process

    Subhvi, what you are proposing is not possible through Java alone. Java runs in the JVM which means it doesnt have access to anything going on outside of the JVM. If you want to do this you would have to look into an approach like using JNI to code a C solution using the windows functions which you can then call from within Java.


    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Kill a process

    PLEASE WHAT IS THIS ABOUT?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Kill a process

    You may want to try using the Runtime.exec() function. You can try to retrieve a process ID for a running app, then run another exec command to try and shutdown the process. How you do so however is very platform dependent, and it may not behave universally
    Last edited by copeg; January 14th, 2010 at 08:55 AM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Kill a process

    Ooh.. I totally misread what you wanted I thought you wanted to kill your java program's process, but instead you want to kill a different process... yes, see the above two posts for how to do that.

Similar Threads

  1. Replies: 4
    Last Post: April 27th, 2010, 01:18 AM
  2. process control
    By ttsdinesh in forum Java Native Interface
    Replies: 6
    Last Post: October 27th, 2009, 06:29 PM
  3. How to kill proceses
    By oyekunmi in forum Java Theory & Questions
    Replies: 3
    Last Post: October 16th, 2009, 08:34 AM
  4. Patch Process
    By Drakenmul in forum Java Theory & Questions
    Replies: 3
    Last Post: August 2nd, 2009, 03:09 AM