To execute a linux shell script in the project, you need to execute External commands such as system commands and linux commands in the java environment, I checked some information online and sorted it out as follows.
Java executes External commands mainly based on two types of Process and Runtime.
I. Process
ProcessBuilder. start () creates a local Process and returns an instance of a Process subclass. This instance can obtain information about the Process or control the Process. This process does not have its own terminal, and its operation result io is redirected to its parent process. The parent process uses getInputStream (), getOutputStream (), getErrorStream () it provides input and output for sub-processes. If there is a buffer size limit for io streams, blocking may occur, resulting in deadlocks.
You can use destory () to forcibly kill the sub-process and exitValue () to return the execution result. If the sub-process needs to wait for return, call the waitFor () method to wait for the current thread until the sub-process exits.
Ii. Runtime class
Runtime.getruntime(cmd.exe c () obtains the Process class. The exec () method can be used in multiple overload operations and set different parameters for different situations. In addition, it should be noted that the minglin Method for running windows and linux is different.
Main (String [] args ){
String command = "notepad.exe txt.txt ";
{
Process process = runtime.getruntime(cmd.exe c (command );
BufferedInputStream bis = BufferedInputStream (
Process. getInputStream ());
BufferedReader br = BufferedReader (InputStreamReader (bis ));
String line;
(Line = br. readLine ())! = ){
System. out. println (line );
}
Process. waitFor ();
(Process. exitValue ()! = 0 ){
System. out. println ("error! ");
}
Bis. close ();
Br. close ();
} (IOException e ){
E. printStackTrace ();
} (InterruptedException e ){
E. printStackTrace ();
}
}
}
Iii. Apache Common-Exec
We strongly recommend that you use apache's third-party library, which provides more detailed settings and monitoring methods.
The command to be executed is called CommandLine. You can use the addArgument () method of this class to add parameters to it. The parse () method wraps your provided command into an executable command. The command is executed by the Executor class. The execute () method of the DefaultExecutor class executes the command, and exitValue can also be returned and received through this method. Set
Using line. addArgument ("/p ");
Using line. addArgument ("/h ");
Using line. addArgument ("$ {file }");
HashMap map = HashMap ();
Map. put ("file", File ("invoice.pdf "));
CommandLine. setSubstitutionMap (map );
DefaultExecuteResultHandler resultHandler = DefaultExecuteResultHandler ();
ExecuteWatchdog watchdog = ExecuteWatchdog (60*1000 );
Executor executor = DefaultExecutor ();
Executor. setExitValue (1 );
Executor. setWatchdog (watchdog );
Executor.exe cute (cmdLine, resultHandler );
/// Int exitValue = resultHandler. waitFor ();
The above is your understanding from the common-exec official documentation. If there is an error, please pat it!