Korlimann
Korlimann

Reputation: 147

ProcessBuilder can't find custom .exe

I am currently trying to write a small program in java which should take over the job of an old batch script I've been using.

This batch script executes a program called sum.exe (Supermicro Update Manager).

However, no matter which way I try, the program either does not respond, or straight up tells me it can't find the file in the directory where the file is.

    boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
    ProcessBuilder builder = new ProcessBuilder("C:\\Users\\[Username]\\SUM\\sum.exe");
    if (isWindows) {
        builder.command("sum.exe", "-i 192.168.4.10 -u ADMIN -p ADMIN -c CheckOOBSupport");
    } else {
        builder.command("sh", "-c", "ls");
    }
    builder.redirectErrorStream(true);
    Process process = builder.start();
    StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), System.out::println);
    StreamGobbler streamGobblerErrors = new StreamGobbler(process.getErrorStream(), System.out::println);
    Executors.newSingleThreadExecutor().submit(streamGobbler);
    Executors.newSingleThreadExecutor().submit(streamGobblerErrors);
    int exitCode = process.waitFor();
    assert exitCode == 0;

This is the code I currently have. The command I'm trying to call here will 100% give an error, so I made sure to redirect those as well.

As far as I understood, there are 3 different ways to set a Filepath for the Processbuilder. Either you:

  1. Set the path in the constructor
  2. Set the path between your executable and arguments in the .command() method
  3. Or you set the directory of the builder by creating a new file (and using System.Property)

I have a complete copy of the SUM-Folder under: C:\Users\[Username]\SUM, and I have tried all 3 options listed above with this, but always got the error message that the system could not find the file specified Additionally, I'm still not sure if the command would even work this way. I have only ever used sum.exe via batch-Script or cmd.exe itself, so wouldn't the command need to be

builder.command("cmd.exe", "sum.exe -i 192.168.4.10 -u ADMIN -p ADMIN -c CheckOOBSupport)

instead?

Can anyone tell me what I'm doing wrong? Thanks!

Upvotes: 0

Views: 81

Answers (1)

DuncG
DuncG

Reputation: 15136

The ProcessBuilder command line is passed in the constructor or the command() method so in your example you've overridden the value used.

Choose the way you need:

ProcessBuilder builder = new ProcessBuilder("C:\\Users\\[Username]\\SUM\\sum.exe",
        "-i", "192.168.4.10", 
        "-u", "ADMIN","-p", "ADMIN",
        "-c", "CheckOOBSupport");

or

ProcessBuilder builder = new ProcessBuilder();
builder.command("sum.exe", 
        "-i", "192.168.4.10", 
        "-u", "ADMIN","-p", "ADMIN",
        "-c", "CheckOOBSupport");

Note also that the arguments for the command need to supplied as separate string values rather than all concatenated together as one value, and you need absolute path to "sum.exe" if that is not found in the current directory or under a directory of environment variable "Path".

Upvotes: 1

Related Questions