Run .Exe File in Java from File Location

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



Leave a reply



Submit