2013-10-29 158 views
1

我在java中執行powershell命令,我寫了兩個程序,但奇怪的部分是一個正常工作,另一個拋出錯誤。拋出錯誤的代碼是如圖Java Powershell CreateProcess錯誤= 2,系統找不到指定的文件

我曾嘗試以下 1)Spcifying的完全指明的powershell 2的路徑)我的路徑變量具有以下 - 「C:\窗口\ system32 \ WindowsPowerShell \ V1。 0"

我知道我可能會做一些小事,但它是一個一天,我無法弄清楚這個問題可能是

import java.io.IOException; 

public class FileCount { 

public static void main(String[] args) { 
    Process flCntProcess = null; 
    try { 

     String test = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe -Command \"& { Get-ChildItem C:\\test -Recurse -force | Measure-Object }\""; 
     System.out.println("Powershell command : " + test); 
     ProcessBuilder builder = new ProcessBuilder(test); 
     builder.redirectErrorStream(true); 
     flCntProcess = builder.start(); 

     // FILE COUNT OUTPUT STREAM PROCESSING 
     NotifyThreadComplete outputThread = new ProcessHandler(flCntProcess.getInputStream(),"OUTPUT"); 
     outputThread.addListener(new ThreadCompleteListener() { 

      @Override 
      public void notifyCompletion(Thread t, long startTm, boolean didErrorOut, String noOfLines) { 
       System.out.println("Completed Output Stream Processing"); 
       System.out.println("Printing values"); 
       System.out.println("No of Lines : " + noOfLines); 
       System.out.println("Did Error out : " + didErrorOut); 

       if(didErrorOut) { 
        System.out.println("Do not continue with processing"); 
       } else { 
        System.out.println("Continue with processing"); 
       } 
      } 
     }); 
     System.out.println("Starting output thread "); 
     outputThread.start(); 

    } catch (Exception e) { 
     System.err.println("Exception while counting files using Powershell Command" + e.getMessage()); 
    } finally { 
     if(flCntProcess != null && flCntProcess.getOutputStream() != null) { 
      try { 
       flCntProcess.getOutputStream().close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
} 
+0

可能重複[無法執行在Java中使用的ProcessBuilder在Windows 7下的javac或其他命令行應用程序] (http://stackoverflow.com/questions/4985038/cant-execute-javac-or-other-command-line-applications-in-java-using-processbuil) – Raedwald

回答

1

錯誤代碼指示文件執行無法找到。嘗試從它的參數分手了程序:

String ps = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe"; 
String args = "-Command \"& { Get-ChildItem C:\\test -Recurse -force | Measure-Object}\"";   
ProcessBuilder builder = new ProcessBuilder(ps, args); 
+0

這完全做到了,除了有輕微的變化,我必須使用-Co​​mmand的引號,如下所示:String s =「C:\\ WINDOWS \\ system32 \\ windowspowershell \\ v1.0 \\ powershell.exe」; String args =「-Command \」&{Get-ChildItem C:\\ test -Recurse -force |測量 - 對象} \「」; ProcessBuilder builder = new ProcessBuilder(ps,args); – user1707141

0

ProcessBuilder構造方法不接受包含CLI調用一個單獨的字符串,但包含以字符串數組:

  • 程序要執行
  • 它的參數

See the javadoc

所以它解釋你的整個字符串作爲test程序名稱,拆分它應該工作:

final String psh = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"; 
final String args = "-Command & { Get-ChildItem C:\\temp -Recurse -force | Measure-Object }"; 
final ProcessBuilder builder = new ProcessBuilder(psh, args); 
相關問題