[英]How can I run this command from java?
I am trying to run this command that it ran successfully from the shell but when I create a String it does not run correctly and it is throwing too many errors.我正在尝试运行它从 shell 成功运行的命令,但是当我创建一个字符串时,它无法正确运行并且抛出太多错误。 This is the command that I want to run:
这是我要运行的命令:
curl -XPOST 'localhost:9200/_security/user/testUser' -H 'Content-Type: application/json' -d'{"password":"userpassword", "full_name":"user","email":"test@gmail.com", "roles": [ "superuser"]}'
And this the code that I am using:这是我正在使用的代码:
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);
This is the function:这是 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;
}
You can go with rest template for external API call.您可以将go与rest模板进行外部API呼叫。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.