1

Possible Duplicate:
Linux commands from Java

I am developing a Java application where I need to run a linux command from my application. How to do this?

Community
  • 1
  • 1
minhaz
  • 503
  • 1
  • 5
  • 12

3 Answers3

2

You can use this:

/* build up command and launch */
String command = "DO SOMETHING";

try {
    Runtime.getRuntime().exec(command);
} catch (Exception ex) {
    ex.printStackTrace();
}
Baz
  • 36,440
  • 11
  • 68
  • 94
1

You can use Runtime.exec() method.

18bytes
  • 5,951
  • 7
  • 42
  • 69
1

Example of such:

String [] arrs = new String [3] ;
arrs[0] = "/bin/bash";
arrs[1] = "-c";
arrs[2] = "rm s*"  ;
pp=Runtime.getRuntime().exec(arrs);
pp.waitFor();

Which removes files starting with s from the directory where the java code is stored.

Arpssss
  • 3,850
  • 6
  • 36
  • 80