Java code to run .exe shortcuts
ioexception, java, process, runtime.exec
Solution
The shortcut you see in your desktop is actually a file with the extension `.lnk`. It's real full path is, then:
C:\Users\Desktop\notepad.exe.lnk
Trying to run it through `exec()` will yield a "CreateProcess error ... is not a valid Win32 application" error.
Fortunately, you can run those as well through the `ProcessBuilder` utility class.
public static void main(String[] args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c",
"C:\\Users\\robert\\Desktop\\notepad.lnk");
Process p = pb.start();
p.waitFor();
}
If you must use `Runtime.getRuntime().exec()`, you can open the `lnk` file through `rundll32`:
Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " +
"C:\\Users\\robert\\Desktop\\notepad.lnk");
p.waitFor(); // watch out
But keep in mind, by this approach, the `p.waitFor();` and similar method calls may not have the expected result: As you can see, the created process is the `rundll32`, not the shortcut's (`notepad.exe`).
Problem
Is there any way that I can open notepad or other application from shortcuts? Here is my code: ``` import java.io.File; import java.io.IOException; public class acrobat { public static void main(String[] args) throws IOException, InterruptedException { String[] notepad = {"C:\\Users\\Desktop\\notepad.lnk"}; Process p = Runtime.getRuntime().exec(notepad); p.waitFor(); } } ``` I want to open application from shortcut, but I am getting error.. ``` Exception in thread "main" java.io.IOException: Cannot run program "C:\Users\robert\Desktop\notepad.lnk": CreateProcess error=193, %1 is not a valid Win32 application at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at acrobat.main(acrobat.java:11) Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 4 more ``` If I only write notepad.exe than its working but, with path its not working. Is there any way that I can open with shortcuts?