Original command not working via Java
Since last day, I have been trying to execute a command on a terminal (MAC) using JAVA, but everything I do nothing seems to work.
I have the following two commands that I want to execute and get the result in JAVA
source activate abc_env
python example.py
So far I have tried the following methods with no output
String[] command = new String[] { "source activate abc_env", "python example.py"};
String result = executeCommands(command);
Here is my executeCommands method
private static String executeCommands(String[] command) {
StringBuffer output = new StringBuffer();
Process p;
try {
for(int i=0; i< command.length;i++)
{
p = Runtime.getRuntime().exec(command[i]);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
System.out.println("Error output: " + p.exitValue());
System.out.println("Output:" + output);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Here");
}
return output.toString();
}
This gives me the following exception
Cannot start program "source": error = 2, no such file or directory
I have searched the internet and people are saying that the source won't work like this and I have to change the command to
String[] command = new String[] { "bash -c 'source activate abc_env'", "python example.py"};
Now I get an exception, but the command still doesn't work and it returns '2' as exitValue ()
Then I tried to execute the commands as a script
#!/bin/bash
source activate abc_env
python example.py
I am getting the following exception when I read the .sh file as a string and pass it to the command
Unable to start program "#! / Bin / bash": error = 2, No such file or directory
So my question is how to properly run the original command followed by the python command through Java? My ultimate goal is to execute some python from Java.
EDIT1: If I try the following command and print the output stream
String[] command = {
"/bin/bash",
"-c",
"source activate cvxpy_env"
};
executeCommand(command));
Output stream:
ExitValue: 1
ErrorOutput: / bin / bash: activate: no such file or directory
If I try the same command but with single quotes around 'source activate abc_env'. I am getting the following output
ExitValue: 127
ErrorOutput: / bin / bash: activate source cvxpy_env: command not found
Decision:
String[] command = {
"/bin/bash",
"-c",
"source /Users/pc_name/my_python_library/bin/activate abc_env;python example.py"
};
source to share
According to the Javadoc, Runtime.exec(String)
breaks the command in the command-args list with StringTokenizer
, which will probably break your command to:
bash
-c
'source
activate
abc_env'
This is clearly not what you want. You should probably use a version Runtime.exec(String[])
that takes a ready-made argument list by passing it to it new String[] {"bash", "-c", "source activate abc_env"}
.
Now, to understand why it doesn't work, you have to read not only it stdout
, but also stderr
using p.getErrorStream()
. Just print out what you are reading and it will be a great help for debugging.
Re: your edit. Now it looks like it works just fine as far as Java and bash. The "activate: no such file or directory" output is probably the result of a successful command run source
. Just source
can't find the file activate
. Is it in the working directory? If not, you should probably have "cd /wherever/your/files/are; source activate cvxpy_env"
. Oh, and if your python script depends on any side effects the original command has, you probably have to execute it on the same instance bash
as:
String[] command = {
"/bin/bash",
"-c",
"cd /wherever/your/files/are && source activate cvxpy_env && python example.py"
};
Or better yet, package it all into one shell script and then just Runtime.exec("./my_script.sh")
(don't forget, though chmod +x
).
source to share