4

How can I create a .lnk file using Java? such as I want to create a .lnk file on my desktop which opens the following directory C:\Windows\System32\calc.exe. I have found this website but it is for creating an URL for the website shortcut. it basically write out 2 lines ([InternetShortcut] and URL = XXXXXX.com) by using FileWriter and save it as .URL file. but it seems not working with lnk extension.

Tiny
  • 27,221
  • 105
  • 339
  • 599
pa pa
  • 43
  • 3
  • I was going to say it would be a fun exercise to build your own .link in binary and write it out to that directory. But there is a whole lot more to .lnk files than I would ever have expected. – JoeManiaci Sep 03 '14 at 19:36
  • http://msdn.microsoft.com/en-us/library/dd871305.aspx – Kevin Mangold Sep 03 '14 at 19:38
  • 2
    See [Creating a shortcut file from Java](http://stackoverflow.com/questions/13145942/creating-a-shortcut-file-from-java) – DavidPostill Sep 03 '14 at 19:39
  • Whatever the working method you'll find, it obviously won't be portable... – JSlain Sep 03 '14 at 19:55
  • Faced with the same problem, I ended up using [this program](http://ss64.com/nt/shortcut.html) embedded in the jar file which I copy to the user's home directory at runtime and call with the appropriate parameters. – schmop Sep 03 '14 at 19:56

1 Answers1

1

You can use a *.symlink file. It works too, and it is simple to create.

Here is a short Java snippet to create a *.symlink file:

// Link target
Path targetPath = Path.of("C:/Windows/System32/calc.exe");
// Link destination
// You can replace Calculator with your own file name (without extension).
Path linkPath = Path.of(System.getProperty("user.home") + "/Desktop/Calculator");
Files.createSymbolicLink(linkPath, targetPath);