22

I have to open a .exe file from my Java program. So I tried following code First.

Process process = runtime.exec("c:\\program files\\test\\test.exe");

But I was getting some error. Then I found out that the exe has to be launched from that location that is c://program files/test/ only then it will open with out errors. So I decided to write a .bat file and execute so that it will cd to that location and execute the .exe file.

Following is my code:

BufferedWriter fileOut;

String itsFileLocation = "c:\\program files\\test\\"
    System.out.println(itsFileLocation);
    try {
     fileOut = new BufferedWriter(new FileWriter("C:\\test.bat"));
     fileOut.write("cd\\"+"\n");
     fileOut.write("cd "+ itsFileLocation +"\n");
     fileOut.write("test.exe"+"\n");
     fileOut.write("exit"+"\n");
     
     fileOut.close(); // Close the output stream after all output is done.
    } catch (IOException e1) {
     e1.printStackTrace();
    } // Create the Buffered Writer object to write to a file called filename.txt
    Runtime runtime = Runtime.getRuntime();
    try {
     Process process =runtime.exec("cmd /c start C:\\test.bat");
    } catch (IOException e) {
     e.printStackTrace();
    }

The above code works perfectly. However, the command prompt is also opened at the back of my .exe (Application). It closes only after the .exe file exits..

I need to clse my command prompt when my application stats.

My .bat file will be like following after it is written by the program.

cd\
cd C:\Program Files\test\
test.exe
exit
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76

6 Answers6

28

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\\"));
Schifty
  • 625
  • 1
  • 7
  • 17
  • Java will correctly interpret `/` as a file separator on windows -- at least on windows 7. – Dunes May 21 '12 at 13:42
  • 7
    @schifty: In fact we do not need to give full path of `test.exe` in the first parameter of `exec(String command, String[] envp, File dir)`. So essentially `Runtime.getRuntime().exec("test.exe", null, new File("c:\\program files\\test\\"));` is good enough. – Kuldeep Jain May 22 '12 at 06:44
  • 1
    @KuldeepJain That's not entirely true and a bit misleading. If the exe is in `PATH` then it's true full path is not needed, but otherwise, it is. – Nom1fan Sep 08 '18 at 15:09
  • @Nom1fan, Looks like you did not follow my comment well. The path can be specified in the 3rd parameter. I mentioned to not give full path in 1st parameter. Please see my example I gave in comment. You can also read further at: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],%20java.io.File) – Kuldeep Jain Sep 09 '18 at 16:27
13

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();
Kuldeep Jain
  • 8,409
  • 8
  • 48
  • 73
  • Thanks Kuldeep I have already tried this.. it is not working I am getting the same error. It is expecting me to be in the same directory of .exe file when I launch the .exe file – Dilip Rajkumar May 21 '12 at 13:19
  • If possible can you give the Runtime.exec example.. with respect to my code.. thanks in advance.. – Dilip Rajkumar May 21 '12 at 13:20
  • Thanks Kuldeep, I have also tried ProcessBuilder.. I am getting the same problem.. – Dilip Rajkumar May 21 '12 at 13:21
  • The .exe files need some property file from that location to start.. I can do one thing is I can set that path as env variable.. I am not sure will that work.. – Dilip Rajkumar May 21 '12 at 13:22
  • 1
    @DilipRajkumar What error do you get when you try this solution? This solution is correct way to do what you want, and will not produce the `cmd.exe` window you wish to avoid (nor will it produce temporary files on your system). – Dunes May 21 '12 at 13:36
  • @DilipRajkumar: Here is the example where `eclipse.exe` is present in *C:\Kuldeep\eclipse3.6* directory: `Runtime.getRuntime().exec("C:\\Kuldeep\\eclipse3.6\\eclipse.exe");` and `Runtime.getRuntime().exec("eclipse.exe", null, new File("C:\\Kuldeep\\eclipse3.6"));` both works fine. What issue are you facing? – Kuldeep Jain May 21 '12 at 14:06
8

Another way of running a file is the following:

import java.awt.Desktop;
import java.io.File;

public static void open(String targetFilePath) throws IOException
{
    Desktop desktop = Desktop.getDesktop();

    desktop.open(new File(targetFilePath));
}
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
5

The the Standard Code for Running bat or any other through command line using java is :

runtimeProcess = Runtime.getRuntime().exec("cmd /c start cmd.exe /C\""+backup_path+"\"");
int processComplete = runtimeProcess.waitFor();

and you can go on continue for multiple files using & separator like: &&

Shaikh Arbaaz
  • 155
  • 1
  • 4
4

This would also work.

 Process process = new ProcessBuilder("C:\\Users\\test\\Downloads\\Termius.exe").start();

It would start the .exe in that file location.

Ifesinachi Bryan
  • 2,240
  • 1
  • 19
  • 20
0

Best way to run exe file

make java.awt.Desktop object and equal Desktop.getDesktop();

Desktop desktop = Desktop.getDesktop(); desktop.open("file path");

run exe file:

desktop.open("C:\\Windows\\System32\\cmd.exe");

or

desktop.open("C:/Windows/System32/cmd.exe");

run url :

desktop.browse(new URI("http://www.google.com"));