As a java developer, at times you may face a situation where you need to create a desktop shortcut on Windows, from a java application to launch another jar file.
Such a particular and especial situation is when you are creating a desktop application which will be installed through java web start and hence you can not use a third party software to create a setup.exe which will have no use here.
So you need a java application that will create a desktop shortcut, so that a double click on the shortcut will launch the jar file present in an installation directory in that system.
There is a JNI library (jshortcut-0.4-oberzalek https://github.com/jimmc/jshortcut/downloads ) using which you can do this task .
Here is a sample code for creating a shortcut to IE.
import net.jimmc.jshortcut.JShellLink;
public class Sc {
JShellLink link;
String filePath;
public Sc() {
try {
link = new JShellLink();
filePath = JShellLink.getDirectory(“”) + “C:\\Program Files\\Internet Explorer\\iexplore.exe”;
} catch (Exception e) {
}
}
public void createDesktopShortcut() {
try {
link.setFolder(JShellLink.getDirectory(“desktop”));
link.setName(“ie”);
link.setPath(filePath);
link.save();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String a[]) {
Sc sc = new Sc();
sc.createDesktopShortcut();
}
}
You can customize it to do your job, i.e create a shortcut to your jar file, by providing path to your jar file, an icon for the shortcut image etc