I want to use ProcessBuilder to execute a python script. I can execute this script with the command "python3 myscript.py" without any problem. But When I use ProcessBuilder in java, I get an error from my script :
import numpyImportError: No module named 'numpy'
numpy is the module that I want to use, but I can't find it.
This is the way I call my script :
ProcessBuilder builder = new ProcessBuilder("python3","main.py","-rd ",selectedFile.getAbsolutePath());
builder.redirectErrorStream(true);
Process process = builder.start();
You need to specify your python path:
run in your terminal: "which python3"
ProcessBuilder builder = new ProcessBuilder("your/python/path/python3","main.py","-rd ",selectedFile.getAbsolutePath());
Related
Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt");
I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created.
This script runs fine if I run it on shell.
Any ideas?
You need to use ProcessBuilder to redirect.
ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException
When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.
Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });
I've got a python file which opens a webpage with selenium.How can I run this file in java application ? I tried Runtime().exec("python main.py") and "python3 main.py" but it did't work.
Use ProcessBuilder.
String[] commands = { "python", "main.py" }; // Need to add a python or python3 to the system path
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory("Set the path of the main.py folder");
Process proc = pb.start();
Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt");
I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created.
This script runs fine if I run it on shell.
Any ideas?
You need to use ProcessBuilder to redirect.
ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException
When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.
Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });
I want to run the following command from a java file command : "java hello < C:\iptest\input.txt > C:\outtest\name.txt "hello" will take input from "C:\iptest\input.txt" and will produce an output file at "C:\outtest\name.txt".
codes i have done
String command[]={"java","hello","< C:\\iptest\\input.txt >","C:\outtest\name.txt"};
ProcessBuilder pb=new ProcessBuilder(command);
pb.directory(new File("E:\"));
and now how to go forward i have no idea .Please Help!!
Here is a link to the ProcessBuilder definition on Oracle, which explains how to use this class. Typically, this is used for executing non-java processes, e.g. .BAT or .EXE processes, but I suppose you could use it to execute any process you wish.
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
I have a java web development project, and want to call a python script to run in the background and then carry on with the java.
String command = "cmd.exe /c cd "C:\\path\\to\\py\\" && python script.py";
Process p = Runtime.getRuntime().exec(command);
Nothing seems to happen when i call this, but i need to change directory first as the script accesses files in its directory.
Thanks for your help
Edit:
Correct answer was adding start, this is my edited code
String command = "cmd.exe /c cd "C:\\path\\to\\py\\" && start python script.py";
Process p = Runtime.getRuntime().exec(command);
Rather than using cmd to change the directory, you can set a process's working directory from the Java side. For example
ProcessBuilder pb = new ProcessBuilder("python", "script.py");
pb.directory(new File("C:\\path\\to\\py"));
Process p = pb.start();
Did you configure your environment to support "executable" python scripts?
If not, you should call it like this:
String command = "cmd.exe /c start python path\to\script\script.py";
Process p = Runtime.getRuntime().exec(command);
The start command runs the appropriate executable (in this case python interpreter), with its supplied arguments (in this case the script itself).