Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

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

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 scriptPath = "/home/kemallin/Desktop/";

public void cleanCSVScript() {

    String script = "clean.sh";
    try {
        Process awk = new ProcessBuilder(scriptPath + script).start();
        awk.waitFor();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

and i get this error:

java.io.IOException: Cannot run program "cat /home/kemallin/Desktop/capture-03.csv | awk -F ',' '{ print $1,",", $2,",", $3,",", $4,",", $6}' > clean.csv": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at ShellScript.cleanCSVScript(ShellScript.java:21)
at Main.main(Main.java:15)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:186)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 2 more
java.io.FileNotFoundException: /home/kemallin/Desktop/clean.csv (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at CSVReader.run(CSVReader.java:25)
at Main.main(Main.java:17)

I have googled it and every solution pretty much indicate that i'm doing the right thing.

I have tried to put the script file in the src and in bin of the Java project but still it says no such file or dir.

What am i doing wrong?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...