简体   繁体   English

为什么我的curl命令不能与Java ProcessBuilder API一起使用

[英]Why does my curl command not work with Java ProcessBuilder API

I want to execute following CURL command from Java: 我想从Java执行以下CURL命令:

curl -v -X PUT --data-binary "@configfile.json" -u username:password -D /tmp/grabbit_headers http:// server:port/grabbit/job curl -v -X PUT --data-binary“@ configfile.json”-u username:password -D / tmp / grabbit_headers http:// server:port / grabbit / job

(The space between http:// and server... is inserted as per stack overflow guidelines but is not part of my code) (根据堆栈溢出指南插入http://和server ...之间的空格,但不是我的代码的一部分)

I have followed 我跟着

http://alvinalexander.com/java/java-exec-processbuilder-process-1 , process2 and process3 http://alvinalexander.com/java/java-exec-processbuilder-process-1,process2和process3

to create my java classes that use ProcessBuilder API to execute shell commands. 创建使用ProcessBuilder API执行shell命令的java类。

If I execute the CURL command directly on the shell, it works and produces expected results. 如果我直接在shell上执行CURL命令,它会工作并产生预期的结果。 If I use the exact same command via Java classes, it executes without any errors, with exitCode=0, but does not product intended result. 如果我通过Java类使用完全相同的命令,它执行没有任何错误,exitCode = 0,但不产生预期的结果。

Intended result here is to use Grabbit ( https://github.com/TWCable/grabbit ) to migrate content from one Adobe AEM instance to another. 这里的预期结果是使用Grabbit( https://github.com/TWCable/grabbit )将内容从一个Adobe AEM实例迁移到另一个实例。

Here are my primary methods: 这是我的主要方法:

public String processBuilderExample() throws IOException, InterruptedException
{


    // build the system command we want to run
  List<String> commands;
  String result="";      
  /*
   * CURL Commands:
   * curl -v -X PUT --data-binary "@$configpath" -u $username:$password -D /tmp/grabbit_headers $client$GRABBIT_JOB > /tmp/grabbit
   * */
  String curl_command="curl -v -X PUT --data-binary \"@"+this.configpath+"\""+" -u "+this.username+":"+this.password+" -D /tmp/grabbit_headers "+this.client+this.GRABBIT_JOB;

  commands = new ArrayList<String>(Arrays.asList(curl_command.split(" ")));      

  result=runCommand(commands);
  return result;

}

//@Override
public String runCommand(List<String> commands) throws IOException, InterruptedException
{

  // execute the command
  SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
  int result = commandExecutor.executeCommand();

  // get the stdout and stderr from the command that was run
  StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();
  StringBuilder stderr = commandExecutor.getStandardErrorFromCommand();

  StringBuilder finalReturnString=new StringBuilder();
  finalReturnString.append("<br/>STDOUT:<br/>");
  finalReturnString.append(stdout.toString());
  finalReturnString.append("<br/>STDERR:<br/>");
  finalReturnString.append(stderr.toString());

  return finalReturnString.toString();

}

Use ProcessBuilder to configure the redirect. 使用ProcessBuilder配置重定向。

">" redirect is part of the shell (sh/bash) and not a command. “>”redirect是shell(sh / bash)的一部分,而不是命令。

Refer to Runtime's exec() method is not redirecting the output on how to use process builder to redirect. 请参阅运行时的exec()方法不会重定向输出,以了解如何使用流程构建器进行重定向。

Refer to &> redirection not working correctly on why it doesn't work. 请参阅&>重定向无法正常工作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM