23

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

6 Answers 6

29

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\\"));
Sign up to request clarification or add additional context in comments.

4 Comments

Java will correctly interpret / as a file separator on windows -- at least on windows 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.
@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, 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/…
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();

6 Comments

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
If possible can you give the Runtime.exec example.. with respect to my code.. thanks in advance..
Thanks Kuldeep, I have also tried ProcessBuilder.. I am getting the same problem..
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..
@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).
|
11

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

Comments

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.

2 Comments

Is it synchronous? I.e. does java wait for the exe file to terminate before continuing its execution?
@SebastianNielsen, the ProcessBuilder class is not synchronized. docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
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: &&

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.