Java.getRuntime() problems? Commons “exec” seems to be simple (with killing process from java example)By neokrates, written on May 20, 2010 |
howto |
- neokrates
- Email: uwarov@yahoo.com
- Website: http://www.thinkplexx.com
- Join date: 05-31-09
- Posts: 20
What new build tool would you consider as Ant replacement?
- I prefer something else (88%, 7 Votes)
- Nant (13%, 1 Votes)
- Ant is still the best (13%, 1 Votes)
- Rant (0%, 0 Votes)
- Gant (0%, 0 Votes)
Total Voters: 8
Loading ...
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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!
|
LEARN MORE (amazon bookstore)
|
|
TAGS
|
|
RELATED
|
Pages
Posts
|
|
SOCIAL
|
|
INCOMING SEARCH TERMS
|



















Great! thanks for the share!
Arron
Like or Dislike:
0
0
This is exactly what I needed. Thanks!
Like or Dislike:
0
0
you are welcome
Like or Dislike:
0
0