How to call the doscommand in Java

Source: Internet
Author: User
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:
...
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 client program is automatically updated today. A brief description is to download the update package from the server, decompress it locally, and delete it ~ Easy to use ~

However, the problem is that using the Java zip module to decompress files is not as simple as you think. resources need to be released, so you cannot delete the original zip file accidentally ~ Resource usage is indeed a big problem, but fortunately, after the client program is updated, it will be restarted, and everything will disappear ~ I am also confused about the issue that zip files cannot be deleted ~ Use del to hard Delete ~ Be sure to pay attention here!

Process = runtime.getruntime(cmd.exe C ("CMD/C del F: // aaa.doc ");
There is no problem with such a call ~

Process = runtime.getruntime(cmd.exe C ("del F: // aaa.doc ");
It is impossible to write like this ~

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.