Java calls a system command or executable program--Returns a runtime runtime object and then initiates another process to execute the command

Source: Internet
Author: User

The Java.lang.Runtime class makes it easy to invoke the operating system commands, or an executable program, the following small example I tested in Windows and Linux separately, both passed. The rationale is to first return the run-time object associated with the current Java application through Runtime.getruntime () and then call Run.exec (CMD) to start a process to execute the command (CMD is the command to be executed).

One, run an executable program

Executes an. exe file, or opens a file in a specific format, such as word, chm, or MP3, through the installed software.

1. Under Window you can execute an. exe file directly, such as executing my Tomcat installation file under the F disk, and writing the command as:

String cmd = "F:\\apache-tomcat-6.0.20.exe";

2. Open a Word document. If your system already has an Office application installed, you can open a Word document by calling the executable program in Word:

String cmd = "D:\\Program Files\\Microsoft Office\\office11\\winword. EXE F:\\test.doc ";

Of course this is a bit of a hassle, we want to open a Word document as long as double-click on it, do not need to find WINWORD.EXE. If you open each format of the file to find its executable program, that can be exhausted, we can open any known format of the file by the following code (as long as the installation of the software to open this file format), equivalent to double-click a file with the mouse icon:

String cmd = "cmd.exe/c start F:\\test.doc";

I wrote a small example of a process operation in C, the executable file compiled under Linux is called "fork_wait" and then the Java file is compiled into Testruntime.class and thrown to Linux, and the Java testruntime command is executed in the console. , the Testruntime and fork_wait programs are running successfully.

String cmd = "./fork_wait";

Second, execute a system command with standard output

The standard output of the execution command can be obtained by invoking the getInputStream () method of the process. The Windows CMD Console window and the Linux console perform the system fame and wealth format is the same, just the input command is different.

To perform a ping command in the Windows console, write as: String cmd = "ping www.baidu.com";

Execute linux ls command, can be written as: String cmd = "ls-l";

If you want to execute a command with parameters, you can use the form of a String array, such as:

String[] Cmd=new string[3];
Cmd[0]= "/bin/sh";
cmd[1]= "-C";
Cmd[2]= "ls-l./";

Here is a small example of my writing:

Package Com.why.runtime;import Java.io.bufferedinputstream;import Java.io.bufferedreader;import Java.io.inputstreamreader;public class Testruntime {public static void main (string[] args) {//windows//string cmd = "f:\\ Apache-tomcat-6.0.20.exe ";//string cmd =" D:\\Program Files\\Microsoft Office\\office11\\winword. EXE f:\\test.doc ";//string cmd =" cmd.exe/c start F:\\test.doc "; String cmd = "ping www.baidu.com";//linux//string cmd = "./fork_wait";//string cmd = "ls-l";//string[] cmd=new string[3]; Cmd[0]= "/bin/sh";//cmd[1]= "-C";//cmd[2]= "ls-l./"; Runtime run = Runtime.getruntime ();//Returns the runtime object that is associated with the current Java application try {Process p = run.exec (cmd);// Start another process to execute the command bufferedinputstream in = new Bufferedinputstream (P.getinputstream ()); BufferedReader INBR = new BufferedReader (new InputStreamReader (in)); String linestr;while ((linestr = Inbr.readline ()) = null)//Gets the command output information System.out.println (LINESTR) after execution in the console;//print out information// Checks whether the command failed to execute. if (p.waitfor () = 0) {if (p.exitvalue () = = 1)//p.exitvalue () ==0 means normal end, 1: Abnormal knotBundle System.err.println ("Command execution failed!");} Inbr.close (); In.close ();} catch (Exception e) {e.printstacktrace ();}}}

  

Java calls a system command or executable program--Returns a runtime runtime object and then initiates another process to execute the command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.