Running Shell Script From External Directory: No such file or directory

前端 未结 2 1552
星月不相逢
星月不相逢 2020-12-10 07:45

I have a shell script file that i want to run from java. My java work space directory is different than the script\'s directory.

private final String script         


        
相关标签:
2条回答
  • 2020-12-10 08:14

    Your program clean.sh is not an executable as Java understands it, even though the underlying system understands it as executable.

    You need to tell Java what shell is needed to execute your command. Do (assuming you are using bash and it is installed at /bin/bash):

    private final String scriptPath = "/home/kemallin/Desktop/";
    
    public void cleanCSVScript() {
    
        String script = "clean.sh";
        try {
            Process awk = new ProcessBuilder("/bin/bash", scriptPath + script).start();
            awk.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-12-10 08:17

    You should do a chmod 755 /home/kemallin/Desktop/clean.sh and ensure the java process is run under the same userid

    0 讨论(0)
提交回复
热议问题