Results 1 to 4 of 4
  1. #1
    MemoNick is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default 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:

    Java 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:

    1. I couldn't change the icon, despite the path linking it to a correct icon.
    2. 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?
    Last edited by MemoNick; 10-30-2012 at 10:49 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default 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)
    Last edited by DarrylBurke; 10-30-2012 at 03:32 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    MemoNick is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default 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?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. gettext binary MO file creating with Java
    By Mortein79 in forum Advanced Java
    Replies: 4
    Last Post: 02-08-2012, 01:34 PM
  2. Replies: 1
    Last Post: 11-27-2011, 04:25 AM
  3. Creating a file is Holding up java.
    By rizowski in forum New To Java
    Replies: 0
    Last Post: 04-20-2011, 05:47 PM
  4. Replies: 1
    Last Post: 05-17-2010, 09:03 PM
  5. Replies: 0
    Last Post: 08-18-2009, 04:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •