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
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"
3 Comments
/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 ForDemocd "'${WORK}/'" for this one: cd "'${WORK}'" and see what happens