Results 1 to 4 of 4
- 10-30-2012, 10:45 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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:
Source: Create an Internet Shortcut (Windows) - Real's Java How-toJava 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/"); } }
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?Last edited by MemoNick; 10-30-2012 at 10:49 AM.
- 10-30-2012, 03:25 PM #2
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.
Why do they call it rush hour when nothing moves? - Robin Williams
- 10-30-2012, 07:57 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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?
- 10-30-2012, 08:18 PM #4
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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
gettext binary MO file creating with Java
By Mortein79 in forum Advanced JavaReplies: 4Last Post: 02-08-2012, 01:34 PM -
Creating a picture in java out of a txt file containing 0's and 1's
By The Dark Dragon in forum New To JavaReplies: 1Last Post: 11-27-2011, 04:25 AM -
Creating a file is Holding up java.
By rizowski in forum New To JavaReplies: 0Last Post: 04-20-2011, 05:47 PM -
Launch Java application with root privileges from gnome desktop shortcut.
By IYIaster in forum New To JavaReplies: 1Last Post: 05-17-2010, 09:03 PM -
Anyone know how to create a URL desktop shortcut from java?
By IYIaster in forum New To JavaReplies: 0Last Post: 08-18-2009, 04:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks