Java uses Runtime to call code blocked by external programs. javaruntime

Source: Internet
Author: User

Java uses Runtime to call code blocked by external programs. javaruntime

Sometimes some external programs are called in java code, such as SwfTools to convert swf and ffmpeg to convert videos. If your code is written as follows: runtime.getruntime(cmd.exe c (command), you will find that the program is executed in one click, and it will be executed in the command line for a while because java has not waited for the execution of external programs, in this case, you need to use blocking to wait for external program execution results:

InputStream stderr = process.getInputStream();InputStreamReader isr = new InputStreamReader(stderr, "GBK");BufferedReader br = new BufferedReader(isr);String line = null;while ((line = br.readLine()) != null)    System.out.println(line);int exitValue = process.waitFor();

For general external programs, you can use the partial code, so there is no problem when using 2swf.exe.

However, I found that for ffmpeg, the above Code will keep the program stuck and you need to use another method to encapsulate it into a method, as shown below:

     @SuppressWarnings("static-access")public static int doWaitFor(Process process) {InputStream in = null;InputStream err = null;int exitValue = -1; // returned to caller when p is finishedtry {in = process.getInputStream();err = process.getErrorStream();boolean finished = false; // Set to true when p is finishedwhile (!finished) {try {while (in.available() > 0) {// Print the output of our system callCharacter c = new Character((char) in.read());System.out.print(c);}while (err.available() > 0) {// Print the output of our system callCharacter c = new Character((char) err.read());System.out.print(c);}// Ask the process for its exitValue. If the process// is not finished, an IllegalThreadStateException// is thrown. If it is finished, we fall through and// the variable finished is set to true.exitValue = process.exitValue();finished = true;} catch (IllegalThreadStateException e) {// Process is not finished yet;// Sleep a little to save on CPU cyclesThread.currentThread().sleep(500);}}} catch (Exception e) {e.printStackTrace();} finally {try {if (in != null) {in.close();}} catch (IOException e) {e.printStackTrace();}if (err != null) {try {err.close();} catch (IOException e) {e.printStackTrace();}}}return exitValue;}

  

 


How to call external applications in java?

Import java. io .*;

Class Runtime1

{

Public Runtime1 ()

{

Try {

Runtime.getruntime(cmd.exe c ("C: \ Program Files \ Microsoft Visual Studio \ Common \ MSDev98 \ Bin \ MSDEV. EXE ");

}

Catch (Exception e)

{

}

}

Public static void main (String [] args)

{

New Runtime1 ();

}

}

// Replace the brackets behind the exec code line 1 with the path of your application. Note that the path is added with a double oblique rod.

How can I call another java program in a java program?

Put the two files under the same package so that you can access another java program.
The general method is to instantiate Class B in program A, and then call the method in Class B through the method name B.

Runtime.getruntime(cmd.exe c ("external program ");

It is equivalent to entering "external program" in the cmd console and pressing enter to execute

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