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