java - 如何从 Linux 上的 java 项目中的 res 文件夹运行 .sh 文件?
问题描述
我可以使用此代码从 java 项目中的 res 文件夹中访问图像。
MyClass.class.getResource("/Images/main.jpg");
但是,如何在 Linux 上运行 java 项目中 res 文件夹中的 .sh 文件?
给这个解决方案?
解决方案
您可以使用Runtime.exec()
public static void excuteCommand(String filePath) throws IOException{
File file = new File(filePath);
if(!file.isFile()){
throw new IllegalArgumentException("The file " + filePath + " does not exist");
}
if(isLinux()){
Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", filePath}, null);
}else if(isWindows()){
Runtime.getRuntime().exec("cmd /c start " + filePath);
}
}
public static boolean isLinux(){
String os = System.getProperty("os.name");
return os.toLowerCase().indexOf("linux") >= 0;
}
public static boolean isWindows(){
String os = System.getProperty("os.name");
return os.toLowerCase().indexOf("windows") >= 0;
}
推荐阅读
- angular - 如何在handsontable中添加时间选择器
- twig - 如何在下面的树枝中使用多维数组我们共享输入字段
- windows - 使用powershell删除文件中的特定行
- java - 没有父元素的 JAXB XML 重复交替 XmlElements
- cplex - 在 CPLEX Concert 技术中获得双重公式
- python - Python图像到字符串转换没有模块工作
- reactjs - 通过 Kendo-React-ui 使用服务器端分页
- javascript - 从角度 4 中的 URL 读取 CSV 时出现未知字符
- tensorflow - 存储整个图
- bash - 根据参数挂钩 bash 自动完成