Java.getRuntime() problems? Commons “exec” seems to be simple (with killing process from java example)

By neokrates, written on May 20, 2010

howto

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

    View Results

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

I remember myself fighting with java.getRuntime() issues. More then a day was invested in different nuances of bash or cmd execution from java. Well, how nice that I can use Commons “exec” package and not care about most of those problems.

Software:

  • Java
  • Commons-exec 1.0

1

Bash script

In Java there is no easy way to kill external process directly (as far as I know).

So, I call bash from java to make it work. In my example I kill firefox process (just an example, in real life I am a firefox browser fan, really:) )

One-liner:

kill -9 `ps -ef | grep firefox | awk '{print $2}'`

2

Project setup with maven

I use Eclipse to create basic maven project, but any IDE would do the trick.

My pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">;
  <modelVersion>4.0.0</modelVersion>
  <groupId>JavaProcessKiller</groupId>
  <artifactId>JavaProcessKiller</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-exec</artifactId>
  <version>1.0</version>
</dependency>
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>1.4</version>
</dependency>
  </dependencies>
</project>

3

Java class

Java class will make use of commons. I guess it works even simpler then that, but I had a problems putting my bash one-liner directly into CommandLine.parse. But if I dump a bash file using FileUtils.writeStringToFile

FileUtils.writeStringToFile(new File("killProcess.bash" , line); 

 
It works like charm

so:
1 – Bash one liner

String line = "kill -9 `ps -ef | grep " + targetName + " | awk '{print $2}'`;"; 

 
2 – Write it into .bash file:

FileUtils.writeStringToFile(new File("killProcess.bash" , line); 

 
3 – Run it

CommandLine commandLine = CommandLine.parse("bash killProcess.bash" ;

try {
 DefaultExecutor executor = new DefaultExecutor();
 executor.execute(commandLine); 

 
4 – Go!

new JavaProcessKiller().run();

 
Full source:

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 firefox(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();
 }

}

4

Run it

Use:

mvn clean eclipse:ecipse compile

 
from the command line, then you can run the class from target

Or, you may use one of Eclipse m2 plugins. After setting up a m2 project the file should be runnable per [Ctrl] + [F11]

That’s it. Have fun! :)

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +2 (from 4 votes)
3 votes 'YES'  1 votes 'NO'


TAGS

SOCIAL
Be Sociable, Share!

INCOMING SEARCH TERMS


3 Responses to “Java.getRuntime() problems? Commons “exec” seems to be simple (with killing process from java example)”

  1. mattress says:

    Great! thanks for the share!
    Arron

  2. bill says:

    This is exactly what I needed. Thanks!

Leave a Reply