如何在linux上从java项目中的res文件夹运行.sh文件?
使用此代码,我可以从java项目中的res文件夹访问图像如何在linux上从java项目中的res文件夹运行.sh文件?,java,shell,Java,Shell,使用此代码,我可以从java项目中的res文件夹访问图像 MyClass.class.getResource("/Images/main.jpg"); 但是,如何在linux上的java项目中从res文件夹运行.sh文件呢 为此提供解决方案?您可以使用Runtime.exec() 听起来很粗略,你想实现什么?我想在linux操作系统上从project的res文件夹运行.sh文件。@用你的.sh文件路径调用excuteCommand,它就会运行it@Pradeep例如,您将.
MyClass.class.getResource("/Images/main.jpg");
但是,如何在linux上的java项目中从res文件夹运行.sh文件呢
为此提供解决方案?您可以使用
Runtime.exec()
听起来很粗略,你想实现什么?我想在linux操作系统上从project的res文件夹运行.sh文件。@用你的.sh文件路径调用excuteCommand,它就会运行it@Pradeep例如,您将.sh文件放在/image/mysh.sh中,然后调用excuteCommand(“/image/mysh.sh”)我可以在linux和windows上运行.sh文件吗?@Pradeep您可以,这段代码将在windows中调用cmd来运行您的.sh,在linux中调用sh来运行您的.sh,只需确保您的文件路径。
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;
}