Sharonneke95
Sharonneke95

Reputation: 51

How to run .exe file in java

I know it has been asked before, but none of those answers seem to work for me. I am trying to get a .exe file running in a java program. The following code (that I plucked from Internet) works; Notepad starts.

import java.io.IOException;

public class start {
   public static void main(String args[])
   {
       try {
           Process p = Runtime.getRuntime().exec(new String[] {"C:\\Windows\\System32\\notepad.exe"});
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

}

But when I change the folder to the one that contains my own .exe file, it doesn't do anything. It doesn't even give an error. It just starts and terminates. If I double-click the file in the folder itself, it just opens and runs, so the file itself works.

So, just to be clear, I changed Process p to

Process p = Runtime.getRuntime().exec(new String[] {"C:\\Users\\Sharonneke\\Documents\\IntraFace-v1.2\\x64\\Release\\IntraFaceTracker.exe"});

Why won't this work and how do I fix it?

Update: So I don't have to use the new String [] but that doesn't solve the problem. Also, using the ProcessBuilder (like kage0x3b said in the answer section) gives the error: "The constructor ProcessBuilder(String) is undefined" while it apparently should work like that :(

Upvotes: 1

Views: 5416

Answers (3)

Sharonneke95
Sharonneke95

Reputation: 51

Thank you all for your help, but unfortunately none of your answers worked. I managed to find something that runs my code well enough (according to my supervisor, that is) so I'm happy. This is what I'm using right now:

Runtime.getRuntime().exec("C:\\Users\\Sharonneke\\Documents\\IntraFace-v1.2\\x64\\Release\\IntraFaceTracker.exe", null, new File("C:\\Users\\Sharonneke\\Documents\\IntraFace-v1.2\\x64\\Release\\"));

For some reason this works even though it wouldn't when I used it like before, but I decided to not ask questions anymore. It works :) Thanks again for taking your time to try and help me!

Upvotes: 0

You dont need an array for only one application to be run...

just do:

Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\notepad.exe");

and add the respective try catch block


using a processBuilder

ProcessBuilder p = new ProcessBuilder("C:\\Windows\\System32\\notepad.exe");
p.start();

Upvotes: 2

Kage0x3B
Kage0x3B

Reputation: 93

Maybe there is a problem with the working directory of the program if it tries to load files from the working directory which obviously works if clicked but I think not when executed from Java code if you do not set it. Try using a ProcessBuilder and then setting the working directory:

    File file = new File("C:\\Users\\Sharonneke\\Documents\\IntraFace-v1.2\\x64\\Release\\IntraFaceTracker.exe");
    ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
    processBuilder.directory(file.getParentFile());

    try {
        processBuilder.start();
    } catch(IOException ex) {
        ex.printStackTrace();
    }

Upvotes: 2

Related Questions