0

I wrote an updater for my Java application which downloads its newest jar-file online, replaces the shortcut to it before starting the new jar and finally deleting itself.

I used the following code to create the shortcut:

try {
    //Location of shortcut -> Working
    String address = "C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\App.lnk";

    //Delete old shortcut -> Not working
    File f = new File(address);
    f.delete();

    //Create new shortcut
    FileWriter fw = new FileWriter(address);
    fw.write("[Program]\n"); //Probably wrong section but cannot find real one
    fw.write("FILE=" + (new File(App.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath()) + "App-"+version+".jar\n"); //Shortcut to newest version
    fw.flush();
    fw.close();
} catch (URISyntaxException e) {e.printStackTrace();}

The code does create a file but it seems to be broken so my question is what am I doing wrong here?

Hering
  • 43
  • 1
  • 1
  • 7
  • a `.lnk` file is **not** a text file - it's binary. Please look for examples of creating one properly. There are plenty of examples of creating them in C/C++. – Anya Shenanigans Mar 13 '16 at 19:18
  • Unfortunately that was the only source I found in Java so I tried that one... I will search for some examples in C++/C and try to convert them. Thank you. – Hering Mar 13 '16 at 19:35

1 Answers1

1

This is how it works:

ShellLink shortcut = ShellLink.createLink("App.jar").setWorkingDir(new File(".").getAbsolutePath());
shortcut.getHeader().getLinkFlags().setAllowLinkToLink();
shortcut.saveTo("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\App.lnk");
Hering
  • 43
  • 1
  • 1
  • 7