package spider.scrapyd;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class Command {
public static String exec(String[] cmd, File path) {
final StringBuffer stringBuffer = new StringBuffer();
try {
final Process process = Runtime.getRuntime().exec(cmd, null, path);
new Thread(new Runnable() {
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
process.getOutputStream().close();
process.waitFor();
if (process.exitValue() == 0) {
System.out.println("successfully execute command");
}
} catch (Exception e) {
e.printStackTrace();
}
String str = stringBuffer.toString();
System.out.println(str);
return str ;
}
public static void main(String[] args) {
// String[] cmd = new String[] { "cmd", "/c", "wmic cpu get name" };
String[] cmd = new String[] { "cmd", "/c", "scrapyd-deploy -p hrtencent -v r1.0.0 " };
File f = new File("F:\\pywork\\PycharmProjects\\TencentHR\\hrtencent");
String str = exec(cmd,f);
System.out.println(str);
}
}
public static String exec(String[] cmd, File path) {
final StringBuffer stringBuffer = new StringBuffer();
try {
// 执行cmd指定的命令,
final Process process = Runtime.getRuntime().exec(cmd, null, path);
// 启动监控线程,读取process的标准输出
new Thread(new Runnable() {
public void run() {
try {
// 创建reader对象,关联到process的输出
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = null;
// 读取输出内容,保存到stringBuffer中
while ((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
// 启动监控线程,读取process的错误输出(操作和标准输出一致)
new Thread(new Runnable() {
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
process.getOutputStream().close();
// 等待process结束
process.waitFor();
// 如果process执行完后返回0,则打印成功信息
if (process.exitValue() == 0) {
System.out.println("successfully execute command");
}
} catch (Exception e) {
e.printStackTrace();
}
// 打印获取的标准输出和错误输出,并返回
String str = stringBuffer.toString();
System.out.println(str);
return str ;
}