使用cmd.exe启动Java工具 [英] Starting a Java tool with cmd.exe
问题描述
我试图在Java自己的cmd.exe窗口中启动一个Java命令行工具。我使用ProcessBuilder启动以下:
cmd.exe / kC:\Program Files(x86)\JavaSoft \jre\1.5.0_07\bin\java.exe
这可以工作。这也工作:
cmd.exe / kC:\Program Files(x86)\JavaSoft\jre\\ \\1.5.0_07\bin\java.exe-version
但是当我尝试运行以下
cmd.exe / kC:\Program Files(x86)\JavaSoft\jre\1.5 .0_07\bin\java.exetest a
找不到C:\Program。我希望从java.exe中得到一个异常。我需要能够在一些参数中使用引号。有没有办法做这个?
从cmd /?:
命令行中的引号字符:
- no / S开关
- 正好两个引号字符
- 在两个引号字符之间,其中特殊的是
之一:&<>()@ ^ | - 在两个引号之间有一个或多个空格字符b $ b个字符
- 两个引号字符之间的字符串是
可执行文件的名称。
字符,如果是这样,剥离前导
字符并删除最后一个引号
字符命令行,
保留最后
引号字符后的任何文本。
好吧,这解释了为什么它不工作,如果有两个以上的报价。但这不解释如何确切地解决问题。幸运的是,Google 有答案:
cmd.exe / kd:\laj soft\java.exetest a
I am trying to start a Java command line tool in its own cmd.exe window from Java. I use ProcessBuilder to start the following:
cmd.exe /k "C:\Program Files (x86)\JavaSoft\jre\1.5.0_07\bin\java.exe"
This works. This also works:
cmd.exe /k "C:\Program Files (x86)\JavaSoft\jre\1.5.0_07\bin\java.exe" -version
But when I try to run the following
cmd.exe /k "C:\Program Files (x86)\JavaSoft\jre\1.5.0_07\bin\java.exe" "test a"
I get an error saying it can't find "C:\Program". I expected to get an exception from java.exe. I need to be able to use quotation marks in some of the arguments. Is there any way to do this?
From "cmd /?":
If all of the following conditions are met, then quote characters on the command line are preserved:
- no /S switch
- exactly two quote characters
- no special characters between the two quote characters, where special is one of: &<>()@^|
- there are one or more whitespace characters between the the two quote characters
- the string between the two quote characters is the name of an executable file.
- Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.
Okay, that explains why it doesn't work if there are more than two quotes. But that doesn't explain how exactly to solve the problem. Thankfully, Google has the answer:
cmd.exe /k ""d:\laj soft\java.exe" "test a""
这篇关于使用cmd.exe启动Java工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!