简体   繁体   English

使用java中的空格运行cmd

[英]Running cmd with spaces from java

I want to run a command through java session. 我想通过java session运行命令。 The command contains spaces. 该命令包含空格。 as "C:\\With Space\\sample.exe" -command_option "C:\\Source File\\test.c" This works if as "C:\\With Space\\sample.exe" -command_option "C:\\Source File\\test.c"

C:\WithoutSpace\sample.exe -command_option "C:\Source File\test.c"

if we keep the quotes in C:\\With Space\\sample.exe we get error as :'The filename, directory name, or volume label syntax is incorrect.' 如果我们将引号保存在C:\\With Space\\sample.exe我们会得到错误:'文件名,目录名或卷标语法不正确。' and if we remove the quotes then the exe do not run... please guide. 如果我们删除引号然后exe不运行...请指导。

Thanks, 谢谢,

Try this: 尝试这个:

String[] arg = {"cmd","/c","C:/Source File/test.c"};
ProcessBuilder pb = new ProcessBuilder(arg);
Process pr = pb.start();   

Also, you can use Runtime.exec(String[]) version 此外,您可以使用Runtime.exec(String[])版本

Example: 例:

Runtime rt = Runtime.getRuntime();
String[] args = { "cmd", "/c", "C:/Source File/test.c"};
try
{
    Process proc = rt.exec(processCommand);
}

You can try this: 你可以试试这个:

Process process = Runtime.getRuntime().exec("'C:\WithoutSpace\sample.exe' -command_option 'C:\Source File\test.c'");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

while ((line = in.readLine()) != null) {
    //line contain command output
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM