views:

145

answers:

1

I'm tearing my hair out trying to work out why the command I'm executing via Java using a ProcessBuilder & Process is not working. I run the "same" command at the Windows command line and it works as expected. It must be that they're not the same but I can't for the life of me work out why.

The command is this:

ccm start -nogui -m -q -n ccm_admin -r developer -d /path/to/db/databasename -s http://hostname:8400 -pw Passw0rd789$

The output is should be a single line string that I need to grab and set as an environment variable (hence the v. basic use of the BufferedReader).

My Java code, which when it runs the command gets an application error, looks like this with entry point being startCCMAndGetCCMAddress():

private static String ccmAddress = "";

private static final String DATABASE_PATH = "/path/to/db/databasename";
private static final String SYNERGY_URL = "http://hostname:8400";

private static final String USERNAME = "ccm_admin";
private static final String PASSWORD = "Passw0rd789$";
private static final String USER_ROLE = "developer";


public static List<String> getCCMStartCommand() {
    List<String> command = new ArrayList<String>();

    command.add("cmd.exe");
    command.add("/C");

    command.add("ccm");
    command.add("start");
    command.add("-nogui");
    command.add("-m");
    command.add("-q");
    command.add("-n "+USERNAME);
    command.add("-r "+USER_ROLE);
    command.add("-d "+DATABASE_PATH);
    command.add("-s "+SYNERGY_URL);
    command.add("-pw "+PASSWORD);

    return command;
}

private static String startCCMAndGetCCMAddress() throws IOException, CCMCommandException {
    int processExitValue = 0;

    List<String> command = getCCMStartCommand();

    System.err.println("Will run: "+command);

    ProcessBuilder procBuilder = new ProcessBuilder(command);
    procBuilder.redirectErrorStream(true);
    Process proc = procBuilder.start();
    BufferedReader outputBr = new BufferedReader(new InputStreamReader(proc.getInputStream()));

    try {
        proc.waitFor();
    } catch (InterruptedException e) {
        processExitValue = proc.exitValue();
    }

    String outputLine = outputBr.readLine();
    outputBr.close();

    if (processExitValue != 0) {
        throw new CCMCommandException("Command failed with output: " + outputLine);
    }

    if (outputLine == null) {
        throw new CCMCommandException("Command returned zero but there was no output");
    }

    return outputLine;

}

The output of the System.err.println(...) is:

Will run: [cmd.exe, /C, ccm, start, -nogui, -m, -q, -n ccm_admin, -r developer, -d /path/to/db/databasename, -s http://hostname:8400, -pw Passw0rd789$]
+1  A: 

I think you need to provide each argument separately, and without leading/trailing spaces, rather than concatenating select ones e.g "-pw PASSWORD". That way you'll invoke the process with the correct argument set (as it would see from the command line)

Brian Agnew
<slaps forehead> Doh! Should have known that. Thanks.
Tom Duckering