2017-08-08 84 views
0

我面臨以下錯誤:爪哇 - > CreateProcess的錯誤= 2,系統找不到指定的文件

java.io.IOException: Cannot run program "C:\abc\man\b\manu.bat C:\Users\12x\test\testFiles\abc.properties" (in directory "C:\Users\12x\test\testFiles\abc.properties"): CreateProcess error=2, The system cannot find the file specified 
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048). 

請找我使用的代碼:

public class TestProcess { 


public TestProcess(Path workPath, Path exe, Path logbackConfig, 
        Path propertyfile) throws IOException { 

    String exeSuffix = ""; 
    if (OS.indexOf("win") >= 0) { 
     exeSuffix = ".bat"; 
    } 
    builder = new ProcessBuilder() 
      .directory(workPath.toFile()) 

      .command(workPath.resolve(exe).toAbsolutePath().toString() + exeSuffix+ " " + propertyfile) 
      .redirectOutput(Redirect.INHERIT) 
      .redirectError(Redirect.INHERIT); 

我的目標是運行bat文件(該文件存在於C:\ abc \ man \ b文件夾中),然後運行abc.properties(位於另一個文件夾C:\ Users \ 12x \ test \ testFiles中)。

在上面的代碼中,workPath有

C:\abc\man\b 

和propertyfile具有

C:\Users\12x\test\testFiles 

回答

-1

你不能exec()一個.BAT文件直接在Windows中值。你必須介入cmd /c

0

您不使用正確的語法:因爲ProcessBuilder不是解析器,所以不能使用字符串連接程序及其參數。

取而代之的是,建立一個名爲propertyfile_path_string對應的屬性文件字符串,並以此替換您的線路.command(...)

.command(workPath.resolve(exe).toAbsolutePath().toString() + exeSuffix, propertyfile_path_string) 
+0

我想這一點,但得到了以下錯誤: –

+0

的方法指令(名單) ProcessBuilder類型不適用於參數 \t(字符串,路徑) –

+1

@sandeepgoyal所以使用'propertyfile.toString()'。 – EJP

相關問題