A project cannot be developed by only one programming language or by one person. Therefore, Java programmers must learn to cooperate with programmers using other programming languages. Let me publish a tool class-Java extension package and publish the corresponding algorithm. Java external interface refers to calling and interacting with the core Console (command line) Program of algorithms written in other programming languages using Java. In order to make the external interface easier, I have compiled this encapsulation class, easy to use.
First, let me introduce the algorithm of this external program-I think this algorithm is called "3 process-3 stream algorithm ". 3. process refers to: 1. Master/target standard input process; 2. Target standard output listening process; 3. Target standard error output listening process. 3. Stream refers to: 1. Target input stream; 2. Target output stream; 3. Target error output stream. Let me explain separately. The target input stream refers to the stream that the Java program inputs information to the console of the external program. It is like you are a Java program. You start cmd, enter the "ping" command in it, which is the first stream established between the Java program and the target program. The target output stream refers to the information that the target program outputs (or prints) to the Java program. It is like cmd is the target program. You are a Java program. After you enter the "ping" command, cmd gave you a response. Because you didn't use a parameter, ping considers that you don't know how to use it, and then provides standard output to you, telling you how to use it. Then, the target error output stream refers to the error message that the target program outputs to the Java program, just like you are a Java program. You input "abcdefg" to the cmd program ", then cmd thought that you entered the wrong command, so it started the error stream and reported the error message to you. The second two processes are designed to monitor the standard output and standard error output streams at any time.
Next, let's take a look at how to implement these functions. First, let's take a look at how to execute the strings that start the external program:
==
Is it very simple? on the Internet, if you search for exec, you will see the code that has been passed in. Then, let's take a look at how to detect the error message:
ErrorStream String err="" = BufferedReader( printer(BufferedReader br) ((temp=br.readLine())!==err+
This Code is one of the three classes I encapsulated: ErrorStream, used to detect error messages. I will not list other code here. My encapsulated source code is also provided to you. You can download and view it!
Another point: all fields, methods, and classes in my encapsulation are set to public, but there are many classes that you do not know or cannot understand. Please do not perform operations to prevent errors. Then let me talk about it. The only static method in this encapsulation is ExternalProgramUtil. the pexe method encapsulates exec commands that directly use Runtime for operations, except that, to save you some code, no initialization operation is performed, and no fields are initialized. Be careful when using this method!
Next, we are playing ExternalProgramUtil! I will provide download services for 360 and Baidu online storage:
360: http://yunpan.cn/Qp8T6QxPWTJSe
Baidu: http://pan.baidu.com/s/1ntlsSit
If the failure or use problems come to me, mail: zhangyutong@zhangyutong.net, QQ: 2276768747.
In addition, the local log file in the jar package cannot be filled in with the download link because the obtained online storage links and logs cannot be synchronized. Sorry!
You are welcome to download and use it! Thank you for your support of BKMMSC-Golden Rooster!