Search code examples
javalinuxspring-bootprocessbuilderopa

ProcessBuilder is not working correctly when I deploy my Spring boot Application to a Linux server


 public void pushDataToOPA() throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder("curl", "-X", "PUT", "-H", "\"Content-Type: application/json\"", "-d",
            "@data.json", dataUrl);
    Process p = pb.start();
    p.waitFor();

}

When I deploy my spring boot application to a Linux server, this data.json file is not put to the target (dataUrl) when this pushDataToOPA method is called. But when I run my application in locally, it successfully puts the JSON file to the same target (dataUrl).

 public void updatePolicy() throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder("curl", "-X", "PUT", "--data-binary", "@policy.rego", policyUrl);
    Process p = pb.start();
    p.waitFor();
}

but this method is working on both sides.


Solution

  • You can redirect the process output to a file with the following code:

    public void updatePolicy() throws IOException, InterruptedException {
            ProcessBuilder pb = new ProcessBuilder();
            pb.command("curl", "-X", "PUT", "--data-binary", "@policy.rego", policyUrl);
            pb.redirectOutput(new File("outputfilePath"));
            pb.redirectError(new File("errorFilePath"));
            Process p = pb.start();
            p.waitFor();
            }
    
    public void pushDataToOPA() throws IOException, InterruptedException {
            ProcessBuilder pb = new ProcessBuilder("curl", "-X", "PUT", "-H", "\"Content-Type: application/json\"", "-d",
                    "@data.json", dataUrl);
            pb.redirectOutput(new File("outputfilePath"));
            pb.redirectError(new File("errorFilePath"));
            Process p = pb.start();
            p.waitFor();
        }
    

    For more details on how to redirect logs read here