I don’t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands.
import java.io.*;
import java.util.*;
public class Exec {
public static void main(String args[]) throws IOException {
Process proc = Runtime.getRuntime().e xec(args);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
Usage:
java Exec VALID_COMMAND
Example:
$ java Exec echo hello
hello
ps: I had to write “e xec” instead of exec because it was triggering some very strange security protection in the blog engine here. If you need to compile this code change that. =P Also there’s no error handling, you should pass a valid command when executing this code.
Be First to Comment