Run .exe file in Java from file location

112,554

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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: &&

Solution 5

This would also work.

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

It would start the .exe in that file location.

Share:
112,554
Dilip Rajkumar
Author by

Dilip Rajkumar

I like to code in Java, Struts2 and iPhone.... Advanced Web2.0 JS coding.. Very Cool... Happy Coding..:) Cheers

Updated on December 04, 2020

Comments

  • Dilip Rajkumar
    Dilip Rajkumar over 3 years

    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
    
  • Dilip Rajkumar
    Dilip Rajkumar almost 12 years
    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
    Dilip Rajkumar almost 12 years
    If possible can you give the Runtime.exec example.. with respect to my code.. thanks in advance..
  • Dilip Rajkumar
    Dilip Rajkumar almost 12 years
    Thanks Kuldeep, I have also tried ProcessBuilder.. I am getting the same problem..
  • Dilip Rajkumar
    Dilip Rajkumar almost 12 years
    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..
  • Dunes
    Dunes almost 12 years
    @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
    Dunes almost 12 years
    Java will correctly interpret / as a file separator on windows -- at least on windows 7.
  • Kuldeep Jain
    Kuldeep Jain almost 12 years
    @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
    Kuldeep Jain almost 12 years
    @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.
  • Nom1fan
    Nom1fan over 5 years
    @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.
  • Kuldeep Jain
    Kuldeep Jain over 5 years
    @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: docs.oracle.com/javase/7/docs/api/java/lang/…
  • Sebastian Nielsen
    Sebastian Nielsen over 3 years
    Is it synchronous? I.e. does java wait for the exe file to terminate before continuing its execution?
  • Ifesinachi Bryan
    Ifesinachi Bryan over 3 years
    @SebastianNielsen, the ProcessBuilder class is not synchronized. docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.h‌​tml