1

I've searched a lot about this, but I did't find it in Java. I want to create a shortcut on desktop when run the JAR for the first time without using plugins. I wanna a native way to generate a shortcut when the first time.

I wanna create a txt file just to know whether it's the first time, but the difficult is generate the shortcut.

Teste
  • 661
  • 1
  • 6
  • 7
  • http://stackoverflow.com/questions/343776/how-do-you-create-and-read-windows-shortcut-lnk-file-in-java might help – seenukarthi Apr 03 '16 at 03:38
  • Step 1: do this http://stackoverflow.com/a/10226697/1897935 . Step 2: check if file exists, else create it. – Srinath Ganesh Apr 03 '16 at 03:42
  • [http://stackoverflow.com/questions/1496343/how-to-create-shortcut-icon-for-java-program](http://stackoverflow.com/questions/1496343/how-to-create-shortcut-icon-for-java-program) – stark9000 Apr 03 '16 at 03:45
  • Shortcuts (under Windows) are created through a COM API, you'll need a jni/jna based solution which can interact with this API for you. Maybe something like [this](https://groups.google.com/forum/m/#!topic/jna-users/Zgkx7XQAJYc) – MadProgrammer Apr 03 '16 at 04:53
  • Is this question a duplicate of the question marked? I don't think so. – seenukarthi Apr 03 '16 at 06:01

1 Answers1

1

you can do this by creating a vb script and execute it by java:

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;



public class Shourtcut {


    public static void main(String[] args) {
        String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "shourtcut.jar";
        path = '"' + path + '"';
        try {
            File file = File.createTempFile("shortcut_geni", ".vbs");
            file.deleteOnExit();
            try (FileWriter fw = new java.io.FileWriter(file)) {
                String vbs = "Set oWS = WScript.CreateObject(\"WScript.Shell\")  \n"
                        + "sLinkFile = oWS.ExpandEnvironmentStrings(\"%HOMEDRIVE%%HOMEPATH%\\Desktop\\jar_file_name.lnk\")\n"
                        + "Set oLink = oWS.CreateShortcut(sLinkFile)\n "
                        + "oLink.TargetPath = oWS.ExpandEnvironmentStrings(" + path + ")\n"
                        + "oLink.Save \n";
                fw.write(vbs);
            }
            Process p = Runtime.getRuntime().exec("wscript " + file.getPath());
            p.waitFor();

        } catch (IOException | InterruptedException e) {
            System.out.println("" + e);
        }

    }

}
stark9000
  • 71
  • 7
  • didn't add code to check if its first time run. here's the link to net beans project of shortcut create jar : http://www.mediafire.com/download/5lhn3vrwe3144l8/shourtcut.7z – stark9000 Apr 03 '16 at 04:59