10

One problem which has been puzzling me for weeks now is how I can create a shortcut file from Java. Now before I say anything else, I have looked all over Google (and also on this site; including this: Creating shortcut links (.lnk) from Java) trying to find something helpful. What I need isn't an installer package which creates a shortcut, but to create a shortcut from code. What I mean by shortcut is a .lnk file which are usually found on the Desktop.

One of the helpful things I found was this program:

Java Code:

import java.io.*;       
public class WindowsUtils {     
     private WindowsUtils() { }
     private static final String WINDOWS_DESKTOP = "Desktop";
     public static String getWindowsCurrentUserDesktopPath() { //return the current user desktop path
         return System.getenv("userprofile") + "/" + WINDOWS_DESKTOP ;
     }
     public static void createInternetShortcutOnDesktop(String name, String target) throws IOException {
         String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
         createInternetShortcut(name, path, target, "");
     }
     public static void createInternetShortcutOnDesktop(String name, String target, String icon) throws IOException {
         String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
         createInternetShortcut(name, path, target, icon);
     }
     public static void createInternetShortcut(String name, String where, String target, String icon) throws IOException {
         FileWriter fw = new FileWriter(where);
         fw.write("[InternetShortcut]\n");
         fw.write("URL=" + target + "\n");
         if (!icon.equals("")) {
             fw.write("IconFile=" + icon + "\n");*
         }
         fw.flush();
         fw.close();
     }
     public static void main(String[] args) throws IOException {
         WindowsUtils.createInternetShortcutOnDesktop("GOOGLE", "http://www.google.com/");
     }
}

I toyed around with it, and managed to create a .lnk shortcut on my desktop. However, I foudn two problems:

I couldn't change the icon, despite the path linking it to a correct icon. I made a path which led me to C:/Users/USER/Documents, however, whenever I clicked the shortcut it took me to C:/. When I delete the shortcut, the dialogue shows me indeed that the path is file:////C:/Users/USER/Documents.

I know that this code above was originally meant to create Internet Shortcuts, so I believe I might have taken the wrong approach. I would appreciate any help/links you can give me.

Community
  • 1
  • 1
MemoNick
  • 501
  • 2
  • 7
  • 20

3 Answers3

14

I can recommend this repository on GitHub:

https://github.com/BlackOverlord666/mslinks

There I've found a simple solution to create shortcuts:

ShellLink.createLink("path/to/existing/file.txt", "path/to/the/future/shortcut.lnk");

If you want to read shortcuts:

File shortcut = ...;
String pathToExistingFile = new ShellLink(shortcut).resolveTarget();

If you want to change the icon of the shortcut, use:

ShellLink sl = ...;
sl.setIconLocation("/path/to/icon/file");

Hope this helps you :)

Kind regards

Josua Frank

Josua Frank
  • 331
  • 3
  • 6
1

I know I am a bit late to the Party, I really like the JShellLink implementations shown up above, but if you need something a bit simpler, then what I have written:
ShortcutFactory.java
should do the trick. To sum it up, it is a simple shortcut creator, that uses VBS run via CMD.
Once using this, to create a desktop shortcut would be as easy as:

ShortcutFactory.createDesktopShortcut("C:/Program Files/Internet Explorer/iexplore.exe", "Shortcut.lnk");
  • Thank you, Jackson, for your code. I just tested it and it works great. I dare to submit the request for an enhancement: Could you add the possibility to set a user specified icon to the link, please? – Jörg Mar 27 '23 at 19:46
  • Although having no knowlege of VBS at all, I found a way to add an icon
    - add a 3rd parameter `String iconPath` to `createShortcut(String source, String linkPath)` 
    - insert a new line `+ "\tscObj.IconLocation = \"%s\"%n"` after `+ "\tscObj.TargetPath = \"%s\"%n"`
    - append `, icon` after `linkPath, source`
    – Jörg Mar 28 '23 at 10:12
  • Hey @Jörg I have added to the github page, and created the release binaries for it. If you have any questions please let me know. – Jackson Brienen Mar 29 '23 at 23:25
  • Wonderful. Thank you again @Jackson. Since everything works fine, I have currently no question, but thank you for your offer as well. You could, however, update the line `Process p = Runtime.getRuntime() ...` which is deprecated by java20 in the current form. – Jörg Apr 03 '23 at 06:17
  • Just pass the command as a String array and everything is fine. – Jörg Apr 03 '23 at 06:25
0

http://www.mindfiresolutions.com/Creating-shortcut-from-a-Java-Application-1712.php

A nice and simple tutorial to use a jni using library called jShellLink

Anuswadh
  • 542
  • 1
  • 11
  • 19