命令的 java run.exec 和 shell 执行有什么区别吗?
我有一个调用 shell 命令的程序。当我使用java的run.exec执行命令时,它不起作用,但是当我直接在终端中执行命令时,它就像魅力一样工作。
ex: pdf2swf "3bbba47.pdf" -T 9 -o "3bbba47.swf" didnt worked
from java program but worked directly executing it in terminal.
但是当我尝试从命令中删除引号时,
pdf2swf 3bbba47.pdf -T 9 -o 3bbba47.swf
它在 run.exec 和终端中都运行良好。
为什么会这样呢?
我在 mac 和 ubuntu 上都尝试过,结果相同。
I have a program which calls a shell command. When I executes the command using java's run.exec, it is not working but when I executes the command directly in terminal, it works like charm.
ex: pdf2swf "3bbba47.pdf" -T 9 -o "3bbba47.swf" didnt worked
from java program but worked directly executing it in terminal.
But when I tried removing the quotes from the command
pdf2swf 3bbba47.pdf -T 9 -o 3bbba47.swf
It worked fine in both run.exec and terminal.
Why is it so?
I tried in both mac and ubuntu and ended with same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
run.exec() 不调用 shell。 shell 解析命令行并有效地删除引号,然后将其作为参数传递给 pdf2swf。您只能使用 run.exec() 运行“原始”命令。
如果需要,您可以使用 run.exec() 运行 shell,并将其解析为 shell 命令。引用会有点痛苦,但可行。
run.exec() does not invoke the shell. The shell parses the command line and effectively removes the quotes before passing them as arguments to pdf2swf. You can only run "raw" commands with run.exec().
You can, if you want, run the shell with run.exec(), and have it parse your command as a shell command. Quoting will be a bit painful, but doable.
当您位于 shell 中时,引号字符会在 shell 将其提供给 JVM 之前进行解释。
当您在 run.exec 中时,引号被视为命令的一部分,因此 JVM 认为您要求的是 ["3bbba47.pdf"] 而不是 [3bbba47.pdf]
When you are in the shell, the quote character is interpreted before the shell give it to the JVM.
When you are in run.exec, the quotes are considered part of the command, so JVM believes that you ask for ["3bbba47.pdf"] instead of [3bbba47.pdf]
来自:http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1" javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
From: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1