Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

java - create link on desktop to jar

I want to create a link, like most applications have, to a jar in another location. Just like in applications like word. I think it is called symbolic link, but I am not sure.

I managed to do that by using Files.createSymbolicLink(), but I didn't find a way yet to set the image of it, and if you try to edit it yourself most of the buttons are locked, other than in normal links. Is there a better way to do it, maybe by using a library? If it doesn't work in Java, which language do I need?

question from:https://stackoverflow.com/questions/65894621/create-link-on-desktop-to-jar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You got two possibilities here regarding file linking: Symlinks(soft links) and Hard Links. This last one may be the one you need for your specific case.

  1. Soft Links These are just pointers to the original files, just as when you create a shortcut of a file/program in desktop while working on Windows, for example. If the original gets deleted, the symlink gets deleted too (or as in WS, just becomes useless).

  2. Hard Links These are clones of the original files, which also reflect the changes made on the original one. Same thing happens if changes are made on one of the hard links: the changes will be reflected on the original and also on all other hard links from the same origin. The hard links do not get deleted once the original file does, acting as support elements for backup operations.


Programmatically creating links

Example:

Path symlink = Paths.get("/home/jars/symlinkToJar.jar");
if (Files.exists(symlink)) 
   Files.delete(symlink);
/*Soft link-Symlink*/
Files.createSymbolicLink(symlink, Paths.get("/opt/jars/yourJar.jar"));

This will first make sure the target file doesn't exist, and creates a symlink in /home/jars/symlinkToJar.jar that behaves as a soft link to the original file /jars/yourJar.jar. This link contents are not equal to the original file's, as a Symlink is just a pointer.

symlinkToJar.jar(2KB)* ----> yourJar.jar(20KB)


  • Hard links are created by calling createLink. These are not just pointers, but complete byte to byte clones.

Example:

Path link = Paths.get("/home/someGui/Desktop/linkToJar.jar");
if (Files.exists(link)) 
    Files.delete(link);
/*Hard link*/
Files.createLink(link, Paths.get("/opt/jars/yourJar.jar"));

This will first make sure the target file doesn't exist, and creates a link in /Desktop/linkToJar.jar that behaves as a hard link to the original file opt/jars/yourJar.jar.

This link is indeed a mirror copy, so every operation you could perform on the original one should also be possible on this one, as they are the same exact clone of each other.

linkToJar.jar(20KB)* <----> yourJar.jar(20KB)


I'd suggest creating not a Symlink but instead a Hard Link to your original file, as your commands may involve accessing the file content itself, or they may assume the given path is a file (and not a pointer).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...