問題は、出力を読み取らないことです。あなたのコードを変更しましたので、はとなります。何らかの理由で正しくテストできないので、私は100%確信していません。
EDIT - これはうまくいきませんでした。これはあなたのために働くはずです。
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curl);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //BufferedReader to read the output
StringBuilder sb = new StringBuilder(); //What will hold the entire console output
String line = ""; //What will hold the text for a line of the output
while ((line = reader.readLine()) != null) { //While there is still text to be read, read it
sb.append(line + "\n"); //Append the line to the StringBuilder
}
System.out.println(sb); //Print out the full output
} catch (Exception e) {
e.printStackTrace();
}
EDIT - 代わりのRuntime
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", curl);
builder.redirectErrorStream(true);
Process p = builder.start();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
int linenum = 0;
while (true) {
linenum++;
line = r.readLine();
if (line == null) {
break;
}
sb.append(line);
}
System.out.println(sb);
が重複する可能性の
ProcessBuilder
を活用した新たなコード(http://stackoverflow.com/questions/2586975/how-to [JavaでのcURLを使用する方法?] -use-curl-in-java) – pczeus