I am facing an issue when it comes to my Java application. The app downloads Music from Youtube using yt-dlp, it is basically a GUI version of yt-dlp. The issue I have is only present on MacOs, it works as intended on Windows. Here is a snippet of my download method code (This is a MacOs specific method, as I have already mentioned before I only encounter the issue on MacOs not on Windows, thus I am not providing any code specific for Windows):
The download method:
private void download() {
Process process = null;
try {
ProcessBuilder processBuilder;
String command =
"'Project/src/Zasoby/yt-dlp'" + " -f bestaudio " +
"-x --audio-format " + chosenFormat
+ " --no-playlist --output '" + outputPath + "/%(title)s.%(ext)s ''"
+ linkYT + "'";
processBuilder = new ProcessBuilder("bash", "-c", command);
processBuilder.inheritIO();
process = processBuilder.start();
process.waitFor();
int exitCode = process.exitValue();
System.out.println(exitCode + " EXIT CODE"); // a log for exit codes
if (exitCode != 0) {
InfoFrame ef = new InfoFrame(2);
ef.setVisible(true);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
The problem is that whenever I try to execute a command using the ProcessBuilder when not in my IDE (in my IDE it works just fine - the path 'Project/src/Zasoby/yt-dlp' is recognized and the program stars downloading Music), instead while executing the program via a .jar File, the program cannot find the yt-dlp program via its provided path.
So far I have tried providing Paths using a getFilePath method I wrote in order to make sure I do not need to specify a "semi-full" path which obviously would not work on a different MacOs computer.
The getFilePath method:
private Path getFilePath(String resourcePath) {
try {
InputStream inputStream = getResourceAsStream(resourcePath);
Path tempFilePath = Files.createTempFile("temp", null);
Files.copy(inputStream, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
return tempFilePath.toAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("Failed to get resource file path: " + resourcePath, e);
}
}
The modified command String in the download method:
Path ytDlpPathMac = getFilePath("Zasoby/yt-dlp");
String command =
ytDlpPathMac.toString() + " -f bestaudio " +
"-x --audio-format " + chosenFormat
+ " --no-playlist --output '" + outputPath + "/%(title)s.%(ext)s ''"
+ linkYT + "'";
Additionally the path "Zasoby/yt-dlp" is a "Path From Content Root", the full Path in the project is"Project/src/Zasoby/yt-dlp" - if that helps.I have already tried other solutions from other Questions on the forum, however none of them fixed the issue.
I have tried debugging the code using Xdebug tool on MacOs and the output I am getting is that ".. directory does not exist" - when using both of the approaches, the one with the "semi-full" path and the one with the getFilePath method. As I have previously mentioned this only happens on Mac, so maybe there is a different way of handling Paths on MacOs than on Windows when it comes to Java itself.