【问题标题】:Java Powershell CreateProcess error=2, The system cannot find the file specifiedJava Powershell CreateProcess 错误=2,系统找不到指定的文件
【发布时间】:2013-10-30 23:45:28
【问题描述】:

我在 java 中执行 powershell 命令并且我已经编写了两个程序,但是奇怪的部分是一个工作正常,另一个抛出错误。抛出错误的代码如图所示

我尝试了以下方法 1)指定powershell的完全指定路径 2)我的路径变量有以下 - “C:\WINDOWS\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 powershell


【解决方案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);

【讨论】:

  • 完全做到了,除了稍有改变,我不得不为 -Command 使用引号,如图所示String ps = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\ \powershell.exe"; String args = "-Command \"& { Get-ChildItem C:\\test -Recurse -force |测量对象}\""; ProcessBuilder builder = new ProcessBuilder(ps, args);
【解决方案2】:

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);

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 1970-01-01
    • 2021-11-24
    • 2018-09-17
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    相关资源
    最近更新 更多