• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Running .lnk file from runnable Jar file doesn't work

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a strange problem.  I'm trying to run a Windows program from a shortcut (.lnk file), using the command below.  It works just fine when I run it from within Eclipse, but as soon as I generate a runable .jar file and execute it from there, I get the error message shown below.  A dir command of the file C:\Shorts\npn.lnk proves that it DOES exist.  Is there some reason I can't run link files from within a runnable .jar file (but it works in Eclipse)?



ERROR MESSAGE:
[Content]
C:\Shorts\npn.lnk
The specified path does not exist.

Check the path, and then try again.

[OK]
 
bobt Thompson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm getting really frustrated.

I tried creating a .vbs file to invoke the link file and running that from within the jar file.  Again it worked in Eclipse.  From the runnable .jar file I got this message.

Best I can tell, it's not possible to run a Windows shortcut from a runnable Java jar file!

---------------------------
Windows Script Host
---------------------------
Script: C:\Users\bthompson\temp.vbs
Line: 10
Char: 1
Error: No application is associated with the specified file for this operation.
Code: 80070483
Source: (null)

---------------------------
OK  
---------------------------
 
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bobt Thompson wrote:Best I can tell, it's not possible to run a Windows shortcut from a runnable Java jar file!


I'm not sure what rundll32 SHELL32.DLL,ShellExec_RunDLL does, but here's a simple example of starting an application from a link:

notepad++ was opened on my desktop - here's the console where I can the jar:
 
Saloon Keeper
Posts: 26291
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot use a softlink inside a JAR. A JAR might have "directories" and "files", but to the Windows filesystem, it's just one file and the filesystem services cannot handle the data components ("directories" and "files") the way that they can do with actual files. Among other things, the JAR components probably don't even carry a full set of Windows file attributes.

I think it might be confusing because while technically you could create a filesystem driver where ZIP-like archives were first-class filesystem objects, I'm pretty sure that that's not what Windows does and that when you click on a ZIP/JAR/WAR/EAR to view what's inside, it's actually the Windows Desktop calling a separate unzip/view service, not doing filesystem function calls.

As for a link to an executable JAR. the issue there is that just because a jar is executable doesn't mean that clicking on it will do the right thing. Which is why executing a JAR should be done via a shell script/batch command file, not as a direct desktop function.

Finally, when in doubt, realize that Java is a "write once/run anywhere" language, so when you actually produce deployable Java code, anything that wouldn't work the exact same way on MacOS or Linux won't work properly on Windows. And vice versa.
 
Ron McLeod
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:You cannot use a softlink inside a JAR.


In this case, the LiNK file (.lnk extension) is a file which which is used to define a shortcut.  It's contents include the path to the actual target, the starting directory where the target should be executed from, the window type to be used, whether the user should be elevated to administrator, etc.

The Windows start command is probably the easiest way to run the shortcut.
 
Tim Holloway
Saloon Keeper
Posts: 26291
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we have 3 very different types of links here.

One is the so-called "hard" link. I'm not sure Windows supports this. In a hard link, the file data is pointed to by several different directory entries. This is very efficient, but only works when all of the links are on the same filesystem, since the directory entries are nothing but hardware pointers (inodes) to the data. Hard links aren't used much any more, although I think a lot of Unix/Linux OS utilities may employ them as aliases for certain commands.

Then there's the softlink. In this case, there is a primary file directory entry and then there are softlinks, which simply resolve to the primary (canonical) path of the file in question. They are extremely popular on the Unix-style OS's.

Finally, there's the shortcut link that you're describing. It's not a true link - it's an execution profile file and it's not supported by the filesystem (unlike hard and soft links) but by the desktop manager program, which reads the link file and attempts to execute the application using the supplied options.

Hard links and soft links are supported by the essential Unix archiving tools, such as tar. A ZIP file could potentially support links, but as far as I can recall JAR files don't use them (I wouldn't). A shortcut link would simply be another data file in a ZIP or JAR, and unless the desktop manager makes some extra effort, I would not expect a LNK to work inside a JAR.

Speaking of ZIP, in a sense, it, too is "write once run anywhere". The actual format of ZIP directory entries is sort of a common denominator of all the filesystems that anyone wanted to be able to ZIP. The Amiga OS, for example, allowed about 80 characters of textual descriptive information for a file (early MacOS was known for something similar even earlier). So the ZIP standard has a place for that data, even though virtually no modern OS can use it.

 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:One is the so-called "hard" link. I'm not sure Windows supports this.


It does: https://docs.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions

Anyway, I think the problem here is that the shortcut is located inside the JAR file. The file system cannot execute anything inside a JAR file, which is little more than a ZIP file. This isn't a limitation of the JAR file but of the file system. If you would like to execute it, you need to unpack it somewhere first.


On a side note, if you wish to "run" a file, such as a shortcut, you should consider using java.awt.Desktop:

To be on the safe side you can first check if Desktop.isDesktopSupported() returns true.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic