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

前端 未结 2 1558
星月不相逢
星月不相逢 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();
        }
    }
    

提交回复
热议问题