简体   繁体   English

如何从 linux 上的 java 项目的 res 文件夹中运行.sh 文件?

[英]How to run .sh file from res folder in java project on linux?

I can access the image from res folder in java project with this code.我可以使用此代码从 java 项目中的 res 文件夹中访问图像。

MyClass.class.getResource("/Images/main.jpg");

But, How can I run.sh file from res folder in java project on linux?但是,如何从 linux 项目的 java 项目中的 res 文件夹中运行.sh 文件?

Give a solution for this?给这个解决方案?

You can use Runtime.exec()您可以使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM