There are generally two ways to start the operating system process through Java
1. By starting
().exec());
Exec has three overload methods, which can set the corresponding commands, system properties, and the working directory of the new process.
2. Start through ProcessBuilder
ProcessBuilder is a recommended way to use after Java 1.5, and it has more processing power.
like:
- Obtain the execution information of the process, including standard output and standard error output;
- Set the current working directory;
- Change environmental parameters, etc.;
ProcessBuilder pb = new ProcessBuilder(); List<String> cmds = new ArrayList<String>(); ("/bin/sh"); ("/data/soft/java"); (""); (cmds); (); (); (); (new File(getWorkerDir())); ();
After calling the start() method, you can return a Process class.
This class has a structure as follows:
public abstract class Process { //Return the output stream of normal input of the connection child process abstract public OutputStream getOutputStream(); //Return the input stream output from the connection child process abstract public InputStream getInputStream(); //Return the input stream of the abnormal output of the connection child process abstract public InputStream getErrorStream(); //Prompt the current thread to wait until only if the process has ended. The function returns immediately when the child process ends abstract public int waitFor() throws InterruptedException; //Return the exit value at the end of the child process abstract public int exitValue(); //Kill the child process abstract public void destroy(); }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.