The processbuilder class is a new class of j2se 1.5in java.lang. it can be used to create sub-processes and is more convenient to use than runtime.exe C.
In the commandexecutor class, script_dir indicates the directory location of the script. For example:ProgramThe default value is the System user directory.
Public static final string script_dir = system. getproperty ("user. dir ");
You can customize your desired directory:
Public static final string script_dir = "your directory ";
The invoke method can call the script, and the result returns the information generated by the call script. Note that the information at this time contains the execution correct information or execution error information, the command cannot be determined based on this method or the script must be successfully executed. This method is applicable to execution commands that do not require a return value.
In the execute method, the first parameter is the expected execution result, as shown in the example.CodeCheck whether the returned information of the Java command contains 1.6.0 _ 26. Therefore, the call example is as follows:
Commandexecutor executor = commandexecutor. createcommandexecutor ();
System.out.println(executor.exe cute ("1.6.0 _ 26", "Java", "-version "));
If you want to call the script command on Linux, you can change it:
Executor.exe cute ("Expected parameter", "sh", "test. Sh ")
The following code uses processbuilder to create and execute commands:
1 /**
2 * The tool provides the method to invoke the system command or the script.
3 * @ Author Gabriel
4 * @ Date 2012-3-15
5 */
6 Public Final Class Commandexecutor {
7
8 Public Static Final String script_dir = system. getproperty ("user. dir ");
9
10 Private Commandexecutor (){}
11
12 /**
13 * Create a command executor instance.
14 * @ Return
15 */
16 Public Static Commandexecutor createcommandexecutor (){
17 Return New Commandexecutor ();
18 }
19
20 /**
21 * Invoke the command.
22 * @ Param Command
23 * @ Return
24 */
25 Public Synchronized String invoke (string... command ){
26 Processbuilder Pb = New Processbuilder (command );
27 // Set the default directory of the script file.
28 PB. Directory ( New File (script_dir ));
29
30 PB. redirecterrorstream ( True );
31
32 Process proc = Null ;
33 Bufferedreader BR = Null ;
34 Inputstream is = Null ;
35 Stringbuffer MSG = New Stringbuffer ();
36 Try {
37 Proc = Pb. Start ();
38 Is = Proc. getinputstream ();
39 BR = New Bufferedreader ( New Inputstreamreader (is ));
40 String TMP = "";
41 While ( Null ! = (TMP = Br. Readline ())){
42 MSG. append (TMP );
43 MSG. append (system. getproperty ("line. separator "));
44 }
45 } Catch (Ioexception e ){
46 }
47 Return MSG. tostring ();
48 }
49
50 /**
51 * Verify the result of the invoking command.
52 * @ Param Pattern
53 * @ Param Command
54 * @ Return
55 */
56 Public Boolean Execute (string pattern, string... command ){
57 String target = invoke (command );
58 Return Null ! = Target & target. Contains (pattern );
59 }
60
61
62 Public Static Void Main (string [] ARGs) Throws Ioexception {
63 Commandexecutor executor = commandexecutor. createcommandexecutor ();
64 System.out.println(executor.exe cute ("1.6.0 _ 26", "Java", "-version "));
65 }
66 }