1

I want to execute following CURL command from Java:

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)

I have followed

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

to create my java classes that use ProcessBuilder API to execute shell commands.

If I execute the CURL command directly on the shell, it works and produces expected results. If I use the exact same command via Java classes, it executes without any errors, with exitCode=0, but does not product intended result.

Intended result here is to use Grabbit (https://github.com/TWCable/grabbit) to migrate content from one Adobe AEM instance to another.

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();

}
rjn
  • 13
  • 4
  • You created the `finalReturnString` but never put any of the output in. – xiaofeng.li Aug 02 '16 at 03:18
  • Just use OkHttp unless there is some curl quirk you need. Calling out to a process for this seems painful and awkward compared to nice HTTP Client libraries available for Java. – Yuri Schimke Aug 02 '16 at 07:48
  • @LukeLee: I have added `append` calls to `finalReturnString`. – rjn Aug 02 '16 at 12:30

1 Answers1

0

Use ProcessBuilder to configure the redirect.

">" redirect is part of the shell (sh/bash) and not a command.

Refer to Runtime's exec() method is not redirecting the output on how to use process builder to redirect.

Refer to &> redirection not working correctly on why it doesn't work.

Community
  • 1
  • 1
Boon
  • 1,871
  • 1
  • 21
  • 31
  • Thanks @Boon. I have added the redirect, the result hasn't changed still: `ProcessBuilder pb = new ProcessBuilder(commandInformation); pb.redirectOutput(new File("/tmp/grabbit")); Process process = pb.start();` – rjn Aug 02 '16 at 12:34
  • Would you please print out the full "command" from Java and paste it here? – Boon Aug 02 '16 at 15:20
  • Actually adding `pb.redirectOutput(new File("/tmp/grabbit"));` did the trick. Thanks a lot ! – rjn Aug 03 '16 at 19:02
  • Glad it worked! Cheers! You may mark the answer as correct... it looks like you are new to StackOverflow. – Boon Aug 04 '16 at 14:40