如何在Java中使用命令提示符中的空格访问文件路径

时间:2014-11-25 14:30:47

标签: java command-line apache-commons-exec

我正在尝试从Java程序运行批处理文件。 例如:我有一批" abc.bat"在" Program Files"。

中的文件夹中

我想从我的Java程序中执行这个批处理。我正在使用CommandLine类,Commons-exec jar。

CommandLine cmdLine = CommandLine.parse("cmd");
cmdLine.addArgument("/c start \"\" \"C:\\Program Files\\abc.bat\"");

DefaultExecutor exec = new DefaultExecutor();
Process p = Runtime.getRuntime().exec(cmdLine.toString());
exec.execute(cmdLine);

上面的代码抛出错误说" Windows无法找到该文件。确保正确输入名称,然后重试#34;。而且,这是因为路径中的空间。

所以,我尝试了@ brso05提供的答案,这是有效的。但我希望它能成为未来的阶级。请在下面找到我的代码并帮我解决。

final CommandLine cmdLine = CommandLine.parse("cmd.exe");
cmdLine.addArgument("/c");
cmdLine.addArgument("start");
cmdLine.addArgument("\""+ batchFileExecute.getParent().toString() + "\"");

ExecutorService es = Executors.newFixedThreadPool(1);
Future<?> future = es.submit(new Runnable() {
        public void run() {
                DefaultExecutor exec = new DefaultExecutor();
                        try {
                            Process p = Runtime.getRuntime().exec(cmdLine.toString());
                            exec.execute(cmdLine);
                            System.out.println(p.waitFor());
                            }
                        catch (IOException e) 
                            {
                                e.printStackTrace();
                            }
                        catch (InterruptedException e)
                            {
                                e.printStackTrace();
                            }
                        }
                        });
                            String thread_status = null;
                            try 
                            {
                                thread_status = future.get().toString(); 
                                System.out.println(thread_status+" completed the execution");
                            } 
                            catch (NullPointerException e) 
                            {
                            System.out.println("The execution of the received project is     complete.");                   
// In here I want to do some processing again.
}

我提到的代码有效但如果我的批处理文件在路径中有空格则它不起作用。你能帮我解决这个问题吗?

因为你已经给出了作品的片段但是我不能把它放到未来。它不能以理想的方式工作。

提前致谢!

3 个答案:

答案 0 :(得分:1)

这是一种替代方式:

     Runtime rt = Runtime.getRuntime();
     rt.exec("cmd.exe /c start \"\" \"C:\\Program Files\\abc.bat\"");

答案 1 :(得分:0)

你试过单引号吗?根据{{​​3}},它应该有效。

答案 2 :(得分:0)

使用ImageMagick时,我有相同的文件名和空格问题。以下是解决问题的代码:

String imageOutput = null;
ByteArrayOutputStream identifyStdout = new ByteArrayOutputStream();
ByteArrayOutputStream identifyStderr = new ByteArrayOutputStream();

try
{
    DefaultExecutor identifyExecutor = new DefaultExecutor();
    // End the process if it exceeds 60 seconds
    ExecuteWatchdog identifyWatchdog = new ExecuteWatchdog(60000);
    identifyExecutor.setWatchdog(identifyWatchdog);


    PumpStreamHandler identifyPsh = new PumpStreamHandler(identifyStdout, identifyStderr);
    identifyExecutor.setStreamHandler(identifyPsh);
    identifyExecutor.setExitValue(0); //0 is success

    CommandLine identifyCommandline = new CommandLine("identify");
    identifyCommandline.addArgument(destFile.getAbsolutePath(), false);

    DefaultExecuteResultHandler identifyResultHandler = new DefaultExecuteResultHandler();

    identifyExecutor.execute(identifyCommandline, identifyResultHandler);
    identifyResultHandler.waitFor();

    if (identifyResultHandler.getExitValue() != 0)
    {
        String output = identifyStdout.toString();
        _logger.debug("Standard Out = " + output);
        _logger.debug("Standard Err = " + identifyStderr.toString());

        String msg = "ImageMagick overlay encountered an error. ImageMagick returned a value of " + identifyResultHandler.getExitValue();
        throw new Exception(msg);
    }
    else
    {
        imageOutput = identifyStdout.toString();
        _logger.debug("Standard Out = " + imageOutput);
        identifyStdout.close();
        identifyStderr.close();
    }
}
catch(Exception e)
{
    _logger.debug("Error: " + e.getLocalizedMessage(), e);
}
finally
{
    identifyStdout.close();
    identifyStderr.close();
}

这里的重要部分是:

identifyCommandline.addArgument(destFile.getAbsolutePath(), false);

此行允许带有空格的文件路径正确处理。

相关问题