Skip to content

Instantly share code, notes, and snippets.

@HydrangeaPurple
Last active July 15, 2021 08:08
Show Gist options
  • Save HydrangeaPurple/cff0a4c025091a41935474aa2664f9f9 to your computer and use it in GitHub Desktop.
Save HydrangeaPurple/cff0a4c025091a41935474aa2664f9f9 to your computer and use it in GitHub Desktop.
java执行cmd命令
/**
* 判断操作系统
*
* @author chengyiqun
* @version V1.0
* @date 2021/7/15 11:07
*/
public class JudgeSystemUtils {
public static boolean isLinux() {
return System.getProperty("os.name").toLowerCase().contains("linux");
}
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
}
public class Main{
public static void main(String[] args) {
// 终端的起始路径
File path = new File("/devt-work/proj/admin/cyqtest");
Runtime rt = Runtime.getRuntime();
Process process = null;// 终端进程
OutputStream command = null; // 终端命令
try {
if (isWindows()) {
process = rt.exec("cmd", null, path);
} else if (isLinux()){
process = rt.exec("sh", null, path);
} else {
throw new BusiException("不支持的操作系统");
}
// 接受命令行
command = process.getOutputStream();
// 命令行输出使用异步线程
ProcessOutputThread outputThread = new ProcessOutputThread(process.getInputStream());
ProcessOutputThread errOutputThread = new ProcessOutputThread(process.getErrorStream());
outputThread.start();
errOutputThread.start();
// TODO 执行命令
command.write("npm install\n".getBytes());
command.write("npm run build\n".getBytes());
// 命令结束
command.write("exit\n".getBytes());
command.flush();
try {
int i = process.waitFor();
// 0 是成功, 但是判断 dist 文件夹更方便
System.out.println("i = " + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 子线程合并
try {
outputThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
errOutputThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 获取控制台输出结果
List<String> outputList = outputThread.getOutputList();
List<String> errOutputList = errOutputThread.getOutputList();
System.out.println("outputList = " + outputList);
System.out.println("errOutputList = " + errOutputList);
} catch (IOException e) {
e.printStackTrace();
} catch(BusiException e){
e.printStackTrace();
} finally {
if (command != null) {
try { command.close(); } catch (IOException e) { e.printStackTrace(); }
}
if (process != null) {
process.destroy();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
/**
* 异步获取cmd执行输入流(cmd的std输出和err输出)
*
* @author chengyiqun
* @version V1.0
* @date 2021/7/15 14:50
*/
public class ProcessOutputThread extends Thread {
private InputStream is;
private List<String> outputList;
public ProcessOutputThread(InputStream is) throws IOException {
if (null == is) {
throw new IOException("the provided InputStream is null");
}
this.is = is;
this.outputList = new LinkedList<>();
}
public List<String> getOutputList() {
return this.outputList;
}
@Override
public void run() {
InputStreamReader ir = null;
BufferedReader br = null;
try {
if (isWindows()) {
ir = new InputStreamReader(this.is, "GBK");
} else {
ir = new InputStreamReader(this.is);
}
br = new BufferedReader(ir);
String output = null;
while (null != (output = br.readLine())) {
// system.out
this.outputList.add(output);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != ir) {
try {
ir.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != this.is) {
try {
this.is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment