简体   繁体   English

如何从 java 运行此命令?

[英]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.

相关问题 如何从Java在命令行中运行ruby文件? - How can I run a ruby file in command line from Java? 如何从 Java 应用程序运行 Windows 命令提示符? - How can I run windows command promt from java application? 如何从Java代码运行命令并读取输出? - How can I run command from Java code and read the output? 如何使用 Nodejs 运行 java 命令 - How can i run java command with Nodejs 如何从命令行运行从Intellij创建的Java JAR - How can I run Java JAR created from Intellij from command line 如何从命令行运行Ruby脚本并使用Java代码获取响应? - How can I run a Ruby script from command line and get the response using Java code? 如何从Windows批处理文件运行Java但隐藏命令窗口? - How can I run a Java from windows batch file but hiding the command window? 我怎么能告诉Java代码运行脚本,因为它是从普通用户的命令行运行的? - How can i tell Java code run the script as it was running from command line as normal user? 如何从命令行运行Eclipse编译的Java类? - How can I run an Eclipse compiled Java class from the command line? 如何在带有分配选项的Java中运行sh命令 - How can I run a sh command in java with assigning options
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM