java中调用cmd命令
2017-11-29 10:24
489 查看
在项目中使用到这方面的,备忘一下!
String cmdo = “chmod 777 start.sh”;
String inputtxt = file.getAbsolutePath() + File.separator + “input.txt”;
String outputtxt = file.getAbsolutePath() + File.separator + “output.txt”;
//首先要构建命令
String cmd = “sh start.sh –inputPath=” + inputtxt + ” –outputPath=” + outputtxt;//在Linux系统中
//构建执行文件夹
//这里为传进来的文件
BufferedReader stdInput = null;
BufferedReader stdError = null;
try {
logger.debug(“开始执行cmdo命令:” + cmd + “执行路径:” + file.getAbsolutePath());
Process execo = Runtime.getRuntime().exec(cmdo, null, file);
execo.waitFor();
logger.debug(“开始执行cmd命令:” + cmd + “执行路径:” + file.getAbsolutePath());
Process exec = Runtime.getRuntime().exec(cmd, null, file);
String s = null;
stdInput = new BufferedReader(new InputStreamReader(exec.getInputStream(), StandardCharsets.UTF_8));
stdError = new BufferedReader(new InputStreamReader(exec.getErrorStream(), StandardCharsets.UTF_8));
while ((s = stdInput.readLine()) != null) {
logger.error(s);
}
while ((s = stdError.readLine()) != null) {
logger.error(s);
}
int resulStatus = exec.waitFor();
if (resulStatus != 0) {
throw new Exception(“执行shell脚本出错了”);
}
logger.debug(“执行完shell脚本”);
} catch (InterruptedException e) {
logger.debug(“发生异常了1”);
} catch (Exception e) {
logger.debug(“发生异常了2”);
throw new Exception(“执行shell脚本出错了”);
} finally {
IOUtils.closeQuietly(stdInput);
IOUtils.closeQuietly(stdError);
}
String cmdo = “chmod 777 start.sh”;
String inputtxt = file.getAbsolutePath() + File.separator + “input.txt”;
String outputtxt = file.getAbsolutePath() + File.separator + “output.txt”;
//首先要构建命令
String cmd = “sh start.sh –inputPath=” + inputtxt + ” –outputPath=” + outputtxt;//在Linux系统中
//构建执行文件夹
//这里为传进来的文件
BufferedReader stdInput = null;
BufferedReader stdError = null;
try {
logger.debug(“开始执行cmdo命令:” + cmd + “执行路径:” + file.getAbsolutePath());
Process execo = Runtime.getRuntime().exec(cmdo, null, file);
execo.waitFor();
logger.debug(“开始执行cmd命令:” + cmd + “执行路径:” + file.getAbsolutePath());
Process exec = Runtime.getRuntime().exec(cmd, null, file);
String s = null;
stdInput = new BufferedReader(new InputStreamReader(exec.getInputStream(), StandardCharsets.UTF_8));
stdError = new BufferedReader(new InputStreamReader(exec.getErrorStream(), StandardCharsets.UTF_8));
while ((s = stdInput.readLine()) != null) {
logger.error(s);
}
while ((s = stdError.readLine()) != null) {
logger.error(s);
}
int resulStatus = exec.waitFor();
if (resulStatus != 0) {
throw new Exception(“执行shell脚本出错了”);
}
logger.debug(“执行完shell脚本”);
} catch (InterruptedException e) {
logger.debug(“发生异常了1”);
} catch (Exception e) {
logger.debug(“发生异常了2”);
throw new Exception(“执行shell脚本出错了”);
} finally {
IOUtils.closeQuietly(stdInput);
IOUtils.closeQuietly(stdError);
}
相关文章推荐
- Java调用QQ快速聊天-调用CMD命令打开网址
- Java调用cmd命令 打开一个站点
- Windows系统中Java调用cmd命令及执行exe程序的方法
- java 调用cmd命令进程阻塞学习
- java通过Runtime调用windows的cmd.exe命令
- Java 调用cmd.exe命令
- JAVA中调用CMD命令,并输出运行结果
- JAVA PHP 调用cmd命令
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值(如果有的话)
- java调用cmd命令
- Java调用CMD命令
- Java 调用cmd.exe命令
- Timer定时通过Runtime的exec方法调用命令“cmd exp..” 后,任务管理器不停的出现“Java.exe”和“cmd.exe”进程
- JAVA中调用CMD命令,并输出执行结果
- Java中调用windows cmd命令的两种方式
- java调用cmd执行maven命令
- java调用CMD命令
- java调用CMD命令显示所有进程的信息
- java中如何调用CMD命令及cmd大全
- Java调用CMD命令