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();
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.