4

I'm trying to execute a bash script from Java and it returns error /bin/bash: '/home/nika/NetBeansProjects/Parallel Framework/process-executor.sh': No such file or directory, I'm working on ubuntu 14.04 with netbeans8 & jdk8.

Here is my code:

public class Process {
public static void main(String[] args) {
        try {
            ProcessBuilder pb = null;
            Process p;
            String cmd2 = "";
            String workingDir = System.getProperty("user.dir");
            System.out.println(""+workingDir);
            String scriptloc="'"+workingDir+"/process-executor.sh'";
            String cmd[] = {"/bin/bash",scriptloc , "workspace/ForDemo.java", "ForDemo.java", "ForDemo"};

            for (int i = 0; i <= cmd.length-1; i++) {
                cmd2 += " "+cmd[i];
            }
            System.out.println("" + cmd2);
            pb = new ProcessBuilder(cmd);
            pb.directory(new File(workingDir));

            p = null;
            try {
                p = pb.start();
            } catch (IOException ex) {
                Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
            }

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");

            String s = null;
            String output = "";
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);


            }
            output = "";

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (IOException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        }
}
}

But when I execute this command from terminal it executes the script bin/bash '/home/nika/NetBeansProjects/Parallel Framework/process-executor.sh' workspace/ForDemo.java ForDemo.java ForDemo

I have another problem with my script it doesn't execute the cd command and says '/home/nika/NetBeansProjects/Parallel Framework/workspace/ForDemo.java/': No such file or directory

Contents of my script are

#!/bin/bash 

 PATH=/bin:/usr/bin:/usr/local/bin
 WORK=${PWD}/workspace/
 echo "'${WORK}${2}'"
 cd  "'${WORK}${2}/'"
 javac $2 
 java $3 
 echo "$3"

My directory hierarchy is like

-Parallel Framework
-- process-executor.sh
-- workspace
--- ForDemo.java (directory)
---- ForDemo.java

1 Answer 1

5

Don't use single quotes in the path to your script in this case, i. e. fix your scriptloc variable like this:

String scriptloc= workingDir + "/process-executor.sh";

Single quotes are necessary if you were executing this in the command line (to escape the space character in your path) but it is not necessary in this case as you are already specifying implicitly in your cmd[] array that such path is just one "unit"

Sign up to request clarification or add additional context in comments.

3 Comments

It executes the script but now i'm getting this error. /home/nika/NetBeansProjects/Parallel Framework/process-executor.sh: line 6: cd: '/home/nika/NetBeansProjects/Parallel Framework/workspace/ForDemo.java/': No such file or directory javac: file not found: ForDemo.java Usage: javac <options> <source files> use -help for a list of possible options Error: Could not find or load main class ForDemo
Sorry, I didn't see you had already put the source of your script, indeed, change this line: cd "'${WORK}/'" for this one: cd "'${WORK}'" and see what happens
@DeepSidhu1313 sorry, I edited so many times my last comment that now I'm not sure if you got my final comment, Did you get it working now?

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.