I have a segment of code something like this
File f = new File("Audio/Testing/mbgitr.wav");
try {
ProcessBuilder pb = new ProcessBuilder("bash/extractAudio.sh", f.getAbsolutePath());
pb.inheritIO();
pb.start();
}catch(IOException e){
System.out.println("Oh no!");
}
System.out.println(new File("Audio/mbgitr.wav").exists());
The bash file converts the audio file to a different format/sample rate and outputs it to the folder Audio. But whenever I run this script, I get the following output:
/home/Ryan/Development/Java/Test/Audio/Testing/mbgitr.wav
mbgitr.wav
/home/Ryan/Development/Java/Test
false
ffmpeg version 2.6.2 Copyright (c) 2000-2015 the FFmpeg developers
[more ffmpeg output]
Process finished with exit code 0
It seems that the line of code testing if the file exists is executing before the code running the script. If I were to run the program again, it would ouput "true" because the file was already created in the last iteration. Is there a way to get the file called by the ProcessBuilder to execute first? Thanks in advance!
ProcessBuilder.start() starts the process as a separate Process and continue to execute. If you need to wait until that process terminates modify the code as shown below. Then you will get the desired output.
File f = new File("Audio/Testing/mbgitr.wav");
try {
ProcessBuilder pb = new ProcessBuilder("bash/extractAudio.sh", f.getAbsolutePath());
pb.inheritIO();
Process p = pb.start(); // Get the process
p.waitFor(); // Wait until it terminates
}catch(IOException e){
System.out.println("Oh no!");
}catch(InterruptedException e){
System.out.println("InterruptedException");
}
System.out.println(new File("Audio/mbgitr.wav").exists());