Java magic Hall: Find the aid provider --runtime.exe c

Source: Internet
Author: User

Java magic Hall: Find the aid provider --runtime.exe c
I. Preface although Java is fully dirty, there is always a weakness. For example, to obtain hardware information such as CPU, we can use JNI to call C/C ++, however, coders who are unfamiliar with C/C ++ and Windows APIs are a series of complex learning and pitfall processes. Can we use a simpler method and a lower learning cost? The answer is yes. When the function implementation is at the top of the list, it is the most concise and powerful practice to use another stone. The Runtime.exe c method opens such a path for us. 2. Understand the function of java.lang.Runtime.exe c: This method is used to call external programs and redirect standard input, standard output, and standard errors of external programs to the buffer pool. The function is the same as "running" in windows. Method overload: exec (String command), calls an external program, and the input parameter command is the startup path or command of an external executable program. Exec (String [] cmdArray): calls an external program. The elements of the input parameter cmdArray are combined into a complete external executable program startup path or command. Exec (String command, String [] envp): sets the system environment variable before calling an external program. This variable is only used by the command input parameter. Each element of envp is a system environment variable, and the string format is "environment variable name = environment variable value ". Exec (String command, String [] envp, File dir), in addition to setting system environment variables, it also sets the current working directory through the input parameter dir. Instance -- run the dir command in the current directory and save the result to the c: \ dir.txt text file. Prerequisites: assume that the current user's home directory is c: \ user \ fsjohnhuang c: \ user \ fsjohnhuang directory structure c: \ user \ fsjohnhuang | -- jottings | --test.txt d: \ test directory structure d: \ test | -- movies | --readme.txt code snippet copy code Runtime r = Runtime. getRuntime (); try {Process proc = r.exe c ("cmd/c dir> % dest %", new String [] {"dest = c: \ dir.txt ", new File ("d :\\ test")}); int exitVal = proc. waitFor (); // block the current thread and wait for the external program to stop and obtain the result code Syst. Em. out. println (exitVal = 0? "Successful": "failed");} catch (Exception e) {e. printStackTrace ();} copy the code to execute the code and view the content of the c: \ dir.txt file, as shown in the following figure: copy the volume in the Code drive D without labels. The serial number of the volume is 8074-B214 D: \ test directory <DIR> movies2014/03/31 8,642 readme.txt copy code III. Note 1. runtime.exe c () is not a cmd or shell environment, so you cannot directly call commands such as dir. To call commands under the command line, refer to the instance in section 2nd. 2. through the Process instance. getInputStream () and Process instances. the input stream and error Stream Obtained by getErrorStream () are provided by the buffer pool to the current Java program, rather than directly obtaining the standard output stream and standard error stream of the external program. The buffer pool capacity is certain. Therefore, if external programs continuously output content to the buffer pool during operation, when the buffer pool is full, the external program is paused until there is space in the buffer pool to receive output content from the external program. (This problem occurs when a large number of files are copied using the xcopy command.) The solution is that the current Java program keeps reading the content of the buffer pool, freeing up the buffer pool space. 4. It is definitely a pitfall to solve the problems mentioned in the points of attention in section 3rd. We can use either of the following methods to copy the code Runtime r = Runtime. getRuntime (); try {Process proc = r.exe c ("cmd/c dir"); // assume that this operation causes a large amount of output content. // read the content of the buffer pool using the prepare stream, free up space BufferedReader reader = new BufferedReader (new InputStreamReader (proc. getInputStream (), "gbk"); String line = null; while (line = reader. readLine ())! = Null) {System. out. println (line);} // or use a byte stream to read the content of the buffer pool to free up space // ByteArrayOutputStream pool = new ByteArrayOutputStream (); // byte [] buffer = new byte [1024]; // int count =-1; // while (count = proc. getInputStream (). read (buffer ))! =-1) {// pool. write (buffer, 0, count); // buffer = new byte [1024]; //} // System. out. println (pool. toString ("gbk"); int exitVal = proc. waitFor (); System. out. println (exitVal = 0? "Successful": "failed");} catch (Exception e) {e. printStackTrace ();} copy the code. Note the following: the external program will be automatically closed after execution. Otherwise, both the ignore stream and the byte stream cannot read the data, the Java Process is blocked because the stream Terminator cannot be read. Cmd/c tells the cmd environment process to close itself after execution is complete.

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.