简体   繁体   中英

Java Execute a bash script using Java process builder

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

Don't use single quotes in the path to your script in this case, ie 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM