executing the customs command on linux with process builder in java
java, linux, shell
Solution
Process does not by default execute in the context of a shell; therefore your shell scripts cannot be executed the way you tried.
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 am not sure about `/anloss` - it's unusual for a command to be in root's home `/`. (Also, the `/.` you had there in front of the shell scripts - what should they achieve?)
Later
Make sure to replace `/anloss` with an absolute pathname or a relative pathname relative to `$HOME/PVproto/Base`, e.g., if it is in this directory, use `./anloss`, if it is in `$HOME/PVproto/Base/SomeSub`, use `SomeSub/anloss`, etc.
Also, if setProto.sh and . setTls.sh are not in `$HOME/PVproto/Base`, use an appropriate absolute or relative pathname. If they are, use `./setProto.sh` and `./setTls.sh` to avoid dependency on the setting of environment variable PATH.
Problem
I've been struggling for a while now with this problem and i can't seem to fix it. i have tried `ProcessBuilder` for executing the custom command on linux terminal but its not working Actually i have two `.sh` file `setProto.sh and setTls.sh` file which is used to set the environment.So for executing the command i need to run these two file first for each instance of linux terminal.Only then we can be able to run the custom command `anloss` on the same instance of linux terminal in which .sh file supposed to run.For some reason i'm not able to make it work what is the mistake in my code ? Here's 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(); } } } ```