Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : E words : exec.
There are also overloaded forms of exec(),
Runtime.getRuntime().exec ( "command.com /E:1900 /C MyBat.bat", null ); Runtime.getRuntime().exec ( "command.com /E:1900 /C MyBat.bat", null, "C:\\SomeDirectory" );
The second argument can be a String [], and can be used to set environment variables. In the second case, "C:\\SomeDirectory" specifies a directory for the process to start in. If, for instance, your process saves files to disk, then this form allows you to specify which directory they will be saved in.
// Windows 98 command.com Runtime.getRuntime().exec ("command.com /E:1900 /C MyBat.bat" ); // Windows NT/W2K/XP cmd.exe Runtime.getRuntime().exec ("cmd.exe /E:1900 /C MyCmd.cmd" ); // Windows 98 with 4DOS Runtime.getRuntime().exec ("C:\\4DOS601\\4DOS.COM /E:1900 /C MyBtm.btm" ); // Windows NT/W2K/XP with 4NT Runtime.getRuntime().exec ("C:\\4NT401\\4NT.EXE /E:1900 /C MyBtm.btm" ); // Linux Bash String[] cmd = {"/bin/bash", "-c", "rm /dirA/*"}; Runtime.getRuntime().exec ( cmd );
Windows and NT will let you feed a URL string to the command processor and it will find a browser, launch the browser, and render the page, e.g.
Runtime.getRuntime().exec ("command.com http://mindprod.com/ects.html" );
Another lower level approach that does not require extension associations to be quite as well set up is:
Runtime.getRuntime().exec ("rundll32 url.dll,FileProtocolHandler http://mindprod.com/ects.html" );
Note that a URL is not the same thing as a file name. You can point your browser at a local file with something like this: file://localhost/E:/mindprod/jgloss.html or file:///E|/mindprod/jgloss.html.
Composing just the right platform-specific command to launch browser and feed it a URL to display can be frustrating. You can use the BrowserLauncher package to do that for you.
Note that
rundll32.exe url.dll,FileProtocolHandler file:///E|/mindprod/jgloss.html
won't work on the command line because | is reserved as the piping operator, though it will work as an exec parameter passed directly to the rundll32.exe executable.
With explicit extensions and appropriately set up associations in Windows 95/98/ME/NT/2000/XP you can often bypass the command processor and invoke the file directly, even *.bat.
Similarly, for Unix/Linux you must spawn the program that can process the script, e.g. bash. However, you can run scripts directly with exec if you do two things:
Runtime.getRuntime().exec (new String[]{"/bin/sh", "-c", "echo $SHELL"}
Process.getOutputStream lets you write out data that will be used as input to the process. Don't forget to include any "\n" and "\r" characters the process is expecting. Process.getInputStream lets you read in the data that your process squirted out to stdout. In a complex case you could have four threads:
The other cruder way to get the output from the execed program is to spawn a command processor and use the > or 2> redirection operator to direct stdout or stderr to a file. When the spawned program completes, read the file.
If you wait for the Process, you might want to do it on some thread other than the main AWT event processing one, i.e. create a new Thread, otherwise your app will not be able to process AWT events while you wait.
In general you need to write to STDOUT and read from STDIN with separate threads to avoid deadlocks. The deadlock occurs if you try to read and/or write more than a certain amount (empirically it is around 4k).
You can't use exec from an Applet otherwise you would be able to wreck havoc on the client machine by spawning FORMAT C:. If you need to do that, you will need to have a signed Applet with high privilege.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 15711. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/exec.html | J:\mindprod\jgloss\exec.html | |||