dmitriishamrikov / mslinks Goto Github PK
View Code? Open in Web Editor NEWJava library for parsing and creating Windows shortcut files (.lnk)
License: Do What The F*ck You Want To Public License
Java library for parsing and creating Windows shortcut files (.lnk)
License: Do What The F*ck You Want To Public License
Hi,
Thanks for this fantastic library. :)
The latest mslinks release fails any dependant project that is built with JDK11, the error is as follows:
[javac] import mslinks.ShellLink;
[javac] ^
[javac] bad class file: /home/runner/work/tray/tray/lib/mslinks-1.0.7.jar(/mslinks/ShellLink.class)
[javac] class file has wrong version 58.0, should be 55.0
[javac] Please remove or make sure it appears in the correct subdirectory of the classpath.
Personally, for historical purposes, I've kept JDK8 level compatible with most of my project. I could probably move permanently to JDK11 but the above suggests JDK13 which will break a lot of projects requiring this library (including my own)
Was this change intended?
at ShellLink.java,while file isn't a mslink , in.close() will not execture, so file handle leak !!!
public ShellLink(InputStream in) throws IOException, ShellLinkException { this(new ByteReader(in)); in.close(); }
Please add a license to this project so it can (or can't) be reused.
Hi!
The latest version of the library that is published here, is not available on Maven central. So, if you can update it, it will be very helpful.
Thank you!
Hi,
It's a very nice project very usefull for me !
There is a little problem with Font, it's LucidaConsole and not LucindaConsole.
Stéphane
Hey,
I've taken the liberty to publish your library to maven central. That means that a binary distribution is now available for public download here and that means again that it will be much easier for devs to use your library in maven.
That's it, no further action is required from you, or almost no action. I would be really glad if you could write something along the lines of the beginning of the readme of my fork in your readme to tell the world about the artifact on maven central.
Thanks for helping me and have a nice weekend,
vatbub
Add a module-info.java file to the source directory for the Java 9 module system.
I don't know which java version this library requires, but adding this file will require at least Java 9.
We need this module file, because without it it's very hard to create jlink images with this library.
The file should look something like this:
module shellinks {
exports mslinks;
exports io;
exports mslinks.data;
exports mslinks.extra;
}
I can create a PR for this if this is fine.
This code worked in 1.0.4, but is broken in 1.0.7:
ShellLink.createLink("C:\\Windows\\notepad.exe", "C:\\Users\\Owner\\Desktop\\Start Notepad.lnk");
Error:
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <<> at index 0: <unknown>
at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232)
at java.base/java.nio.file.Path.of(Path.java:147)
at java.base/java.nio.file.Paths.get(Paths.java:69)
at mslinks.ShellLinkHelper.saveTo(ShellLinkHelper.java:153)
at mslinks.ShellLink.saveTo(ShellLink.java:330)
at mslinks.ShellLink.createLink(ShellLink.java:366)
at LinkBug.main(LinkBug.java:5)
Workaround:
ShellLinkHelper.createLink("C:\\Windows\\notepad.exe", "C:\\Users\\Owner\\Desktop\\Start Notepad.lnk");
Heya DmitriiShamrikov,
I've recently stumbled upon an UnsupportedCLSIDException
for 59031A47-3F72-44A7-89C5-5595FE6B30EE
, when trying to resolve a lnk from a users Desktop pointing towards folders or files on another drive.
My impression was that a Windows lnk could be resolved to its actual target by calling
new ShellLink(file).resolveTarget()
where file
represents the lnk we want to resolve.
If I'm doing this the wrong way, I'd appreciate a pointer in the right direction.
Thanks in advance!
Stay safe and stay awesome.
Cheers,
Griefed
Can i adopt for 1.8 java and pull new branch?
I am trying to update the target on an existing link using this code:
String one = "C:"+File.separator+"Tmp"+File.separator+"1.txt"; // Already exists
String two = "C:"+File.separator+"Tmp"+File.separator+"2.txt"; // Already exists
String link = "C:"+File.separator+"Tmp"+File.separator+"link.txt.lnk";
ShellLink.createLink(one,link);
ShellLink slink = new ShellLink(link);
slink.getLinkInfo().setLocalBasePath(two);
slink.saveTo(link);
At the end of this operation, the link is still pointing to file 1.txt and the program did not generate an error to indicate anything is wrong. I think the code should either update the link or throw an error telling the user the operation did not complete as anticipated.
There is no class "ShellLinkHelper" found in latest version 1.0.6.2 from Maven Center. And it will use "ShellLinkHelper" class in README example.
Nice feature would be an usable Ant-Task. Many many people are searching for a proper solution for creating lnk files with Ant.
can mslinks also be used to create those?
If so please document an example.
Hello,
https://dl.bintray.com is no longer available
[ERROR] Failed to execute goal on project ant: Could not resolve dependencies for project com.wac.core:ant:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.github.vatbub:mslinks:jar:1.0.6.1: Failed to read artifact descriptor for com.github.vatbub:mslinks:jar:1.0.6.1: Could not transfer artifact com.github.vatbub:parentPom:pom:2.0.1-alpha2 from/to bintray-vatbub-fokprojectsReleases (https://dl.bintray.com/vatbub/fokprojectsReleases): Authorization failed for https://dl.bintray.com/vatbub/fokprojectsReleases/com/github/vatbub/parentPom/2.0.1-alpha2/parentPom-2.0.1-alpha2.pom 403 Forbidden -> [Help 1]
In this time we like to use it, but can't at Java compilation 1.7 because Java 1.8 functions or lambdas are used.
Please make a Java 1.7 compiled code version.
Just avoid lambda, method group calls, streams and defaults in interfaces (constants in interfaces are ok)
Then compile it with Java 1.7 SDK.
Tank you @BlackOverlord666
Hi!
While your code works brilliantly on Windows, it would be awesome to get this running on Linux. I've seen plenty of checks and input modifications in ShellLink.java, they're all doomed to fail or raise NPEs in a Linux environment.
Any idea on how to achieve this? You'd somehow offer a way to override all checks and just trust the dev that what he/she puts in is really valid.
Cheers
André
PS: Reason behind: I'd like to create 100s of links out of an oracle database.
When it creates a short cut it doesn't set the shortcut to have the icon. If I right click on the short cut and click on "change icon...", the path I set pointing to the icon is there, as well as the icon. Clicking OK sets it. Is there a way to have it create the icon with the image set from the start?
Hi there! I think it would be great if mslinks was available in mavencentral. I've got a template that I use to auto-upload to bintray and mavencentral, add the javadocs to gh-pages, etc. It would take me about an hour for me to put my fork/ up as "com.diffplug.mslinks".
If you'd like, I can apply the template to your project and you can upload it yourself under your own prefix.
Here's what a project looks like under the template:
https://github.com/diffplug/durian
You'd need to create a Sonatype account, bintray account, and Travis CI account, and the project would use gradle as its build system. If you're interested in a PR let me know, and if not I'll put it up as "com.diffplug.mslinks" (or something else if you prefer).
I want to set some thing like "cmd.exe /c my-command" as a target, I get an exception as the createLink tries to resolve it as avalid path only (Paths.get(...))
Hey,
as I know that getting feedback is one of the best parts of programming and content creation so I thought to share some download stats from Maven Central and the Bintray repo. Although we're not in any top 10 list yet, but we have a good average of 90 downloads per month (peak of 227 in March) coming from 40 unique IPs (average).
In my opinion, that shows that you did a good job with the lib :)
Hi!,
I'm having troubles getting the built jar from maven central with Gradle. I'm working with Java 1.8 (class version 52.0). I couldn't find any mention of it being Java 14+ only, so I'm making an issue.
I'm using the following Gradle code:
dependencies{
// ...
implementation("com.github.vatbub:mslinks:1.0.6")
// ...
}
It's the same shown on the project's maven central page.
I'm getting the following error:
[...]\manager\control\Controller.java:13: error: cannot access ShellLink
import mslinks.ShellLink;
^
bad class file: [...]\.gradle\caches\modules-2\files-2.1\com.github.vatbub\mslinks\1.0.6\926d37969cba023bb65a93f3bf038585aeab6312\mslinks-1.0.6.jar(mslinks/ShellLink.class)
class file has wrong version 58.0, should be 52.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
In case there is a environment variable name, that contains backslashes, the method createLink
fails.
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 12
%\\.\Global\Motherboard Resources\PATHS%
^
at java.base/java.util.regex.Pattern.error(Pattern.java:2015)
at java.base/java.util.regex.Pattern.escape(Pattern.java:2604)
at java.base/java.util.regex.Pattern.atom(Pattern.java:2273)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2197)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2056)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1778)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1427)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1094)
at mslinks.ShellLink.resolveEnvVariables(ShellLink.java:395)
at mslinks.ShellLink.setTarget(ShellLink.java:336)
at mslinks.ShellLink.createLink(ShellLink.java:380)
Looks like just adding replace("\\", "\\\\");
might fix the problem
Hi,
I get this Exception:
mslinks.ShellLinkException: unsupported ItemID type
at mslinks.data.ItemID.(ItemID.java:144)
at mslinks.LinkTargetIDList.(LinkTargetIDList.java:71)
at mslinks.ShellLink.(ShellLink.java:113)
at mslinks.ShellLink.(ShellLink.java:105)
This happens on Windows 10.
I created a directory shortcut using the context menu option. It's very simple shortcut.
I remember this used to work just fine when I started using the library. I'm wondering if something changed with the latest Windows updates.
Thanks,
Alex.
Hi, Sorry to disturb you but I couldn't find it in doc or anything.
Does your API allow to set the Property "Run as Admin" for the shortcut?
Is this a cmd arg?
Hi,
I'm using your lib to replace some VB code and I encountered this part:
IconLocation = "C:\Windows\system32\SHELL32.dll, 131"
Can I pass that 131 somehow to ShellLink
? I tried:
.setIconLocation("C:\\Windows\\system32\\SHELL32.dll, 131")
but it does not work.
I believe I've found a bug with new ShellLink("foo.lnk")
What works:
CreateShortCut <link> <exe> <params> <icon> <index>
on Windows 10What doesn't work:
CreateShortCut <link> <exe> <icon> <index>
on Windows XP.
I've attached some sample lnk
files for examination here.
For now, the workaround is to catch the exception on Windows XP and optionally rewrite the icon using mslinks
when the exception occurs.
Backtrace:
java.lang.NegativeArraySizeException
at io.ByteReader.readUnicodeString(ByteReader.java:161)
at mslinks.data.ItemID.<init>(ItemID.java:74)
at mslinks.LinkTargetIDList.<init>(LinkTargetIDList.java:45)
at mslinks.ShellLink.<init>(ShellLink.java:90)
at mslinks.ShellLink.<init>(ShellLink.java:82)
at mslinks.ShellLink.<init>(ShellLink.java:77)
at mslinks.ShellLink.<init>(ShellLink.java:69)
at qz.deploy.WindowsDeploy.hasStartupShortcut(Unknown Source)
at qz.deploy.DeployUtilities.hasShortcut(Unknown Source)
at qz.deploy.DeployUtilities.createShortcut(Unknown Source)
at qz.common.TrayManager.shortcutToggle(Unknown Source)
at qz.common.TrayManager.lambda$new$2(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Hi all,
I just saw that you added gradle to your project. I am not that familiar with gradle as I am with Maven, hence, I would like to stop releasing mslinks to Maven Central.
So, if anyone feels like taking that job over, feel free to do so 😎
Regards,
Freddie aka vatbub
Hey, just wondering if you could toss a version number into the code somewhere. Thanks!
I have a .ico that I'm trying to set for the shortcut with this but it won't work. If I manually go into the .lnk properties and set it, it does work. I've checked that the paths are all correct in the code "C:\Users\Jake\Desktop\Folders n' stuff\Korder\sprites\logo.ico". I can create the shortcut just fine and it does open the target file but I just can't set the icon.
I've tried "ShellLink.createLink(p,p1).setIconLocation(p2);" with p being the target file, p1 being the shortcut, and p2 being the icon and I've tried
ShellLink sl = ShellLink.createLink(p,p1);
sl.setIconLocation(p2);
Edit: Just tried "%USERPROFILE%\Desktop\Folders n' stuff\Korder\sprites\logo.ico" and that didn't work either.
Edit: I got it to work. I had to put .saveTo(p1) at the end.
The following method needs to be added to the ShellLinkHelper so target arguments are supported:
public void setArgs(String args) { link.setCMDArgs(args); }
here is my .lnk file,one is japanese,another is chinese
here is my code
ShellLink shellLink = new ShellLink(file); System.out.println(shellLink.getLinkInfo().getLocalBasePath());
and console out put:
O:\game\J�c����B\Start.exe A:\�½��ı��ĵ�.txt
my os is windows10 x64 Chinese version.
here is the sample .lnk files.
lnk.zip
when I change your code
localBasePath = data.readString(pos + size - data.getPosition());
to
localBasePath = new String(bytes,"GBK");
the "新建文本文档" file can be correct printlned on console,but another file is incorrect. I change Charset to 'Shift_JIS' the other "JD温泉。" file can be correct printlned.
is there a way to read the .lnk file's Charset ?
This is an actual text in a shortcut target
I:\DXALW32.EXE -cUK "-dI:;I:\Country\UK" -uABC
I have then created this
ShellLink sl = ShellLink.createLink("i:\DXALW32.EXE -cUK "-dI:\;I:\Country\UK" -uABC")
but it gives this error:
java.nio.file.InvalidPathException: Illegal char <"> at index 20: i:\DXALW32.EXE -cUK "-dI:;I:\Country\UK" -uABC
How am I supposed to format the options correctly to make it work?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.