Skip to content

I’m running a process in Java and am getting stuck when I wait for it to finish

I have a Java program that is supposed to make copies of segments of a video and then stitch them back together, using ffmpeg. My “snip” method, the one that makes the segment files, has a problem, it gets stuck when I call “process.waitfor()”. When I take it out, the videos load partly, but cannot be accessed until I close the program. When I try to delete them, while the program is running, it says that they cannot be deleted because they are in use. Could anyone lead me in the right direction? Here is the method:

//snips out all the clips from the main video
public void snip() throws IOException, InterruptedException {
    for(int i = 0; i < snippets.size(); i++) {
        //Future reference:
        //Example: ffmpeg -i 20sec.mp4 -ss 0:0:1 -to 0:0:5 -c copy foobar.mp4
        String newFile = "foobar" + String.valueOf(i) + ".mp4";
        ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-i", videoName, "-ss",
                snippets.get(i).getStartTime(), "-to", snippets.get(i).getEndTime(), newFile);
        //I tried this first and then added in the process/process.waitfor below
        Process process = processBuilder.start();
        System.out.println("Snip " + i + "n");
        //add to the formatted list of files to be concat later
        if(i == snippets.size() - 1) {
            stitchFiles += newFile + """;
        else {
            stitchFiles += newFile + "|";



Programs often produce log or error output which has to go somewhere. By default Java sets up “pipes” for these, which allow you to read the produced output from Java. The downside is, pipes have a limited capacity, and if you don’t read from them, the external program will eventually get blocked when it tries to write more output.

If you’re not interested in capturing the log output, you can for example let ffmpeg inherit the Java application’s I/O streams:

Process process = processBuilder.inheritIO().start();
1 People found this is helpful