0

I am a high school student working on a project that will convert the video from a YouTube link into an MP3 and download it. However, the way that the video form YouTube is downloaded and converted is through the Mac OS terminal by using YouTube-dl.

This is what I put into the terminal:

youtube-dl -f bestvideo+bestaudio \"ytsearch:{https://www.youtube.com/watch?v=5X-Mrc2l1d0}\"

This works in terminal, but when I try to use:

Runtime.getRuntime().exec("cd /Users/poppa/Desktop/IA Vids");

and there's an error saying "No such file or directory"

Another Problem that I am having is running the code that is inputted into the Terminal from Java. I'm using IntelliJ IDEA if that helps. Thank You!

Zeid Akel
  • 1
  • 1
  • It depends on where you run the `youtube-dl` application. If you run from the terminal, probably it executes from its original root file path path folder (where it is actually installed) or with the help of the `PATH` environment variable which includes the installed root file path to find the `youtube-dl` application. With Java application, you may need to concat this installed root file path with the `youtube-dl` application filename. About the command `"cd /Users/zeidakel/Desktop/IA Vids"`, maybe you need to check for the path first with Java `Files.exists()`? – ecle Sep 23 '20 at 03:18
  • Refer [How to use “cd” command using Java runtime?](https://stackoverflow.com/questions/4884681/how-to-use-cd-command-using-java-runtime) – ecle Sep 23 '20 at 03:27

2 Answers2

1

You have a space in the directory path. Try putting double quotes around like this:

Runtime.getRuntime().exec("cd \"/Users/zeidakel/Desktop/IA Vids\"");

Also note that executing cd command from JVM may have no effect on current user dir when (for example) creating files with new File("path")

DelfikPro
  • 729
  • 4
  • 10
  • 1
    With all due respect, putting quotes around the filename almost certainly won't help. The tokenization that `exec()` does is not smart enough to parse the quotes. You'll end up with three arguments to the shell -- the second beginning with a quote, and the third ending with one. – Kevin Boone Sep 23 '20 at 08:30
  • 1
    That is, if you want to `exec()` a command with multiple arguments, it's far safer to split the command and arguments yourself, and use the variant of `exec()` that takes a string array. – Kevin Boone Sep 23 '20 at 08:38
1

If cd means change directory (and isn't the name of an executable), then it almost certainly won't take effect, even if it is executed correctly. The process spawned by exec() will have a working directory, and it can be changed -- but that change will only affect the spawned process.

In addition, having spaces in arguments to exec() is inherently problematic. exec() is not a shell, and you won't be able to protect the string from being split at the spaces by using shell mechanisms like quotes. Instead, you need to split the command into arguments yourself, knowing where the splits should be, and then use the form of exec() that takes a String[] as input. That is, split the arguments into an array of strings yourself, rather than relying on exec() to do it for you (wrongly).

Runtime.exec() is fraught with difficulties, and needs very careful handling. I've written extensively about this subject here:

http://kevinboone.me/exec.html

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15