How does Java call external external commands?
My own experience is Runtime.Getruntime.exec (cmd);
For example, the following code:
String [] cmd = new string [3];
IF (OsName.equals ("Windows NT") || OsName.Equals ("Windows 2000")) {cmd [0] = "cmd.exe"; cmd [1] = "/ c";
Else IF (OsName.equals ("Linux"))
{
CMD [0] = "sh"
CMD [1] = a directory under Linux
} Else if ("Windows 98")) {cmd [0] = "command.com"; cmd [1] = "/ c";} cmd [2] = "mysqldump --opt -u root YH_TPL> " backup_path filename ". SQL "; Process PS = runtime.getRuntime (). EXEC (CMD);
I saw someone else to reply to this type of question, I didn't know like this:
The Java is transferred from EXEC to Java's JVM, so it requires yourself to maintain your own environment. ============================================================================================================================================================================================================= ============================================================================================================================================================================================================= ==
Method of Java to call external programs: Chen Gang, Guilin, 97, graduated from the Department of Mathematics, Guangxi Normal University, temporarily at IBM China Research Center, a part-time engaged in software development, focus on java platform software development (2004.2-?). Email: Glchengang@hotmail.com blog: Glchengang.yeah.net Recently develops report modules, after generating report files (an Excel file) to play this file. Very simple, use Java to call external programs. The code is as follows: string path = "C: / Report _20040504.xls"; try {/ 相 相 相 相 相 相 - -> "Run" effect runtime.Getruntime (). EXEC ("cmd / c start" PATH);} catch (ioException e) {E.PrintStackTrace ();} But if the file is included in the path, it will be wrong. Going to Google, no, no, see CMD /? Help, I have not, I have tried many times, I found that the solution is actually very simple, it is ignorant. The code is as follows, no need to say. String path = "c: / documents and settings / giles / desktop / report _20040504.xls"; try {runtime.getRuntime (). EXEC ("cmd / c start excel /" path "/"); } catch (ioException e) {E.PrintStackTrace ();} ===================================== ============================================================================================================================================================================================================= ================== Script language = javascript1.2 src = "../ scripts / watermark.js"> script>
Execute An External Program
This Example Will Capture The Output (from stdio) of an external program.
Import java.io. *;
Public class cmdexec {
Public cmdexec (String cmdline) {
Try {
String line;
Process P = runtime.getRuntime (). EXEC (CMDLINE);
BufferedReader INPUT =
New BufferedReader
(New INPUTSTREAMREADER (P.GetInputStream ()));
While ((line = INPUT.READLINE ())! = null) {system.out.println (line);
}
INPUT.CLOSE ();
}
Catch (Exception Err) {
Err.PrintStackTrace ();
}
}
Public static void main (String Argv []) {
New cmdexec ("myprog.bat");
}
}
[myprog.bat]
Echo Hello World!
Note 1: Reading a Bat File Output Containing The Dir Command May Hang. I don't know why ...
NOTE 2: To make a shortcut:.. Create a shortcut to CmdExec.class Go in the shortcut properties and change the target as "java CmdExec" Change the run option to Minimized to hide the black DOS console when the BAT is executed.
Note 3: a Useful Link on The Subject: JavaWorld Article, THANKS to O. Thomann.
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.getime (). EXEC ("Dialup.bat");
p.WaitFor ();
System.out.println ("DONE.");
}
The Bat File Contains The dos call to bring the dialog. The "myconnection" is the dun and it's case sensitive.
[Dialup.bat]
Rundll32.exe rnaui.dll, RNADial myconnection
You STILL NEED TO PRESS ENTER TO Connect, There is an Option In The Connection Properties To Connect Automatical.
If You Want To Wait Until The Bat File Is Finished, Use "start / wait".
[Dialup.bat]
Start / Wait Rundll32.exe Rnaui.dll, RNADial MyConnection
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 winpageform, you need to put the path in set.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 saffer to a string array especiallyiffness.
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 ("ShowPdf.bat mypdf.pdf");
p.WaitFor ();
System.out.println ("DONE.");
}
}
[showpdf.bat]
Rundll32 Url.dll, FileProtocolhandler% 1
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 letter ife ing...
Runtime.getRuntime (). EXEC
("Rundll32 shell32.dll, shellexec_rundll" file.getabsolutePath ());