如何使用Java通过Mac OS终端运行命令

huangapple go评论68阅读模式
英文:

How to use Java to run a command through Mac OS Terminal

问题

我是一名高中学生,正在进行一个项目,该项目可以将YouTube链接的视频转换为MP3并下载下来。然而,从YouTube下载和转换视频的方式是通过在Mac OS终端中使用YouTube-dl工具。

以下是我在终端中输入的命令:

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

这个命令在终端中可以正常工作,但是当我尝试使用以下Java代码时:

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

出现错误,提示“没有这个文件或目录”。

我还遇到另一个问题,就是如何从Java中运行输入到终端的命令。我正在使用IntelliJ IDEA开发环境,希望这能有所帮助。谢谢!

英文:

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!

答案1

得分: 1

你在目录路径中有一个空格。尝试像这样加上双引号:

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

还要注意,从 JVM 执行 cd 命令可能不会对当前用户目录产生影响,比如使用 new File("path") 创建文件时。

英文:

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")

答案2

得分: 1

如果cd表示change directory(并且不是可执行文件的名称),那么即使正确执行,它几乎肯定不会生效。由exec()生成的进程将具有工作目录,可以更改 - 但该更改只会影响生成的进程。

此外,将参数中的空格传递给exec()本质上是有问题的。exec() _不是_一个shell,你不能使用像引号这样的shell机制来保护字符串免受在空格处拆分。相反,你需要自己将命令拆分为参数,知道拆分应该发生的位置,然后使用接受String[]作为输入的exec()形式。换句话说,自己将参数拆分为字符串数组,而不依赖于exec()来为你(错误地)执行此操作。

Runtime.exec()充满了困难,并且需要非常小心地处理。我在这里详细介绍了这个主题:

http://kevinboone.me/exec.html

英文:

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

huangapple
  • 本文由 发表于 2020年9月23日 09:53:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64019918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定