Executable name has embedded quote, split the arguments

將JDK升級到7u21後,一個應用的內置Tomcat啓動不了,報出 Executable name has embedded quote, split the arguments 的錯誤。

經過查詢發現 jdk 7u21 和 jdk 6u 45的改變了Runtime.exec方法實現。對含有空格的命令會有影響。


Changes to Runtime.exec

On Windows platform, the decoding of command strings specified to Runtime.exec(String), Runtime.exec(String,String[]) and Runtime.exec(String,String[],File) methods, has been improved to follow the specification more closely. This may cause problems for applications that are using one or more of these methods with commands that contain spaces in the program name, or are invoking these methods with commands that are not quoted correctly.

For example, Runtime.getRuntime().exec("C:\\My Programs\\foo.exe bar") is an attempt to launch the program "C:\\My" with the arguments "Programs\\foo.exe" and "bar". This command is likely to fail with an exception to indicate "C:\My" cannot be found.

The example Runtime.getRuntime().exec("\"C:\\My Programs\\foo.exe\" bar") is an attempt to launch the program "\"C:\\My". This command will fail with an exception to indicate the program has an embedded quote.

Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.

Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.


參考資料

[1] Java™ SE Development Kit 6, Update 45 (JDK 6u45) Update Release Notes. http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html
[2] Java™ SE Development Kit 7, Update 21 (JDK 7u21) Update Release Notes.  http://www.oracle.com/technetwork/java/javase/7u21-relnotes-1932873.html
[3] exec problem is JDK 1.7.0_21. http://www.velocityreviews.com/forums/t959814-exec-problem-is-jdk-1-7-0_21-a.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章