I found the following code which uses Runtime.getRuntime().exec
to run an arbitrary program (like Notepad.exe ) .
public class RuntimeDemo {
public static void main(String[] args) {
try {
// create a new array of 2 strings
String[] cmdArray = new String[2];
// first argument is the program we want to open
cmdArray[0] = "notepad.exe";
// second argument is a txt file we want to open with notepad
cmdArray[1] = "example.txt";
// print a message
System.out.println("Executing notepad.exe and opening example.txt");
// create a process and execute cmdArray and currect environment
Process process = Runtime.getRuntime().exec(cmdArray,null);
// print another message
System.out.println("example.txt should now open.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I ran this from Eclipse, and it opens Notepad.exe (or I can use calc.exe also ) .
But what if we want to run an arbitrary Java command from this program, something like :
java -version
-
will this run? I see a problem here is that .. where do you see the results of it? If I'm running command-line, it comes out to the command line.. but here?