2014-11-25 49 views
1

我試圖從Java程序運行批處理文件。 例如:我在「Program Files」文件夾中有一個批處理「abc.bat」。如何使用Java中的命令提示符訪問文件路徑

我想從我的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無法找到該文件。請確保您正確,鍵入名稱,然後再試一次」。而且,這是因爲路徑中的空間。

所以,我嘗試了@ 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. 
} 

我提到的代碼工作,但它不工作,如果我的批處理文件在路徑中有空格。你能幫我解決這個問題嗎?

因爲你已經給出了作品的片段,但後來我不能把它放到未來。它不以所需的方式工作。

在此先感謝!

+1

它應該工作,你確定該文件在那裏? – brso05 2014-11-25 14:32:30

+0

是的..我試過..和它不工作..我試圖通過命令提示符從Java運行批處理.. – user1395264 2014-11-25 14:52:30

+0

試試我的回答我認爲它會爲你工作... – brso05 2014-11-25 14:58:13

回答

0

你用單引號試過了嗎?根據this,它應該工作。

+0

單引號??在您提到的鏈接中..只使用雙引號.. – user1395264 2014-11-25 14:53:33

+0

請參見此部分,在提供的鏈接中: 「由於文件名中有空格,我們必須用單引號或雙引號引用文件名」 – jmm 2014-11-25 15:04:41

1

這是另一種方法:

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

this只是給我一個路徑在我的控制檯「D:\ Execution \ Java>」,我認爲這是我的cmd提示符的默認路徑。 – user1395264 2014-11-25 15:04:38

+0

@ user1395264嘿,我只是編輯它現在應該工作! – brso05 2014-11-25 15:14:28

+0

,對我來說工作正常..但我有一個問題..我在循環中運行此操作,並且每個批處理都有特定操作..完成操作後,我希望執行下一批操作...爲此,我需要使用CommandLine類..但這個解決方案不適用於CommandLine ...你能幫忙嗎?在我的問題中更新了我的剪輯。 – user1395264 2014-11-26 15:03:31

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); 

這一行允許用空格的文件路徑正確地處理。

相關問題