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 中都试过,结果相同。

4

3 回答 3

2

run.exec() 不调用 shell。shell 解析命令行并在将引号作为参数传递给 pdf2swf 之前有效地删除它们。您只能使用 run.exec() 运行“原始”命令。

如果需要,您可以使用 run.exec() 运行shell,并让它将您的命令解析为 shell 命令。引用会有点痛苦,但可行。

于 2009-11-14T07:36:58.077 回答
1

当您在 shell 中时,引号字符在 shell 将其提供给 JVM 之前被解释。

当您在 run.exec 中时,引号被视为命令的一部分,因此 JVM 认为您要求的是 ["3bbba47.pdf"] 而不是 [3bbba47.pdf]

于 2009-11-14T07:39:57.790 回答
0

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.

于 2009-11-14T08:47:49.667 回答