Create Desktop shortcut

2020-02-14 05:51发布

问题:

I am working on a java application.

I want to create desktop shortcut of my application's Exe file.

Is is possible to do it from my application itself ? Or user has to do it manually by right clicking ?

回答1:

package farzi;

import net.jimmc.jshortcut.JShellLink;

public class Sc {
    JShellLink link;
    String filePath;

    public Sc() {
        try {
            link = new JShellLink();
            filePath = JShellLink.getDirectory("")
                + "C:\\Program Files\\Internet Explorer\\iexplore.exe";

        } catch (Exception e) {

        }

    }

    public void createDesktopShortcut() {

        try {
            link.setFolder(JShellLink.getDirectory("desktop"));
            link.setName("ie");
            link.setPath(filePath);
            link.save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String a[]) {
        Sc sc = new Sc();
        sc.createDesktopShortcut();
    }
}

you can get the jar from here



回答2:

Java Web Start does this. You write a regular application with a regular main method, and point to it with a short XML file with a .jnlp extension. When the user clicks the link to the XML file, Java Web Start will, among other things, create the shortcut, if your XML file contains this:

<information>
    <!-- Other elements go here -->
    <shortcut>
        <desktop/>
    </shortcut>
</information>

More details are here.



回答3:

This is a fairly old topic, nevertheless I came across Austin`s brilliant answer and encountered some problems using jshortcut from within NetBeans, but I fear this might also affect Ecclipse.

  1. Adding jshortcut.dll to java.library.path from wihtin NetBeans using properties->Run->VM options did not work.
  2. Adding this dll by source code like "System.setProperty("java.library.path", yourDllPath)" does not work either.
  3. Instead put jshortcut.dll to that particular folder where you store your jars for this project. JShellLink looks at this folder on its own, so you dont need to add the DLL`s path to any path, which could cause trouble when running on some closed-down machines.
  4. Most Important: When downloading your jshortcut.dll- Version, use AMD for all actual Intel machines. Rename the dll to shortcut.dll after downloading. JShellLink only looks for shortcut.dll, it ignores all other names.

Excert form jShellLink: /** Provide access to shortcuts (shell links) from Java. * * The native library (jshortcut.dll) is loaded when JShellLink is first * loaded. * By default, JShellLink first looks for the native library in the PATH, * using System.loadLibrary. * If the native library is not found in the PATH, * JShellLink then looks through each directory in the CLASSPATH * (as determined by the value of the system property java.class.path). * If an entry in the CLASSPATH is a jar file, * then JShellLink looks for the native library * in the directory containing that jar file. * The application can override this behavior and force JShellLink to look * for the native library in a specific directory by setting the system * property JSHORTCUT_HOME to point to that directory. * This property must be set before the JShellLink class is loaded. * This makes it possible to use this library from a self-extracting jar file. */