0

I am trying to run a Linux command from Java. The command runs perfect from command line itself but from Java it does not. The command from command line is

curl -H "Content-Type: text/plain" -d "$(printf '#Genes\nPIK3C2A\nPTEN\nUNC5B')" -X POST --url https://reactome.org/AnalysisService/identifiers/projection/

The command from Java is

 String $notifyMsg="\"$(printf '#Genes\nPIK3C2A\nPTEN\nUNC5B')\"";
 String $reactome = "https://reactome.org/AnalysisService/identifiers/projection/";
 String $notifyTitle= "\"Content-Type: text/plain\"";
 String $command = "curl" + " " + "-H"+ " " +  $notifyTitle + " "+ "-d " + $notifyMsg +" "+ "-X" + " " + "POST" + " " +  " " + "--url" +" " + $reactome;
 System.out.println("Command is: " + $command);

      Process p=Runtime.getRuntime().exec($command);
      p.waitFor();
      System.out.println("Run Result " + p.exitValue() )

I am supprosed to get an output like this

{"summary":{"token":"MjAxODAxMjMxNjQyMDZfNjcy","projection":true,"interactors":false,"type":"OVERREPRESENTATION","sampleName":"Genes","text":true},"expression":{"columnNames":[]},"identifiersNotFound":0,"pathwaysFound":51,"pathways":

But instead i get 0 . Can anyone tell me what could be wrong?

2
  • Consider using jsoup or apache http components or any other Java library. You don't need to use curl here. Commented Jan 28, 2018 at 18:12
  • Why would you want to download a file by launching curl when there are a number of native Java libraries to do this? Commented Jan 28, 2018 at 18:12

3 Answers 3

2

Quoting and escaping are your enemies. The best thing to do is avoid them entirely. Start the process with a method that lets you pass the individual arguments as separate strings without having to split them apart.

Process p = new ProcessBuilder(
    "curl",
    "-H", "Content-Type: text/plain",
    "-d", "#Genes\nPIK3C2A\nPTEN\nUNC5B",
    "-X", "POST",
    "--url", "https://reactome.org/AnalysisService/identifiers/projection/"
).start();

Your code calls exitValue(), which gives you the exit code of the process. To read its output, read from its input stream.

InputStream s = p.getInputStream();
Sign up to request clarification or add additional context in comments.

Comments

1

p.exitValue() will give you the exit code of the process. It returns 0 because the curl succeeded with exit code 0.

I would consider looking into the HttpClient library to perform these natively in Java.

Comments

1

This is what worked for me:

String notifyMsg = "#Genes\nPIK3C2A\nPTEN\nUNC5B";
String reactome = "https://reactome.org/AnalysisService/identifiers/projection/";
String notifyTitle = "Content-Type: text/plain";

Process p = new ProcessBuilder("curl", "-H", notifyTitle, "-d", notifyMsg, "-X", "POST", "--url", reactome).start();
p.waitFor();
try (Scanner scanner = new Scanner(new InputStreamReader(p.getInputStream()))) {
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
}
System.out.println("Run Result " + p.exitValue());
  1. Don't concat the command line manually, use vararg constructor of the ProcessBuilder
  2. There were extra double quotes in your Content-Type instruction and your -d
  3. $(printf is something that shell interprets, but Java doesn't. Just use \n in Java string literals to get new lines.

I've also added some code to read the output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.