Creating a shortcut file from Java
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 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:
Code:
import java.io.*;
public class WindowsUtils {
private WindowsUtils() { }
// see note
private static final String WINDOWS_DESKTOP = "Desktop";
/**
* the current user desktop path
* @return the current user desktop path
*/
public static String getWindowsCurrentUserDesktopPath() {
return System.getenv("userprofile") + "/" + WINDOWS_DESKTOP ;
}
/**
* Create an Internet shortcut on User's Desktop no icon specified
* @param name name of the shortcut
* @param target URL*
* @throws IOException
*/
public static void createInternetShortcutOnDesktop(String name, String target)*
throws IOException*
{
String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
createInternetShortcut(name, path, target, "");
}
/**
* Create an Internet shortcut on User's Desktop, icon specified
* @param name name of the shortcut*
* @param target URL
* @param icon URL (ex. http://www.server.com/favicon.ico)
* @throws IOException
*/
public static void createInternetShortcutOnDesktop
(String name, String target, String icon)*
throws IOException
{
String path = getWindowsCurrentUserDesktopPath() + "/"+ name + ".URL";
createInternetShortcut(name, path, target, icon);
}
/**
* Create an Internet shortcut
* @param name name of the shortcut
* @param where location of the shortcut
* @param target URL*
* @param icon URL (ex. http://www.server.com/favicon.ico)
* @throws IOException
*/
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();
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
WindowsUtils.createInternetShortcutOnDesktop
("GOOGLE", "http://www.google.com/");
}
}
Source: Create an Internet Shortcut (Windows) - Real's Java How-to
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.
EDIT: Why is the code displaying wrongly?
Re: Creating a shortcut file from Java
Here's the approach I would take:
1. Write out a VBScript file using createTempFile(...)
2. Set the file to deleteOnExit().
3. Execute the VBScript with appropriate parameters.
I would stick the creation of the VBS file in a constructor or a static initializer, and the creation of the Windows shortcut in an instance or static method respectively; possibly two methods, one which creates the shortcut in a folder of choice and the another which defaults to the desktop.
If you go with this approach, you can probably find a VBScript code on the net or sign up to a VBScript forum (should be one on the Microsoft site) for help. The VBS will involve the "WScript.Shell" object.
db
edit There's an example of this approach for solving a different problem here: Find windows special folders (Java in General forum at JavaRanch)
Re: Creating a shortcut file from Java
Thanks for the reply. Are there any workarounds, since I don't know how to write a VBScript file?
Re: Creating a shortcut file from Java
I've already given you an example from another forum thread and suggested you ask in a Microsoft support area.
If you just don't want to learn how to create a VBScript file, then why don't you drop the requirement altogether?
db