liunx shell java打包_通过java代码打包调用linux命令执行shell脚本

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:51   2034   0

package linux;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class JavaExecuShell {

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

String cmd = "sh /root/test/test.sh " + args[0];

System.out.println(cmd);

Process proc = Runtime.getRuntime().exec(cmd);

/**

* 可执行程序的输出可能会很多,而运行的输出窗口的缓冲区有限,会造成waitfor一直阻塞

* 解决的办法是用Java的Process类提供的getInputStream,getErrosStream方法

* 让java虚拟机捕获调用程序的标准输出,错误输出.让waitfor在输出之前读掉输出缓冲区的内容

*

/

String flag = “”;

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

while (bufferedReader.readLine() != null) {

System.out.println(“result-----” + flag);

}

bufferedReader.close();

/*

* 等待脚本执行完成

*/

proc.waitFor();

} catch (IOException | InterruptedException e) {

e.printStackTrace();

}

}

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP