1

ProcessBuilderとProcessを使用してJava経由で実行しているコマンドが機能しない理由を解明しようとして、髪の毛を引き裂いています。Windowsのコマンドラインで「同じ」コマンドを実行すると、期待どおりに機能します。それらは同じではないに違いありませんが、私は一生の間、その理由を理解することができません。

コマンドは次のとおりです。

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

出力は、取得して環境変数として設定する必要がある1行の文字列である必要があります(したがって、v。の基本的な使用法BufferedReader)。

コマンドを実行するとアプリケーションエラーが発生する私のJavaコードは、次のようになります。エントリポイントは次の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;

}

の出力System.err.println(...)は次のとおりです。

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$]
4

1 に答える 1

1

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)

于 2010-04-05T08:37:09.580 に答える