Run .exe file in Java from file location
You don't need a console. You can execute a process using a working directory:
exec(String command, String[] envp, File dir)
Executes the specified string command in a separate process
with the specified environment and working directory.
- command is the location of the .exe
- envp can be null
- dir, is the directory of your .exe
With respect to your code it should be...
Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
How to run a exe file using Java
You can execute a process using a working directory:
exec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory.
command is the location of the .exe
envp can be null
dir, is the directory of your .exe
With respect to your code it should be...
Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
You can use Runtime.exec(java.lang.String, java.lang.String[], java.io.File) where you can set the working directory.
Or else you can use ProcessBuilder as follows:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
pb.directory(new File("myDir"));
Process p = pb.start();
For reading output from process:
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
How to run exe file in Java program
You can send a click signal to the system and specify its position on the screen. Check this question
Executing .exe file in Java in separate directory
You want to look at Process Builder. In particular, you want to set the working directory using the ProcessBuilder.directory(File)
method. You execute it using the start()
method.
For example:
final ProcessBuilder pb = new ProcessBuilder("theExecutable");
pb.directory(new File("the/working/directory/path"));
final Process p = pb.start();
Executing .exe file from java application
You have created a process, and then immediately destroyed it. Of course the executable won't run. Try calling .waitFor()
instead (or just let it run).
How to run .exe file within program
Lets imagine a File f = new File(Path);
If in that case we have the file inside our project (same directory),then we dont need to add the path, just the file name and extension (ex: .txt)......I guess in your case it would be something similar.
Related Topics
Java Wait for Thread to Finish
How to Enumerate All Classes in a Package and Add Them to a List
Replace the Last Part of a String
Java 8 Instant.Now() with Nanosecond Resolution
Java Division by Zero Doesnt Throw an Arithmeticexception - Why
Comparing Float/Double Values Using == Operator
Drawing an Object Using Getgraphics() Without Extending Jframe
What Does an Assignment Expression Evaluate to in Java
Running Multiple Launch Configurations at Once
"Int Cannot Be Dereferenced" in Java
Java Date - Insert into Database
How to Make My Swingworker Example Work Properly
Bidirectional Multi-Valued Map in Java
Why Does List<String>.Toarray() Return Object[] and Not String[]? How to Work Around This
Drawing in Jlayeredpane Over Exising JPAnels