Search code examples
javashortcutexecute

Executing external program from shortcut


I'm trying to open a program from my desktop within java. Some of my desktop icon properties have a *.exe extension, but other program shortcuts have a *.lnk extension. As this will open a program with param being the location of an *.exe:

try {
        p = Runtime.getRuntime().exec(params);
        int result = p.waitFor();

        if (result != 0) {
            System.out.println("Process failed with status: " + result);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

this doesn't work for any other shortcut with a different extension.

Is there a way I can target the proper application the shortcut is pointing to and open it?


Solution

  • You can use java.awt.Desktop ; Desktop.getDesktop().open(new File("*.lnk"));