Rookie007
Rookie007

Reputation: 1219

How to kill a java program with java program

I have scenario, where I need to kill a java process.

Here is my scenario:

I have web application in that web application I have implemented a Listener class like below

   public class MyListener extends ServletContextListener {        
    java.lang.Process ps  = null;

public void contextInitialized(ServletContextEvent arg0) {
   // This will be excuted when my web application executed
       String command= "cmd.exe start maybatch";// This command is to execute a batch program that triggers java program   
      ps =  Runtime.getRunTime().exec(command);
}

public void contextDestroyed(ServletContextEvent arg0) {
    //This part will be executed when server shutdown happens.
    // Here I want to close the java process which was triggered when project deployed.
    if( ps !=null)
        ps.destroy();
    }  
}

}

What my requirement is to close the Java process when my tomcat is shutting down and start the java program when my web application deployed.

Everything is working fine till to start the java process when Project is deployed.

But I dont know how to close the java process which was triggered when project is deployed

Any help would be appreciated. Thanks in advance.

Sorry to be not clear.. mybatch lanches a new java program.

Upvotes: 1

Views: 1501

Answers (3)

Eric
Eric

Reputation: 1297

This is a hack for Windows. Using the two DOS commands tasklist and taskkill, you should be able to do what you need.

1) tasklist

// After starting the process (exec) find and record it's process ID
Process psGetId = Runtime.getRuntime().exec("cmd.exe start tasklist /V /FO \"CSV\" /FI \"IMAGENAME eq cmd.exe*\"");
InputSteam is = psGetId.getInputStream();
// now parse the incoming stream searching for the PID that you need

Try manually running the tasklist command as described above to see what it returns, then you'll know what to expect from the InputStream

2) taskkill

// Assuming you have found and saved the process ID from above, the following can be used to kill it
Process psKillId = Runtime.getRuntime().exec("cmd.exe start taskkill /F /PID " + savedProcessId);

Remember, this is a hack and will only work under Windows, for other OS's you'll need to use other commands.

Upvotes: 1

Rookie007
Rookie007

Reputation: 1219

I have used below line in my java program which will be triggered by mybatch

  String processName =
          java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
    Strnig proccessId =  processName.split("@")[0];
  // Now I have saved this in a file and file will be saved somewhere available to my listner class.

Then I will read this proceesID from my listener class and I will kill taskkill /F /PID proccesID

I know this is a hack. Better than this will appreciated .. Thank you.

Upvotes: 0

Grigory
Grigory

Reputation: 465

first if you want to be informed about shutdown of web application and kill your subprocess you should use contextDestroyed() method instead of contextInitialized() in your code snippet. So basically you need to swap content of your methods.

public class MyListener extends ServletContextListener {        
  java.lang.Process ps  = null;

    public void contextInitialized(ServletContextEvent arg0) {
       // This will be excuted when my web application executed
           String command= "cmd.exe start maybatch";// This command is to execute a batch program that triggers java program   
          ps =  Runtime.getRunTime().exec(command);
    }

    public void contextDestroyed(ServletContextEvent arg0) {
        //This part will be executed when server shutdown happens.
        // Here I want to close the java process which was triggered when project deployed.
        if( ps !=null)
            ps.destroy();
        }  
    }
  }

UPD

Is it possible for you to avoid usage of batch file ? If yes you will be able to shutdown spawned java application. PS i found this link quite useful:

Start a java process (using Runtime.exec / ProcessBuilder.start) with low priority?

Upvotes: 1

Related Questions