Java Execute a bash script using Java process buil

2019-07-04 04:48发布

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条回答
Explosion°爆炸
2楼-- · 2019-07-04 05:28

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"

查看更多
登录 后发表回答