开发者

Problem ProcessBuilder running script sh

开发者 https://www.devze.com 2023-01-26 03:00 出处:网络
trying to execute an开发者_StackOverflow中文版 script, using this piece of code: String command = \"./myScript.sh\";

trying to execute an开发者_StackOverflow中文版 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


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();
    }
}


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消