0

I started a project in Processing and then found that I needed more functionality. I had the following function that worked fine in Processing but now in the java environment I am getting an error.

Function:

void camSummary() {
        System.out.format("Cam summary");
          String commandToRun = "./camSummary.sh"; 
          File workingDir = new File(main.path);
          try {

            Process p = Runtime.getRuntime().exec(commandToRun, null, workingDir);
            int i = p.waitFor();
            if (i==0) {
              BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
              while ( (returnedValues = stdInput.readLine ()) != null) {
                  System.out.format(returnedValues);
              }
            }
          }
          catch(Throwable t) {
              System.out.format("error: " + t + "\n");  
          }
        }

Error:

Cannot run program "camSummary.sh" (in directory "/Users/lorenzimmer/Documents/RC/CamSoft/ "): error=2, No such file or directory

I've found that there have been some very slight differences from processing to java. I'm wondering if this function just needs to be tweaked slightly to run properly.

Any help would be greatly appreciated.

Loren

5
  • Does the file /Users/lorenzimmer/Documents/RC/CamSoft/camSummary.sh actually exist? Commented Aug 10, 2015 at 15:04
  • @heenenee yes. In fact I just ran the processing version and it worked like I thought it should. Commented Aug 10, 2015 at 15:05
  • @LorenZimmer, java doesn't look for the file in path denoted by the workingDir. It actually looks for the script in the working directory of the java application itself. It just executes the script from the workingDir path. So put your script relative to the working directory of the java application. Commented Aug 10, 2015 at 15:08
  • @Codebender just tried that too. I got the same error Commented Aug 10, 2015 at 15:12
  • Hum... Mine is just a shot of fortune, because I'm not quite good at Unix, but I suspect of the period + slash at the beginning of commandToRun. Already tried to remove them? Commented Aug 10, 2015 at 20:41

1 Answer 1

0

Runtime.exec does not invoke a shell, so you have to explicitly invoke one (bash, sh, etc)

Try this:

Change String commandToRun = "./camSummary.sh";

to

String[] commandToRun = {"bash", "-c", "/path/to/camSummary.sh"};

or

String[] commandToRun = {"sh", "/path/to/camSummary.sh"};

Then change

Process p = Runtime.getRuntime().exec(commandToRun, null, workingDir);

to

Process p = Runtime.getRuntime().exec(commandToRun);

In one line: Process p = Runtime.getRuntime().exec(new String[] {"bash", "-c", "/path/to/script"});

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

Comments

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.