Java调用cmd小Demo

一、应用cmd打开一个文件

Process process = null;

String resumePath=" E:/temp/aaa.html";

try {

process = Runtime.getRuntime().exec("cmd /k start"+resumePath);

} catch (IOException e) {

e.printStackTrace();

}

二、应用cmd运行程序

1)执行运行node.js命令

Process process = null;

String comand58 ="cmd.exe /k start node E:\\bootside-crawler\\58index.js";

ListprocessList = new ArrayList();

try {

//Thread.sleep(5000);

//process = Runtime.getRuntime().exec("/root/2.sh");  传参时需要用空格隔开

process = Runtime.getRuntime().exec(comand58+""+canshu);

BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = "";

while ((line = input.readLine()) != null ) {

processList.add(line);

}

input.close();

} catch (IOException e) {

e.printStackTrace();

}

for (String line : processList) {

//if (line.indexOf("nginx") <0)

//logger.info(line);

}

2)运行exe程序

Process process = null;

//E:\\node\\node.js 需要打开文件的位置

String comand ="cmd.exe /k start  E:\\node\\node.js";

ListprocessList = new ArrayList();

try {

//Thread.sleep(5000);

process = Runtime.getRuntime().exec(comand);

BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = "";

while ((line = input.readLine()) != null) {

processList.add(line);

}

input.close();

} catch (IOException e) {

e.printStackTrace();

}

你可能感兴趣的:(Java调用cmd小Demo)