【MER】java 运行 shell/cmd 命令

编程开发   © 文章版权由 admin 解释,禁止匿名转载

#楼主# 2022-12-13

package com.marssvn.svnapi.common;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import java.io.IOException;

public class CommandUtils {

/**
* log4j.Logger
*/
private static Logger logger = Logger.getLogger("CommandUtils");

/**
* execute command, windows: cmd, linux: /bin/sh
*
* @param command command
* @return Process
* @throws IOException IOException
*/
public static Process execute(String command) throws IOException {
// os
String os = System.getProperty("os.name").toLowerCase();

Process process;
if (os.startsWith("win")) {
logger.debug("cmd: " + command);
process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", command});
} else {
logger.debug("/bin/sh: " + command);
process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
}
String errorMsg = IOUtils.toString(process.getErrorStream(), System.getProperty("sun.jnu.encoding"));
if (StringUtils.isNotBlank(errorMsg)) {
logger.error(errorMsg);
}
return process;
}
}
// 调用代码
CommandUtils.execute("echo ipconfig > ip.txt");

沙发
昵称379988 2022-12-13
啊!沙发的感觉如此美好
地板
昵称379988 2022-12-14
这个是干嘛用的呢
4楼
玖月๑ 2022-12-26
奖励结算,当前结算等级:1;少侠辛苦了。

评论

登录后才可发表内容
  • 主题

    33

  • 帖子

    347

  • 关注者

    0

Copyright © 2019 凯特网.   Powered by HYBBS 2.3.4  

Runtime:0.0907s Mem:2085Kb