Running an EXE from Java - java

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();

Related

Can't save mysql dump file to my hard drive from a java progam [duplicate]

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" });

How to run python file in java project?

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();

change workspace of a ProcessBuilder Java

Im actually doing a Java Program that execute a shell script with this ProccesBuilder:
ProcessBuilder builder = newProcessBuilder("sh","script.sh",params);
builder.redirectOutput(new File("outNormal.txt"));
builder.redirectError(new File("outError.txt"));
Process p = builder.start();
p.waitFor();
The problem i have is that outNormal.txt and outError.txt, and another file my script creates are placed at the same level as /src and /bin, and i want to change this to a new directory.
is this possible?

save the output of command prompt to a file [duplicate]

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" });

Change the working drive java processbuilder

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

Categories