Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a shortcut file from Java

Tags:

java

windows

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.

like image 420
MemoNick Avatar asked Oct 30 '12 19:10

MemoNick


People also ask

How do I create a shortcut for a file?

Create a desktop shortcut for an Office document or file In Windows Explorer, browse to the document or file for which you want to create a desktop shortcut. Right-click the name of the document, and then click Create shortcut.

How do I create a shortcut to a file in Java?

File shortcut = ...; String pathToExistingFile = new ShellLink(shortcut). resolveTarget(); ShellLink sl = ...; sl. setIconLocation("/path/to/icon/file");


2 Answers

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

like image 112
Josua Frank Avatar answered Sep 22 '22 22:09

Josua Frank


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:
https://github.com/JacksonBrienen/VBS-Shortcut/blob/main/src/vbs_sc/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");
like image 28
Jackson Brienen Avatar answered Sep 19 '22 22:09

Jackson Brienen