How to Kill Java Process in Linux Operating System using Java Code

Java Code for killing the Java process in Linux OS.

Hi,
    Here I have written the small snippet for killing the java running  process in linux OS.

public void checkprocess()
{
    String s = null;
    int x=0;
    try
    {
        Process p = Runtime.getRuntime().exec("jps"); //JPS command is used for showing all the java running process with ID.
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((s = stdInput.readLine()) != null)
        {
            String[] tokens = s.split(" ");
            if(tokens.length!=2)
            {
                throw new IllegalArgumentException();
            }
            String commandid = tokens[0];
            String prcess = tokens[1];
            System.out.println("Processes  Name is : "+prcesss);
            if(prcs.equals("Process Name which you want to kill"))
            {
                Process p1 = Runtime.getRuntime().exec("kill "+commandid+"");
                x++;
            }
            if(x>1)
            {
                System.out.println("Processes  Name is : "+prcess);
                System.out.println("Processes  ID is : "+commandid);
                System.exit(0);
            }
        }
    }
    catch (IOException e)
    {
    System.out.println("exception : "+e);
    }
}

Comments

  1. Bollywood News in Hindi - Check out the latest Bollywood news, new Hindi movie reviews, box office collection updates
    venom 2 Let There Be Carnage Full Movie Download & Review
    83 Full Movie Download & Review

    ReplyDelete

Post a Comment

Popular posts from this blog

Window could not start the Apache tomcat 6 on Local Computer.For more information, review the System Events Logs.If this is a non-Microsoft service,contact service vendor, and refer to service-specific error code 0.

How to Read Excel File in VC++....

ORACLE DBA INTERVIEW QUESTION & ANSWERS