小编Sat*_*iya的帖子

运行 powershell 命令时出错

如何使用java调用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();
    }
Run Code Online (Sandbox Code Playgroud)

注意:我映射了正确的路径。

我尝试使用上面的代码,但它给出了类似的错误

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))
Run Code Online (Sandbox Code Playgroud)

任何人都可以请你帮忙。

java powershell process runtime.exec ioexception

0
推荐指数
1
解决办法
7272
查看次数

标签 统计

ioexception ×1

java ×1

powershell ×1

process ×1

runtime.exec ×1