Killing system process from java

By neokrates, written on May 20, 2010

java   snippet

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 165 views
Rate it
Ad
Poll
  • What SVN client do you use?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

This example explained here
 

import java.io.File;
import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.io.FileUtils;

/**
 * This file tries to eliminate the existing 'firefox' process 
 * from java. It is an example. Change it into real java process
 * killer by adding parameters. extend it with windows cmd process
 * kill script etc..
 *
 * @author diuw
 * 
 */
public class JavaProcessKiller {
 private void run() {

  // Go for mysql(only current user process can be killed)
  String targetName = "firefox";
  String line = "kill -9 `ps -ef | grep " + targetName + " | awk '{print $2}'`;";

  // Create a tmp file. Write permissions!?
  try {
   FileUtils.writeStringToFile(new File("killProcess.bash" , line);

   // Execute the file we just creted. No flags are due if it is
   // executed with bash directly
   CommandLine commandLine = CommandLine.parse("bash killProcess.bash" ;

   try {
    DefaultExecutor executor = new DefaultExecutor();
    executor.execute(commandLine);
   } catch (ExecuteException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }

  } catch (IOException e1) {
   e1.printStackTrace();
  }

 }

 public static void main(String[] args) {
  new JavaProcessKiller().run();
 }

}

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +4 (from 4 votes)
4 votes 'YES'  0 votes 'NO'
TAGS
RELATED
Pages
Posts
SOCIAL
Be Sociable, Share!

Leave a Reply