0
点赞
收藏
分享

微信扫一扫

Java代码执行Linux脚本


Java代码执行Linux脚本

  • ​​1. 代码示例​​
java.io.IOException: Cannot run program "./home/jack/copyReport.sh": error=2, No such file or directory

1. 代码示例

/**
* @author Jack魏
* @date 2021-04-08
*/
public class TestRun {
public static void main(String[] args) {
copyRepot();
}

private static boolean IS_LINUX = false;

static {
IS_LINUX = System.getProperty("os.name").toLowerCase().contains("linux");
}
/**
* 执行拷贝脚本
*/
private static void copyRepot() {
try {
String cmd = "source /home/jack/copyReport.sh";
if (IS_LINUX){
String[] cmdArr = {"/bin/sh", "-c", cmd};
if (IS_LINUX){
Process proc = Runtime.getRuntime().exec(cmdArr);
proc.waitFor(); //阻塞,直到上述命令执行完
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


举报

相关推荐

0 条评论