1

trying to execute an script, using this piece of code:

String command = "./myScript.sh";
pb = new ProcessBuilder(command, param1, param2);
pb.directory(directory);
pb.start();

I am not getting any kind of error, but neither the supposed results. Anyway, I tryed to run the same command, direclty in the terminal, and everything working correctly.
Am I missing something??

Thanks in advance

asked Nov 19, 2010 at 13:59
3
  • 1
    p = pb.start(); p.waitFor(); System.err.println("exit with " + p.exitValue()); What does it print? Commented Nov 19, 2010 at 14:39
  • what dataType is p?? I tryed System.out.println(pb.start().exitValue()); and no output to display Commented Nov 19, 2010 at 16:03
  • Are you sure myScript.sh is executable? Commented Nov 19, 2010 at 16:26

2 Answers 2

4

When you start a process (pb.start()) you get back a Process instance. If your script reads input or writes output to stdout or stderr you need to handle this on separate threads using Process.getInputStream(), ...getOutputStream() and getErrorStream(). If you don't do this the process can hang. You also should call Process.waitFor() and then Process.exitValue() to get the return status of the process. If it's a negative number then the system was unable to launch your script.

EDIT: Here is a short simplified example. This is a toy only and will work reliably ONLY under the following conditions:

  1. The script does not require any input

  2. The script does not produce a large amount of output on both stdout and stderr. If it does, then since the program reads all of stdout before stderr, the stderr buffer may fill up and block the process from completing. In a 'real' implementation you would read stdout and stderr in separate threads (hint, wrap the loadStream() method in a class that implements Runnable).

public class PBTest
{
 public static void main(String[] args) throws Exception
 {
 ProcessBuilder pb = new ProcessBuilder("sc","query","wuauserv");
 Process p = pb.start();
 String output = loadStream(p.getInputStream());
 String error = loadStream(p.getErrorStream());
 int rc = p.waitFor();
 System.out.println("Process ended with rc=" + rc);
 System.out.println("\nStandard Output:\n");
 System.out.println(output);
 System.out.println("\nStandard Error:\n");
 System.out.println(error);
 }
 private static String loadStream(InputStream s) throws Exception
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(s));
 StringBuilder sb = new StringBuilder();
 String line;
 while((line=br.readLine()) != null)
 sb.append(line).append("\n");
 return sb.toString();
 }
}
answered Nov 19, 2010 at 21:24
Sign up to request clarification or add additional context in comments.

3 Comments

could you please give me an example how to use these Process.getInputStream(), ...getOutputStream() ??? Process.exitValue() gives me a possitive value, but my script is not running. I tryed: pb.directory(directory); pb.start(); Process p = pb.start(); p.getInputStream(); p.getOutputStream(); int i= p.exitValue();
Thank you, now I can see the error: ./script.sh: line 11: ffmpeg: command not found But I don't understand, because this command is installed, if I execute this the script directly in console, it works.
if it is a program you have installed, you should not use ./ffmpeg but simply ffmpeg. the . suffix means it only looks for the command at the working directory
2

The problem was not on the way I called the script, which was right.
But it was inside the script. At first it was:

#!/bin/bash
inputFolder=1ドル
outputFolder=2ドル 
cd $inputFolder
for file in `ls ` ; do
ffmpeg -i $inputFolder/$file -ar 22050 $outputFolder/$file.mp4 
done

But I got ffmpeg command not found, so I changed it to:

#!/bin/bash
inputFolder=1ドル
outputFolder=2ドル 
cd $inputFolder
for file in `ls ` ; do
/usr/local/bin/ffmpeg -i $inputFolder/$file -ar 22050 $outputFolder/$file.mp4 
done 

with the hole path. But I have still doubts, why this is necessary, if I have ffmpeg in my path and I cand execute in console direclty form any directory?? If someone can give me an answer, it will be welcome :)

answered Nov 23, 2010 at 10:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.