This example will capture the output (from stdio) of an external program.
import java.io.*;
public class CmdExec {
public static void main(String argv[]) {
try {
String line;
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tree.com /A");
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
catch (Exception err) {
err.printStackTrace();
}
}
}
ProcessBuilder class provides more controls overs the process to be started. It's possible to set a starting directory.
import java.io.*;
import java.util.*;
public class CmdProcessBuilder {
public static void main(String args[])
throws InterruptedException,IOException
{
List command = new ArrayList();
command.add(System.getenv("windir") +"\\system32\\"+"tree.com");
command.add("/A");
ProcessBuilder builder = new ProcessBuilder(command);
Map environ = builder.environment();
builder.directory(
new File(System.getenv("temp")));
System.out.println("Directory : " + System.getenv("temp") );
final Process process = builder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("Program terminated!");
}
}
The following example start a Dial-up connection on the Win plateform :
[Dialup.java]
public class Dialup {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime()
.exec("rundll32.exe rnaui.dll,RnaDial MyConnection");
p.waitFor();
System.out.println("Done.");
}
}
The "MyConnection" is the DUN and it's case sensitive.
You still need to press ENTER to CONNECT, there is an option in the Connection properties to connect automatically.
On NT and W2K, rnaui.dll is not available. Use rasdial.exe instead.
rasdial "connection name"
rasdial "connection name" /d to drop
rasdial /? for more options
To launch a Unix script
String[] cmd = {"/bin/sh", "-c", "ls > hello"};
Runtime.getRuntime().exec(cmd);
You can include a path for the program to be executed. On the Win plateform, you need to put the path in quotes if the path contains spaces.
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
"\"c:/program files/windows/notepad.exe\"");
p.waitFor();
}
}
If you need to pass arguments, it's safer to a String array especially if they contain spaces.
String[] cmd = { "myProgram.exe", "-o=This is an option" };
Runtime.getRuntime().exec(cmd);
PDF (Windows only)
[ShowPDF.java]
public class ShowPDF {
public static void main(String[] args) throws Exception {
Process p =
Runtime.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler mypdf.pdf");
p.waitFor();
System.out.println("Done.");
}
}
PDF (Mac only)
[ShowPDF.java]
public class ShowPDF {
public static void main (String[] args) throws Exception{
Process p = Runtime.getRuntime().exec("open /Documents/mypdf.pdf");
}
}
VBSCRIPT
// Win9x
Runtime.getRuntime().exec("start myscript.vbs");
// WinNT
Runtime.getRuntime().exec("cmd /c start myscript.vbs");
HTML Help (Windows only)
Runtime.getRuntime().exec("hh.exe myhelpfile.chm");
Any program using the Windows file association mechanism
where "file" is the filename of the data file ex. myresume.doc to start Word if the doc extension is associated with it.
Runtime.getRuntime().exec
("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());