Java调用shell脚本解决传参和权限问题的方法
作者:双斜杠少年 时间:2023-04-27 05:50:37
1. java 执行shell
java 通过 Runtime.getRuntime().exec() 方法执行 shell 的命令或 脚本,exec()方法的参数可以是脚本的路径也可以是直接的 shell命令
代码如下(此代码是存在问题的。完整代码请看2):
/**
* 执行shell
* @param execCmd 使用命令 或 脚本标志位
* @param para 传入参数
*/
private static void execShell(boolean execCmd, String... para) {
StringBuffer paras = new StringBuffer();
Arrays.stream(para).forEach(x -> paras.append(x).append(" "));
try {
String cmd = "", shpath = "";
if (execCmd) {
// 命令模式
shpath = "echo";
} else {
//脚本路径
shpath = "/Users/yangyibo/Desktop/callShell.sh";
}
cmd = shpath + " " + paras.toString();
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
2. 遇到的问题和解决
传参问题,当传递的参数字符串中包含空格时,上边的方法会把参数截断,默认为参数只到空格处。
解决:将shell 命令或脚本 和参数 放在一个 数组中,然后将数组传入exec()方法中。
权限问题,当我们用 this.getClass().getResource("/callShell.sh").getPath() 获取脚本位置的时候取的 target 下的shell脚本,这时候 shell 脚本是没有执行权限的。
解决:在执行脚本之前,先赋予脚本执行权限。
完整的代码如下
/**
* 解决了 参数中包含 空格和脚本没有执行权限的问题
* @param scriptPath 脚本路径
* @param para 参数数组
*/
private void execShell(String scriptPath, String ... para) {
try {
String[] cmd = new String[]{scriptPath};
//为了解决参数中包含空格
cmd=ArrayUtils.addAll(cmd,para);
//解决脚本没有执行权限
ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755",scriptPath);
Process process = builder.start();
process.waitFor();
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
//执行结果
String result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
源码位置:
https://github.com/527515025/JavaTest/tree/master/src/main/java/com/us/callShell
参考://www.jb51.net/article/61529.htm
来源:https://blog.csdn.net/u012373815/article/details/78771689
标签:java,shell,脚本,传参,权限问题
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android自定义控件之继承ViewGroup创建新容器
2023-06-15 08:50:11
![](https://img.aspxhome.com/file/2023/8/98688_0s.jpg)
Android开发入门环境快速搭建实战教程
2022-06-17 15:59:41
![](https://img.aspxhome.com/file/2023/3/139473_0s.jpg)
Unity实现游戏存档框架
2023-02-10 13:05:15
解析HikariCP一百行代码轻松掌握多线程
2023-03-21 19:11:00
一行java代码实现高斯模糊效果
2023-02-07 17:31:34
![](https://img.aspxhome.com/file/2023/5/78975_0s.jpg)
java根据负载自动抓取jstack dump详情
2021-09-05 01:47:05
![](https://img.aspxhome.com/file/2023/6/77106_0s.png)
控制Android LED灯颜色的代码实例
2022-12-30 02:53:20
Java interrupt()方法使用实例介绍
2022-05-04 11:09:35
Android调试出现The selected device is incompatible问题解决
2023-08-11 12:58:34
![](https://img.aspxhome.com/file/2023/5/137935_0s.jpg)
Android Drawable代码编写的新姿势分享
2021-11-21 04:11:12
![](https://img.aspxhome.com/file/2023/8/122208_0s.png)
Android convinientbanner顶部广告轮播控件使用详解
2021-08-20 13:09:09
kafka的消息存储机制和原理分析
2022-02-01 13:07:39
![](https://img.aspxhome.com/file/2023/0/100050_0s.png)
Android基于广播事件机制实现简单定时提醒功能代码
2023-07-10 09:57:55
java实现倒序读取文件功能示例分享
2023-07-12 09:23:54
Android RecyclerView缓存复用原理解析
2022-04-29 10:51:03
![](https://img.aspxhome.com/file/2023/9/138739_0s.png)
Java double转BigDecimal的注意事项说明
2022-12-13 14:44:38
c语言动态数组示例
2023-11-02 22:56:44
简述Mybatis增删改查实例代码
2023-03-06 18:07:53
![](https://img.aspxhome.com/file/2023/1/65311_0s.png)
详解Eclipse 字体、字号的设置、最佳字体推荐
2023-11-26 12:25:32
![](https://img.aspxhome.com/file/2023/9/60149_0s.png)
SpringBoot整合EasyExcel实现文件导入导出
2021-11-07 17:14:51