• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Opening shortcuts using Java?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone.

The problem I'm having is that I'm trying to figure out how to make Java open a shortcut link in Windows. For example: for the file (C:\Users\Darnell Day\Documents\HQTester\Favs\Legend\New Journal Document.lnk) I would like Java to return the original location of this file which is ("C:\Users\Darnell Day\Documents\HQTester2\New Journal Document.jnt"). Is there anyway Java could do this?

Please and Thank You.
 
Sheriff
Posts: 22805
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't any way to do this with the JSE API.
There are a few libraries around that allow you to read and create Windows shortcuts. Most of these use JNI. One I've encountered recently is JShortcut. I haven't used it myself so I can't tell you how well it works.
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try using the JShortcut then, but I've never been successful in installing/using a jar library on Netbeans. Would you mind explaining specifically how I could install and use it in my coding?
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> The problem I'm having is that I'm trying to figure out how to make Java open a shortcut link in Windows.

if you just want to open whatever the link is to, use Runtime

to get the info from the link, you could create a vbscript file to do that, run it from java and have it return the data,
but it gets a bit messy.
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to post an update, I've been trying to figure out how to implement Jar files into my Java project but I'm not getting any luck. I've googled and found a source of JShortcut here but I don't see any .jar files within the folder, even though it says to "Double click on the jar file", which leaves me very confused. I see that this can create shortcut links, which I will need later and is very useful, but I hope it can retrieve the original path of the shortcut link which is what I ultimately need.

Michael Dunn wrote:> The problem I'm having is that I'm trying to figure out how to make Java open a shortcut link in Windows.

if you just want to open whatever the link is to, use Runtime

to get the info from the link, you could create a vbscript file to do that, run it from java and have it return the data,
but it gets a bit messy.



It's not that I want to open the link exactly, I really want the location the link is pointing to.
For example I have a shortcut C:\New Folder1\Example.lnk. The target of the shortcut is C:\New Folder2\Example.txt. I would like Java to select the shortcut link "C:\New Folder1\Example.lnk" and return the target which would be "C:\New Folder2\Example.txt".

Creating a VBScript file sounds extremely complicated, but if it's the only way I guess I don't really have any other option. But would you mind explaining a little more in depth of how I would go about doing this? I don't know that much Visual Basic and I just started on Java about 6 months ago so I'm a real light beginner here. Every bit of information would really help out alot.
 
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darnell Day wrote:I've googled and found a source of JShortcut here but I don't see any .jar files within the folder, even though it says to "Double click on the jar file", which leaves me very confused.



So I'm confused, too. When I follow that link I don't see a page which tells me to double-click on the jar file. And I don't see any "folder" either.

But I did see a "Downloads" link so I followed that. It led me to a page with various "jar" links which (if I am to believe the URL) would let me download jar files. So that's your first step. The next step is to follow the link to the installation instructions, which now that I see it must have been the page you went to first. So what you should do is to download the jar file first, and then follow those instructions.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darnell Day wrote:Just to post an update, I've been trying to figure out how to implement Jar files into my Java project but I'm not getting any luck. I've googled and found a source of JShortcut here but I don't see any .jar files within the folder, even though it says to "Double click on the jar file", which leaves me very confused. I see that this can create shortcut links, which I will need later and is very useful, but I hope it can retrieve the original path of the shortcut link which is what I ultimately need.


I did a quick google search. The first link was to a page with clear links for downloads, documentation, and a readme for installation. It also had a link to github which also has links for downloading (and has more download options). The next 4 results from Google were to the downloads, documentation, and other pages on the same site. They each had a clear link to the home page with those clear links I talked about. The 6th link in google is the github page. And although that one isn't as obvious as to how to get the files there is a Downloads link on that page as well.

So what have you tried to download?
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry I wasn't clear enough. Well to be more specific on what I did, I downloaded it from here first and I couldn't find any jar files within the zip. Next, I downloaded the jar files from the older download links, specifically this one. But it seems everytime I double click it, windows doesn't know which program to open it with. Otherwise, when I run in it the command window, with the jar file within this folder: C:\Users\Darnell Day, I get the following error:


Meanwhile, when the file is placed within my other drive, I get this error:


I hope I'm more clear this time.
 
Paul Clapham
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yeah, those installation instructions do assume that you know how to run the "java" command at the command line and that you're familiar with things such as the current working directory and so on. That's typical of installation instructions, unfortunately.

As for the message about 32-bit DLLs, you probably didn't notice that the last version of the product was dated several years ago. 2003 or 2004, I believe. That was in the days before 64-bit machines, so naturally it was written to run on 32-bit machines. I think that if you use the 32-bit version of Java instead of the 64-bit version you have installed, then you might be able to get it to work. At least, I have 32-bit Java installed on my 64-bit Windows box at home and my code uses DLLs which I'm pretty sure are 32-bit DLLs.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if you use the GitHub source and read the directions there you will see that one of the distributables does have a 64 bit DLL which was contributed after-the-fact by a user.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Creating a VBScript file sounds extremely complicated, .... But would you mind explaining a little more in depth of how I would go about doing this?

easier to show you (not many lines)

save this as GetShortcutPath.java

in the *same* folder, save this as GetShortcutPath.vbs
copy into Notepad, make sure it's not saved as GetShortcutPath.vbs.txt

compile and run the .java file, if all done correctly the full path should appear in a JOptionPane
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:> Creating a VBScript file sounds extremely complicated, .... But would you mind explaining a little more in depth of how I would go about doing this?

easier to show you (not many lines)

save this as GetShortcutPath.java

in the *same* folder, save this as GetShortcutPath.vbs
copy into Notepad, make sure it's not saved as GetShortcutPath.vbs.txt

compile and run the .java file, if all done correctly the full path should appear in a JOptionPane



Alright, I tried this and here's what I did, with me being more specific now :) . I opened NetBeans, created a new Project called GetShortcutPath. Inside GetShortcutPath.java I copied and pasted the code. Then I went inside the src folder of the project, created a new text document with the code and saved it as GetShortcutPath.vbs. I ran it within NetBeans which made it open the command window and then closed it immediately after it opened. I could barely make out what the error said but it seemed to say Error: Cannot load main class GetShortcutPath.vbs I think. (It opens and closes extremely fast). So I googled how to compile a java project, which told me to open the command window, type in 'javac GetShortcutPath.java'. Then it created a class file. Then within the command window I typed 'java GetShortcutPath' which I'm pretty sure ran it and it opened up a message Panel I believe. So it works, but somewhat.
The link I put in is the following: "C:\Users\Darnell Day\Documents\HQTester\Favs\Legend\New Journal Document.lnk".
Which should return: "C:\Users\Darnell Day\Documents\HQTester2\New Journal Document.jnt".
But instead it returns: "Full Path: C:\Users\Darnell"

Also, one more question. Would there be any way I could use the vbscript file somewhat as a method? Meaning can I pass parameters to the file and can it return a value back to the java file? That would be REALLY useful!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
before the line
shell.run(...)

put this line
If targetPath = "" then targetPath = "Nothing_Or_Error"

otherwise you might get a recursive opening of everything (if targetPath is empty)

as you've found out (via creating a project) all 3 files must be in the same folder
.java .class .vbs

the reason you're getting "Full Path: C:\Users\Darnell" is because of the space between Darnell and Day,
i.e. treating 'C:\Users\Darnell' as args[0] then 'Day\Documents\HQTester2\New' as args[1] etc
try it like this (untested)
shell.run("java.exe -classpath """&currDir&""" GetShortcutPath " & """&targetPath&""")

> Would there be any way I could use the vbscript file somewhat as a method?
I don't know of a way, it's unlikely as I believe they'd need access to each others memory addresses
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the late reply, I've been busy these past couple of days. So now I changed up the VBScript file and modified it with these lines:



So now the entire VBScript file looks like this:



I saved it, compiled it again, and ran it. The output is now "Full Path: &targetPath&"
It's a shame that I won't be able to use this program as a method. The program I'm trying to create is that I'm trying to make somewhat of a Windows Media Player playlist saver. To where it saves a playlist based on what's inside a selected folder. The playlist is simply a textfile of all the files within that folder. For shortcuts, it replaces it with what the shortcut is targeting. If the shortcut targets a file, it replaces it with the target of that file. If the shortcut is targeting a folder, it selects all the files within that folder. It sounds like a pretty basic program, which I thought would be great as I'm trying to practice more of my Java skills, but I knew the only problem I would encounter would be to try to get the targets of various shortcuts. And from the sounds of it, I might not able to complete it.
 
Rob Spoor
Sheriff
Posts: 22805
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The number of quotes is probably off:
So let's analyze the String:
- " to start a String (before java.exe)
- "" to include one single " (after classpath)
- " to close a String (after classpath)
- " to start a String (before GetShortcutPath)
- "" to include one single " (before GetShortcutPath)
- " to close a String (after GetShortcutPath)
- " to start a String (before targetPath)
- "" to include one single " (before targetPath)
- "" to include one single " (after targetPath)
- " to close a String (after targetPath)

As you can see from this analysis, &targetPath& is inside the String. You need to change that last part to Those four double quotes are Strings containing only one single " character - exactly what you need.
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, sorry for the long reply, I've been unable to go on my computer due to this heatwave. So I changed the string of targetPath from """&targetPath&""" to """"&targetPath&"""" and now it finally displays the proper target path for the file. Which is fantastic. But I'm still somewhat confused. Is there anyway I can use this VBScript file in my java coding? As my intentions are to have it find the target path for a variable amount of shortcut link files, based on how many the user selects. Is there anyway Java can handle this? For instance, I can change up my program so that I can put all of the shortcut links the user selected in an array. Can I somehow pass this array to the VBScript and make the VBScript return the result back to my java program?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Can I somehow pass this array to the VBScript and make the VBScript return the result back to my java program?

VBScript takes arguments the same way java does.

if you're feeling adventurous, you can get java to write the vbscript file, run it, then delete it.
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:> Can I somehow pass this array to the VBScript and make the VBScript return the result back to my java program?

VBScript takes arguments the same way java does.

if you're feeling adventurous, you can get java to write the vbscript file, run it, then delete it.


Ok, so let's say I have a File type called file1 in my Java program. It sounds like your saying I could pass file1 to VBScript from Java, VBScript calculates a string based on file1, and then VBScript can return that string back into Java?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
supplying args[0] to a java program run
from the command line is the same with VBScript
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:supplying args[0] to a java program run
from the command line is the same with VBScript


I'm sorry but I'm really confused on what that means.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
say you want to supply 3 names to the program, as arguments - Fred, Mary and Joe

if you want to run a java program from the command line:
java.exe -classpath . Test Fred Mary Joe

if you want to run a vbscript file from the command line:
wscript.exe Test.vbs Fred Mary Joe

so, supplying the arguments is identical

[edit]
all the above, for simplicity, has the command prompt in the same folder as the .class and .vbs files
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok, so if I'm understanding this correctly, your stating that I should have my Java program generate a VBScript file, run it, get the output, close the program, and delete it immediately afterwards. If so that is a really interesting concept. I could manage to code something like that, but if that's the case, how would I get the output to return to the Java program?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> how would I get the output to return
> to the Java program?

exactly the same as earlier posts.
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:> how would I get the output to return
> to the Java program?

exactly the same as earlier posts.


You mean to run it from the Java program via the command line?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you've had it working where you run the java program, which then runs the vbscript file,
which, in turn, re-runs the java program, this time supplying the target path as an argument.

the java program works 2 ways, via args[] > 0
a) if == 0, it runs the vbscript file, and exits
b) args > 0, it is being run from the vbscript file, which is supplying the argument

if you want the java program to write the vbscript file, then delete it -
in (a) this is where you'd write the file, save the file, then run it
you may have to add a small delay (e.g. 500ms) between saving and running
in (b) once the argument has been supplied, the vbscript file can be deleted
 
Darnell Day
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alrighty, I just about finished the program I've been working on. But the last piece to this puzzle is this little problem here.

Michael Dunn wrote:you've had it working where you run the java program, which then runs the vbscript file,
which, in turn, re-runs the java program, this time supplying the target path as an argument.

the java program works 2 ways, via args[] > 0
a) if == 0, it runs the vbscript file, and exits
b) args > 0, it is being run from the vbscript file, which is supplying the argument

if you want the java program to write the vbscript file, then delete it -
in (a) this is where you'd write the file, save the file, then run it
you may have to add a small delay (e.g. 500ms) between saving and running
in (b) once the argument has been supplied, the vbscript file can be deleted



Yes, the way I'm going to handle this is by letting Java automatically create the file for me, run it, get the output, and then delete it. Problem is I never had Java run a program before and get the output of that program. Which is why I'm mostly confused because I have no idea how to go about doing this. Creating the file is not going to be a problem. I'm going to create a File Type 'saveFile' for this program, create a new 'saveFile', put the code for the program within a string, then printwrite that string to 'saveFile', somehow run it and get the output, and then delete 'saveFile'. That's my plan of attack.

I'm still not too very keen on the arguments > 0 thing, but I'm going to be researching more on how to make this program run via Java.

EDIT: Ok, I just quickly typed up this code here. It's a method that I'm going to call that so far just creates the vbs file. Now I have to figure out how to run it, and how to get the output of it.


EDIT: Ok, after doing some more research and some trial and error, I think I'm starting to get somewhere. I changed up my Java coding to the following updated method:


Apparently I had to surround the shortcutPath with quotations as it was giving me an error with the command prompt. First I've been trying to get the Java generated VBScript to run from the command line. I found out that to run it you need to use "cscript.exe scriptname.vbs". The next problem was that it wasn't printing out anything so I found the way to print the vbscript to the command line is by using "Wscript.Echo "" then whatever you want to print. Now all I needed to do was to call the command line in Java, run the vbscript file, and print the output, which I'm sort of close on figuring out. So I created another method 'run' that will run the command prompt. This is the coding I use to call it:


And this is the method I'm using:


I would like this method to return a String value later, but for now I just want it to print out its results. But now the problem I'm having is that when I call this program, nothing happens. When I call it, it just displays the following:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.


When I try and close the gui the program created, the gui is unresponsive. Seems as if the gui or the project is just hanging. When I call the vbscript file from the command line it displays the proper path that I'm trying to get, so I know the vbscript file is working correctly. I also used the same line of code that is in 'commandPrompt' into the command line and it also displayed the correct solution. But for some reason when I'm calling it through Java, it's not giving me anything.
 
There's a city wid manhunt for this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic