简体   繁体   中英

Java IOException on exec call with any command-line arguments

I've got a java application that will eventually get fairly deep into external process integration, including IPC with those processes. For now though, what I'm trying to do is simply run a powershell script from java.

what I've got:

private void runPowershellScript() {
    String command =
            "" + "powershell" + " ";
//            Paths.get("").toAbsolutePath() + "\\" + scriptFileName + " " +
//            Paths.get("").toAbsolutePath() + "\\" + INPUT_FILE_NAME + " " +
//            Paths.get("").toAbsolutePath() + "\\" + OUTPUT_FILE_NAME + "";

    try {
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        Process process = builder.start();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;

        while ((line = reader.readLine ()) != null) {
            System.out.println ("Stdout: " + line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

With what you see there, I get the Windows Powershell name and copyright coming out over that reader, but if I add any of the commented out lines (all of which resolve to proper paths, eg C:\\Users\\Geoff\\Code\\OPTIP\\FakeProgram.ps1 ) I get: java.io.IOException: Cannot run program "powershell C:\\Users\\Geoff\\Code\\OPTIP\\FakeProgram.ps1 ": CreateProcess error=2, The system cannot find the file specified

I've tried a dozen different combinations of strong and weak quotes, and I've tried passing them as arguments to cmd.exe /c powershell ... but nothing I've tried runs the script. If there is a single space in the command string, I get an IO Exception.

I'm wondering if maybe it has something to do with character encoding? When I simply invoke powershell , I'm getting 'back from reader.readLine() is: W\i\n\ ... Which I presume is my IDE's (IntelliJ's) way of telling me its "Windows Powershell" with a null unicode character between each letter.

The Java ProcessBuilder documentation is a little vague on exactly what you can pass as arguments:

a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. For example, it is common for each conceptual argument to be an element in this list, but there are operating systems where programs are expected to tokenize command line strings themselves - on such a system a Java implementation might require commands to contain exactly two elements.

I dont know what that means. The command I'm trying to give it works from a CMD and Powershell window, and also from the windows run dialog.

gist containing the class of above method: https://gist.github.com/Groostav/9c5913e6f4696a25430d gist containing my powershell script: https://gist.github.com/Groostav/347a283ac7ec6a738191

Thanks for any help.

You have to put the arguments in separate strings, not concatenating them to the powershell call as a single string. Something like

new ProcessBuilder("Powershell", scriptFileName,  INPUT_FILE_NAME);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM