Java – Use the process builder in Java to execute customs commands on Linux

Use the process builder in Java to execute customs commands on Linux… here is a solution to the problem.

Use the process builder in Java to execute customs commands on Linux

I’ve been struggling with this issue for a while now but I can’t seem to fix it. I’ve tried using ProcessBuilder to execute a custom command on a linux terminal, but it doesn’t work

I actually have two .sh files setProto.sh and setTls.sh files for setting up the environment. So in order to execute the commands, I need to run these, first creating two files for each Linux terminal instance. Only then can we run the custom command anloss on the same Linux terminal instance where the .sh file should be run. For some reason I can’t get it to work, what’s wrong in my code? Here is the code.

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;

public class EngineTest {

public static void main(String[] args) {
        try {
            ProcessBuilder builder = new ProcessBuilder(
                    "/. setProto.sh",
                    "/. setTls.sh",
                    "/anloss -i ${TOOL_INPUT}/census_10000_col5.csv  -d ${TOOL_DEF}/attr_all_def.txt -q k=14,dage=2 -g ${TOOL_RES}/census_100_col8_gen.csv");
            builder.directory(new File(System.getenv("HOME") + "/PVproto/Base"));
            File log = new File("log");
            builder.redirectErrorStream(true);
            builder.redirectOutput(Redirect.appendTo(log));
            Process process = builder.start();

BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line = "";
            String output = "";
            while ((line = bufferedReader.readLine()) != null) {
                output += line + "\n";
            }
            System.out.println(output);
            int exitValue = process.waitFor();
            System.out.println("\n\nExit Value is " + exitValue);
        } catch (IOException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Solution

By default, processes are not executed in the context of the shell; As a result, your shell script does not execute the way you are trying to.

ProcessBuilder pb =
    new ProcessBuilder( "/bin/bash",
                        "-c", 
                        ". setProto.sh && . setTls.sh && /anloss -i ${TOOL_INPUT}/census_10000_col5.csv  -d ${TOOL_DEF}/attr_all_def.txt -q k=14,dage=2 -g ${TOOL_RES}/census_100_col8_gen.csv" );

I’m not sure it’s unusual for the /anloss – command to be in the root directory /. (Also, what should you implement in front of your shell scripts with /. – what should they implement?) )

Later

Be sure to replace /anloss

with an absolute path name or a relative path name relative to $HOME/PVproto/Base, for example, if in this directory, use ./anloss, if in $HOME/PVproto/Base/ SomeSub, using SomeSub/anloss, etc

Also, if setProto.sh and . setTls.sh are not in $HOME/PVproto/Base, use the appropriate absolute or relative path name. If so, use ./setProto.sh and ./setTls.sh to avoid dependencies on the environment variable PATH setting.

Related Problems and Solutions