How to execute the doscommand in Java

Source: Internet
Author: User


The following is a typical program mode:



Process = runtime.getruntime(cmd.exe C (". // p.exe ");

Process. waitfor ();


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"). In Windows 95/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:

Exec ("START. // a.doc ");

Exec ("C: // program files // Microsoft Office // 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 is usually not closed automatically after the program is executed, leading to Java application blocking in waitfor (). 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 process class provided by 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:


String ls_1;

Process = runtime.getruntime(cmd.exe C ("CMD/C dir // windows ");

Bufferedreader = new bufferedreader (/

New inputstreamreader (process. getinputstream ());

While (ls_1 = bufferedreader. Readline ())! = NULL)

System. Out. println (ls_1 );


Process. waitfor ();


The above content is reprinted ~ The following content is original!

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: 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.