我在春季启动中运行shell命令,但process = Runtime.getRuntime()。exec(cmd)不运行

时间:2018-11-17 09:52:16

标签: java spring spring-boot centos7 runtime.exec

我在Spring Boot和Java中运行以下代码,但是process = Runtime.getRuntime()。exec(cmd)不在Spring Boot应用程序中运行。在纯Java应用程序中可以。在春季启动中,它不会报告异常或错误。你能帮我吗?

public String convetor(String videoInputPath,String basepath) {
    File output = new File("/root/output.txt");
    File error = new File("/root/error.txt");
    File tmp = new File(videoInputPath);
    String des = tmp.getName();
    String[] filename = des.split("\\.");
    String dest = basepath+filename[0] + ".ts";

    System.out.println(videoInputPath);
    List<String> command = new ArrayList<String>();
    command.add("ffmpeg");
    command.add("-i");
    command.add(videoInputPath);
    command.add("-vcodec");
    command.add("copy");
    command.add("-vbsf");
    command.add("h264_mp4toannexb");
    command.add(dest);
    ProcessBuilder builder = new ProcessBuilder(command);
    Process process = null;
    String cmd = "ffmpeg -i " + videoInputPath + "-vcodec copy -vbsf h264_mp4toannexb "+dest;
    try {
        System.out.println("==============");
        builder.redirectOutput(output);
        builder.redirectError(error);
//            process = builder.start();
        process = Runtime.getRuntime().exec(cmd);
        System.out.println("++++++++++++");
        InputStreamReader isr = new  InputStreamReader(process.getInputStream());
        BufferedReader br = new BufferedReader(isr);

        String lineRead;
        while ((lineRead = br.readLine()) != null) {

        }
        process.waitFor();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    catch (InterruptedException e){
        e.printStackTrace();
    }
    return dest;

}`

0 个答案:

没有答案