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();
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 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());
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 need to run an executable file from Java and have it open in a separate window. When I open the file without using Java, it works perfectly, but nothing seems to happen when I try to open it using Java code. I have tried using getRuntime and ProcessBuilder. The lines I used are below.
ProcessBuiler:
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\test.exe");
Process p = pb.start();
getRuntime:
Runtime.getRuntime().exec("C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\test.exe", null, new File("C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\"));
Both seemed to give the same result (nothing). Any ideas how I can make test.exe open in a new window?
On windows, you could try using cmd.exe to launch the process.
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "start", "C:\\Users\\Louis Windows\\workspace\\Py2exetest\\dist\\test.exe");
pb.start();
My setup is as follows:
C: contains operating system and final release of programs
Z: contains code I am working on
I am using Netbeans, which is installed on C:\Program Files (x86)
My project folders are in Z:
I am trying to debug a project which needs to run a process where the file for the process directory is "C:\TaxiPIM"
I have tried:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "Pim_Update_Client.jar");
pb.directory(new File("/TaxiPIM"));
and:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "Pim_Update_Client.jar");
pb.directory(new File("c:/TaxiPIM"));
and ended up google-e-eyed with results explaining how to change the directory...
But I need to change the drive as well as the directory.
Thanks for reading - feedback is most appreciated!
Edit: ProcessBuilders directory(File) method returns a new ProcessBuilder so try pb=pb.directory(new File("...)
crude way would be to export the command to a batchfikle in the same dir as your project and putting the change drive code into the batch file, too and then run the batch file from your code.
Example that changes from a directory on C to a directory on D; (i have my NetBeans installation and the project-directory on the C-Drive)
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","start","cmd");
pb=pb.directory(new File("D:\\src"));
pb.start();
And then after getting the new ProcessBuilder, just put in your command:
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","start","cmd");
pb = pb.directory(new File("c:/TaxiPIM/"));
pb.command("java", "-jar", "Pim_Update_Client.jar");
Thanks again to #masterX244