Java runtime.exe C ()

Source: Internet
Author: User

0. runtime.exe C () is used to execute external programs or commands

 

 

1. runtime.exe C () has four call Methods

* Public process exec (string command );
* Public process exec (string [] cmdarray );
* Public process exec (string command, string [] envp );
* Public process exec (string [] cmdarray, string [] envp );

 

 

2. Get the return value of program execution. 0 is success.

You need to use the waitfor () function, such

PROCESS p = runtime.getruntime(cmd.exe C ("javac ");

(Processing .....)
Int exitval = P. waitfor ();

 


3. Get the execution result or error message.

You must use bufferedinputstream and bufferreader to obtain the data. Otherwise, the program will

For example, if you get the error message, use P. geterrorstream () and then output it:

Bufferedinputstream in = new bufferedinputstream (P. geterrorstream ());
Bufferedreader BR = new bufferedreader (New inputstreamreader (in ));

 

 

4. runtime.exe C () is not the same as directly executing the command line command!

Ah, I have suffered a lot here. Runtime.exe C () has limitations. For some commands, you cannot directly pass the content in command line as a string parameter to Exec ().

Such as redirection. For example:

Javap-l XXX> output.txt

In this case, the second type of exec overload is used, that is, the input parameter is string []:

PROCESS p = runtime.getruntime(cmd.exe C (New String [] {"/bin/sh", "-c", "javap-l XXX> output.txt "});

 

Reprinted please indicate the source of this article: http://blog.csdn.net/flying881114/archive/2011/03/23/6272472.aspx

 

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.