java를 사용하여 powershell 명령을 호출하는 방법.powershell 명령을 실행하는 동안 오류가 발생했습니다.
try {
ExecuteWatchdog watchdog = new ExecuteWatchdog(20000);
Process powerShellProcess = Runtime.getRuntime().exec(
"powershell.exe \"D:\\testscript.ps1\"");
if (watchdog != null) {
watchdog.start(powerShellProcess);
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
powerShellProcess.getInputStream()));
String line;
System.out.println("Output :");
while ((line = stdInput.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
참고 : 올바른 경로를 매핑합니다.
나는 위의 코드와 노력하지만 당신이에 도움을 주시기 바랍니다 수
java.io.IOException: Cannot run program "powershell.exe": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at com.powershell.PsJava.main(PsJava.java:17))
누구나 같은 오류를 제공합니다.
1) [Runtime.exec()이 실행되지 않을 때] (* http://www.javaworld.com/jw-12-2000/jw-1229-traps)의 권장 사항을 모두 읽고 (구현하고) 구현하십시오. html). 그게 문제를 해결할 수도 있습니다. 그렇지 않은 경우 실패한 이유에 대한 자세한 정보를 제공해야합니다. 그런 다음'exec'를 참조하고'ProcessBuilder'를 사용하여'Process'를 빌드한다는 것을 무시하십시오. 또한'String arg'를'String [] args'로 분해하여 그들 자신이 공백을 포함하는 인수를 설명하십시오. 2) 제목에 주요 태그를 추가 할 필요가 없습니다. –
파일을 찾을 수 없습니다. ''powershell.exe ''를''C : \ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe ''로 변경하십시오. – Cole9350
감사합니다 @ Cole9350, 문제가 해결되었습니다. – Sathiya