引入Hutool工具类
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
</dependency>
使用方法
String compileCmd = "你想要执行的命令"
Process compileProcess = Runtime.getRuntime().exec(compileCmd); // 执行命令,compileProcess是返回的流,Process可以读取执行命令后的返回内容的流
Hutool的exec命令
/**
* Executes the specified string command in a separate process.
*
* <p>This is a convenience method. An invocation of the form
* <tt>exec(command)</tt>
* behaves in exactly the same way as the invocation
* <tt>{@link #exec(String, String[], File) exec}(command, null, null)</tt>.
*
* @param command a specified system command.
*
* @return A new {@link Process} object for managing the subprocess
*
* @throws SecurityException
* If a security manager exists and its
* {@link SecurityManager#checkExec checkExec}
* method doesn't allow creation of the subprocess
*
* @throws IOException
* If an I/O error occurs
*
* @throws NullPointerException
* If <code>command</code> is <code>null</code>
*
* @throws IllegalArgumentException
* If <code>command</code> is empty
*
* @see #exec(String[], String[], File)
* @see ProcessBuilder
*/
public Process exec(String command) throws IOException {
return exec(command, null, null);
}
感兴趣的可以去查看Hutool的官网:Hutool官网