命令的 java run.exec 和 shell 执行有什么区别吗?

发布于 2024-08-11 07:25:28 字数 411 浏览 5 评论 0原文

我有一个调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情妓 2024-08-18 07:25:28

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.

梦里寻她 2024-08-18 07:25:28

当您位于 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]

对岸观火 2024-08-18 07:25:28

来自: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

Runtime.exec() 不是命令行

最后一个需要解决的陷阱
Runtime.exec() 错误地假设
exec() 接受任何字符串
你的命令行(或shell)接受。
Runtime.exec() 受到更多限制
并且不能跨平台。这个陷阱
是由用户尝试使用引起的
exec() 方法接受单个
字符串就像命令行一样。这
混乱可能是由于以下事实:
命令是参数名称
exec() 方法。于是,程序员
错误关联参数
指挥他或她所做的任何事情
可以在命令行上输入,而不是
将其与单个程序关联
及其论据。

From: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

Runtime.exec() is not a command line

One final pitfall to cover with
Runtime.exec() is mistakenly assuming
that exec() accepts any String that
your command line (or shell) accepts.
Runtime.exec() is much more limited
and not cross-platform. This pitfall
is caused by users attempting to use
the exec() method to accept a single
String as a command line would. The
confusion may be due to the fact that
command is the parameter name for the
exec() method. Thus, the programmer
incorrectly associates the parameter
command with anything that he or she
can type on a command line, instead of
associating it with a single program
and its arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文