java调用.sh脚本
时间: 2023-09-27 20:08:01 浏览: 109
要通过Java调用.sh脚本,你可以使用Runtime类的exec方法来执行shell命令。可以使用以下代码来执行.sh脚本:
String[] cmd = { "sh", "/path/to/script.sh", "parameter1", "parameter2" };
try {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("line = " + line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
在这段代码中,我们定义了一个字符串数组cmd,其中第一个元素是"sh",表示使用shell来执行命令,第二个元素是"/path/to/script.sh",表示要执行的.sh脚本的路径,后面的参数可以根据需要进行更改。然后通过Runtime类的exec方法执行这个命令,最后通过BufferedReader来读取命令的输出并打印出来。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [java调用shell脚本传参数](https://blog.csdn.net/weixin_57463074/article/details/127802692)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [使用java执行bat、sh脚本文件](https://blog.csdn.net/gxy6661159/article/details/128615151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文