Use the runtime and process classes in Java to run external programs

Source: Internet
Author: User

Use runtime.getruntime(cmd.exe C () you can run an external program in Java Program .
1. exec (string command)
2. exec (string command, string envp [], file DIR)
3. exec (string cmd, string envp [])
4. exec (string cmdarray [])
5. exec (string cmdarray [], string envp [])
6. exec (string cmdarray [], string envp [], file DIR)
general applications can use the first version directly, and later versions are used when environment variables are passed. In versions 2 and 6, you can pass a directory to identify the current directory. Because some programs use relative directories, you must use this version.

cmd.exe/C start
you must also use the call when using the doscommand (such as DIR. If you want to interact with the called program, you need to use the process object returned by this method, through getinputstream (), getoutputstream () and geterrorstream () of process () you can obtain the input and output streams, and then use inputstream to obtain the output information of the program to the console. You can use outputstream to input commands to the program, which achieves the program switching function.

When writing an application in Java, you sometimes need to call another ready-made executable program or system command in the program. At this time, you can use the runtime class and process class methods provided by Java in combination. The following is a typical program mode:

1
2Process=Runtime.getruntime(cmd.exe C (". \ P.exe" );
3 Process. waitfor ();
4

In the above program, the first line of ". \ p.exe "is the name of the program to be executed, runtime. getruntime () returns the runtime object of the current application. The Exec () method of this object instructs the Java Virtual Machine to create a sub-process to execute the specified executable program, and return the process object instance corresponding to the sub-process. You can use process to control the execution of the sub-process or obtain information about the sub-process. The purpose of the second statement is to wait for the sub-process to complete and then execute it.
However, on Windows, improper processing may sometimes fail to get the expected results. The following are some situations that need attention in actual programming:
1. Execute dos internal commands
If you want to execute a dos internal command, there are two methods. One way is to include the command interpreter in the exec () parameter. For example, execute the Dir command. On NT, you can write it as exec ("cmd.exe/C dir"), and on Windows95/98, you can write it as cmdcommand.exe/C dir ", the parameter "/C" indicates that the DOS window is closed immediately after the command is executed. Another method is to place internal commands in a batch command my_dir.bat file and write them as exec ("my_dir.bat") in a Java program "). If only exec ("dir") is written, the Java Virtual Machine reports a runtime error. To ensure program portability, you must read the operating system platform in the program to call different command interpreters. The latter method does not require more processing.
2. Open an unexecutable File
Open an unexecutable file, but the file has an associated application, there are two ways. The following two methods can be used in Java to open a wordfile a.doc:

1Exec ("Start. \ a.doc" );
2Exec ("Files \ Microsoft Office \ winword.exe. \ a.doc");

Obviously, the previous method is simpler and more convenient.
3. Execute a DOS executable program with standard output
On Windows, the DOS window for running the called program does not close automatically after the program is executed, which causes the Java application to block the waitfor () statement. One possible cause of this phenomenon is that the standard output buffer of the executable program is not large enough because there are many standard outputs of the executable program. The solution is to use the methods provided by the process class in Java to enable the Java Virtual Machine to intercept the standard output of the DOS runtime window of the called program, in waitfor () read the content in the standard output buffer of the window before the command. A typical program is as follows:

1
2 String S;
3Process=Runtime.getruntime(cmd.exe C ("CMD/C dir \ Windows" );
4Bufferedreader= NewBufferedreader (New Inputstreamreader (process. getinputstream ());
5While(S=Bufferedreader. Readline ())! = Null )
6 System. Out. println (s );
7Process. waitfor ();

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.