java runtime.exec cmd / c解析引用的参数 [英] java runtime.exec cmd /c parsing quoted arguments
问题描述
我试图运行
runtime.exec(String [],null,new File(目录))
,前两个参数是cmd 和/ c。
我试图指定我的tomcat运行的java版本。看来,cmd / c参数使得runtime.exec通过空间描述来解析所有的参数,或者更适当地cmd解析每个参数。
cmd / c .\bin\Tomcat7.exe // US // Tomcat7 --Jvm =C: \\ Program Files \Apache Tomcat 7 \jre\bin\server\jvm.dll
正在将jvm参数分解成参数C:\Program,Files\Apache,Tomcat .....这导致无法解释参数。引用参数似乎是
有没有办法让引用jvm参数由cmd尊重或利用指定的目录在runtime.exec?
感谢您阅读。一个有效的解决方法是使用windows短名称:
使用 PROGRA〜1
替换程序文件
DIR / XC:\ | findProgram
)查看是否是正确的名称
code> Apache Tomcat 7 由 APACHE〜1
(或由 DIR / XC:\ PROGRA〜1 |找到Apache
)
您的命令很可能是
cmd / c .\bin\Tomcat7.exe // US // Tomcat7 --Jvm = C:\PROGRA〜1 \APACHE〜1 \jre\bin\ server\jvm.dll
没有更多空格:问题解决
编辑:
未经验证,但您可能想尝试此类型的exec:
public Process exec(String command,String [] envp,File dir);
如下:
exec(cmd / c .\bin\Tomcat7.exe // US // Tomcat7 --Jvm = \C:\Program Files\Apache Tomcat 7 \jre\ bin \server\jvm.dll\,null,new File(directory))
您当前使用的API必须从您的参数重建完整的命令行,这不是真正的参数,而是具有空格,引号等的参数组。可能(未选中)API尝试在不需要的地方添加引号,从而破坏您的命令行。
I'm trying to run
runtime.exec(String[],null, new File(directory))
with the first two arguments being "cmd" and "/c".
I'm trying to specify the version of java for my tomcat to run. It appears that the cmd /c arguments are causing runtime.exec to parse all of the arguments by space delineation or probably more appropriately cmd is parsing each argument out.
So,
cmd /c .\bin\Tomcat7.exe //US//Tomcat7 --Jvm="C:\Program Files\Apache Tomcat 7\jre\bin\server\jvm.dll"
is getting the jvm argument broken into arguments "C:\Program", "Files\Apache","Tomcat..... which is causing to not be able to interpret the arguments. Quoting the arguments appears to being ignored as well.
Is there a way to either make quoting the jvm argument be respected by cmd or to utilize the specified directory in the runtime.exec?
Thanks for reading.
an efficient workaround is to use windows short names:
replace Program Files
by PROGRA~1
(use DIR /X C:\ | find "Program"
) to see if it is the correct name
replace Apache Tomcat 7
by APACHE~1
(or whatever is returned by DIR /X C:\PROGRA~1 | find "Apache"
)
Your command is very likely to be
cmd /c .\bin\Tomcat7.exe //US//Tomcat7 --Jvm=C:\PROGRA~1\APACHE~1\jre\bin\server\jvm.dll
no more spaces: problem solved
EDIT:
unverified, but you may want to try this flavour of exec instead:
public Process exec(String command, String[] envp, File dir);
like this:
exec("cmd /c .\bin\Tomcat7.exe //US//Tomcat7 --Jvm=\"C:\Program Files\Apache Tomcat 7\jre\bin\server\jvm.dll\"",null,new File(directory))
The API you're currently using has to rebuild the full command line from your parameters, which are not really parameters, but rather groups of parameters with spaces, quotes, etc... It is possible (unchecked) that the API tries to add quotes where it's not needed, thus corrupting your command line.
这篇关于java runtime.exec cmd / c解析引用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!