Shell 跟Java 相互调用和获取结果
被调用的shell
a.sh
Shell代码
- #!/bin/bash
- echo 111
- exit 8
Java 代码
- public static void main(String[] args) throws IOException {
- Process p = Runtime.getRuntime().exec(command);
- InputStream is = p.getInputStream();
- int data;
- StringBuffer strBuffer = new StringBuffer();
- while ((data = is.read()) != -1) {
- strBuffer.append((char) data);
- }
- System.out.println("命令:\n" + command);
- System.out.println("结果:\n" + p.exitValue());
- System.out.println("log:\n" + strBuffer.toString());
- int ret = p.exitValue(); // 全路径
- System.exit(ret); // 直接返回shell执行的结果
- }
调用java的shell
test.sh
- #!/bin/bash
- #调用java打包后的jar文件
- java -jar test.jar
- #显示执行结果
- echo $?
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20