1

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

Sharonneke95
  • 51
  • 2
  • 9
  • Can you try running the file from the command line to make sure that works? Press [Windows Key]+R, type "cmd", hit enter, copy the full path to the exe in the cmd window, and see if it runs or gives you relevant error information – John Biddle May 09 '16 at 18:55
  • It gives an error indeed: | Error opening model file "DetectionModel-v1.5.bin"! FaceAlignment cannot be initialized. – Sharonneke95 May 09 '16 at 19:02
  • Check if you have to run it as an administrator – John Biddle May 09 '16 at 19:07
  • By right-clicking it and then clicking "run as administrator"? That just works. But normally double-clicking worked as well. – Sharonneke95 May 09 '16 at 19:12
  • It doesn't work. I still get the same error. – Sharonneke95 May 09 '16 at 20:00
  • @Sharonneke95 so what is "DetectionModel-v1.5.bin"? How is that supposed to be fed to the program? – eis May 09 '16 at 20:06

3 Answers3

2

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();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

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();
    }
Kage0x3B
  • 93
  • 6
  • You beat me to it. OP's question looks a lot like http://stackoverflow.com/questions/6811522/changing-the-working-directory-of-command-from-java – cf- May 09 '16 at 19:19
  • I cannot run it because of this error: "The constructor ProcessBuilder(String) is undefined" – Sharonneke95 May 09 '16 at 19:21
  • Maybe you are using the wrong ProcessBuilder class (it should be java.lang.ProcessBuilder as ΦXoce 웃 Пepeúpa said. There certainly is a constructor with a String argument. – Kage0x3B May 09 '16 at 19:39
  • I am calling the right class apparently. I'll restart my computer once. That sometimes works when everything else fails, right? Haha – Sharonneke95 May 09 '16 at 19:52
0

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!

Sharonneke95
  • 51
  • 2
  • 9
  • Why are you creating new file directory in your code? –  Jun 16 '16 at 09:33
  • I don't know. It worked so I was happy. I found it somewhere else online :3 – Sharonneke95 Jun 16 '16 at 12:41
  • Ok ! I was also having the same problem. Somehow My `exe` started working. But sometimes it does not being started by java code. –  Jun 16 '16 at 12:59