If you mean starting an executable, this is (eg on Window$)
Code:
try {
String[] cmdLine = new String[] {"myProgram.exe", "arg1"}; // program name (possibly with path) + zero or more command line args
String[] env = new String[] {"PATH=%PATH%;C:\\somedir"}; // just an example, null is also valid
java.io.File dir = new java.io.File("C:\\Program Files\\workingdir"); // null for current directory
Process myProcess = Runtime.getRuntime().exec(cmdLine, env, dir);
} catch (java.io.IOException ioX) {
ioX.printStackTrace();
}
which starts "myProgram.exe" as a subprocess of the Java VM. You can also feed (standard) input to and get output from this newly created process via
Code:
java.io.OutputStream stdin = Process.getOutputStream(); //STDIN of myProgram.exe
java.io.InputStream stdout = Process.getInputStream(); //STDOUT of myProgram.exe
java.io.InputStream stderr = Process.getErrorStream(); //STDERR of myProgram.exe
Note that from your java program's point of view STDIN of myProgram.exe is something you want to write to and hence an OutputStream, while STDOUT and STDERR are streams you want to read from and hence InputStreams.
If you need not set environment variables or the working directory, there are also abbreviated versions of Runtime.exec(). There are also the methods Process.exitValue() and Process.waitFor().
Consider to explicitely kill the process - via Process.destroy() - if you are not sure that it does properly terminate since the subprocess will execute asynchronously when there are no more references to the Process object (or maybe you want that).
Finally, Java 1.5 offers the more convenient and flexible ProcessBuilder to create Processes.