[英]How can I run this command from java?
我正在嘗試運行它從 shell 成功運行的命令,但是當我創建一個字符串時,它無法正確運行並且拋出太多錯誤。 這是我要運行的命令:
curl -XPOST 'localhost:9200/_security/user/testUser' -H 'Content-Type: application/json' -d'{"password":"userpassword", "full_name":"user","email":"test@gmail.com", "roles": [ "superuser"]}'
這是我正在使用的代碼:
String s = "curl -XPOST 'localhost:9200/_security/user/testUser' -H 'Content-Type:application/json' -d'{\"password\":\"userpassword\",\"full_name\":\"user\",\"email\":\"test@gmail.com\",\"roles\":[\"superuser\"]}'";
Process process = Runtime.getRuntime().exec(s);
String result = printResults(process);
這是 function:
public String printResults(Process process) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
line += line;
}
return line;
}
您可以將go與rest模板進行外部API呼叫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.