Short-cut to a file

M

MVB

Hi All,
I want to get the actual path of target file from a short-cut to that
file, using Java. Can anyone help me to get this?

Thanks,
MVB
 
O

Oliver Wong

MVB said:
Hi All,
I want to get the actual path of target file from a short-cut to that
file, using Java. Can anyone help me to get this?

Different OSes implement shortcuts in different ways. In the case of
Windows XP, a shortcut file is a plain old regular file which you can open
up and read, but they are binary (as opposed to plain text), so you'll
have to figure out the file format to decypher.

- Oliver
 
S

Steve Sobol

Different OSes implement shortcuts in different ways. In the case of
Windows XP, a shortcut file is a plain old regular file which you can open
up and read, but they are binary (as opposed to plain text), so you'll
have to figure out the file format to decypher.

With Windows you're probably best off using JNI to call the Win32 API
function that creates shortcuts. With Linux/*BSD and Gnome (and probably also
KDE) the shortcut file is simply a text file.

I'd like to create a Java class that does what we're talking about, but I don't
have a lot of time in which to do it...
 
R

Real Gagnon

I want to get the actual path of target file from a short-cut to that
file, using Java. Can anyone help me to get this?

The easiest way on Windows is to call a VBS and capture the output.

In this`example, I open the Firefox shortcut on the desktop and read the
target Path.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class VBSUtils {
private VBSUtils() { }

public static String readShortcut() {
String result = "";
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);

String vbs = "set wshshell = WScript.CreateObject(\"WScript.Shell\") \n"
+ "desktop = wshshell.SpecialFolders(\"AllUsersDesktop\") \n"
+ "set shortcut = wshshell.CreateShortcut"
+ "(desktop & \"\\Mozilla Firefox.lnk\") \n"
+ "WScript.Echo shortcut.TargetPath \n"
+ "Set wshshell = Nothing \n";

fw.write(vbs);
fw.close();
Process p =
Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
return result.trim();
}

public static void main(String[] args){
msgBox("Shortcut for Firefox is " + readShortcut());
}
public static void msgBox(String msg) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, msg, "VBSUtils", javax.swing.JOptionPane.DEFAULT_OPTION);
}
}

Bye.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top