7

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.

5
  • 1
    java's trying to run your ENTIRE command line as a single command. The string cat /home/... is not a program, cat is, and the rest is an argument for cat. Commented Sep 3, 2014 at 15:19
  • 1
    Marc B is probably right, and I'm not a java guy, so what I would probably do is to check the permissions for /home/kemallin/Desktop/capture-03.csv and /home/kemallin/Desktop/clean.sh. Make sure the user running the java program have access to those files (a+rwx if you just want to discard a permissions problem... if it is, then you can work from there) Commented Sep 3, 2014 at 15:27
  • I think your error output does not match the java code shown. In the Java code the command to run should be "/home/kemallin/Desktop/clean.sh" but in your error output shown appears "cat /home/kemallin/Desktop/capture-03.csv | awk..." and "/home/kemallin/Desktop/clean.csv" :( Commented Sep 3, 2014 at 15:42
  • @MarcB so what do you suggest? That I should break down the shell script? Commented Sep 4, 2014 at 8:22
  • @jim clean.sh have 755, user running the java program owns the clean.sh so accessing shouldn't be a problem is that correct? Commented Sep 4, 2014 at 8:22

2 Answers 2

15

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();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That fixed some part of the problem. Thank you I updated the question accordingly.
My mistake this fixed it entirely.
1

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

1 Comment

It still says "no such file or directory".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.