0

The code compiles OK, but when I press the button, nothing happens, would you know why? I want to call the EXIF software when I press my EXIF

EXIF.addActionListener (new ActionListener(){
    public void actionPerformed (ActionEvent e) {
        try {     
            Runtime.getRuntime().exec("C:\\Program Files (x86)\\Exif Pilot\\ExifPilot.exe");  
        } catch(Exception exc) {
            /*handle exception*/
        }          
    }
});
River
  • 8,585
  • 14
  • 54
  • 67
learnerNo1
  • 147
  • 1
  • 5
  • 23
  • _"nothing happens"_, actually your `catch` block silently handles any exception that is raised by `exec(..)` so maybe something is happening but you are ignoring it. – Jack Jun 21 '16 at 12:16
  • do you print the stacktrace? what does handle exception do actually?? or it is just a comment as we see it here? – Apostolos Jun 21 '16 at 12:16
  • No, I am just starting to code my GUI, I wanted to call another program. Sorry, I am just starting with this – learnerNo1 Jun 21 '16 at 12:18
  • No, you shouldn't remove your catch block but if you don't do anything in the catch block and an exception is raised, the effect from your point of view is that nothing happened, while it's not true. You should at least place an `e.printStackTrace()` inside the catch block to check if something is going wrong and what. – Jack Jun 21 '16 at 12:20
  • ok exc.printStackTrace(); first and then tell us if an exception is thrown – Apostolos Jun 21 '16 at 12:20
  • Thank you. Now I have a few exceptions....... Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation – learnerNo1 Jun 21 '16 at 12:23
  • please look at http://stackoverflow.com/questions/5853529/createprocess-error-740-the-requested-operation-requires-elevation looks like the issue is about administrator privileges – user3136131 Jun 21 '16 at 12:37
  • Thank you, I am studying the link, I right click my program and I increased the permissions to everything, still, it does not let me run the software. I was thinking to simplify the program and with the button, take the user to where the file is located and run from there. – learnerNo1 Jun 21 '16 at 14:04

1 Answers1

1

The problem is with the executed process output/input streams. When you execute a new process with java it allocates only 8KB for its input stream so you must consume it. Search for process gobblers

Also with windows use cmd /c to execute the program or better:

String[] cmd = {"CMD", "/C", "C:\\Program Files (x86)\\Exif Pilot\\ExifPilot.exe"};

ProcessBuilder processBuilder = new ProcessBuilder(command);

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((String line = br.readLine()) != null) {
    System.out.println(line);
}

try {
    int exitValue = process.waitFor();
    //TODO - do something with the exit value
} catch (InterruptedException e) {
    e.printStackTrace();
}
shachar
  • 641
  • 5
  • 12