Get local file path of a ressource
Solution 1
So I said that the first answer was correct for me but I was wrong since the directory structure doesn't match at all after deploying my application. The following code finds a ressource inside a jar file and returns its local filepath.
String filepath = "";
URL url = Platform.getBundle(MyPLugin.PLUGIN_ID).getEntry("lib/dummy.exe");
try {
filepath = FileLocator.toFileURL(url).toString();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(filepath);
The string filepath contains the local filepath of the ressource which is inside a plugin.
Solution 2
The install directory is typically the directory where your java app is started, this path can be found from a running java app as
System.getProperty("user.dir");
If you want to get absolute path of a file relative to app dir you can use File.absolutePath
String absolutePath = new File("lib/dummy.exe").getAbsolutePath();
Solution 3
Try this
URL loc = this.getClass().getResource("/file");
String path = loc.getPath();
System.out.println(path);
Solution 4
First, you should take a look at the Oracle "Finding Files" documentation.
They list recursive file matching and give example code:
public class Find {
public static class Finder
extends SimpleFileVisitor<Path> {
private final PathMatcher matcher;
private int numMatches = 0;
Finder(String pattern) {
matcher = FileSystems.getDefault()
.getPathMatcher("glob:" + pattern);
}
// Compares the glob pattern against
// the file or directory name.
void find(Path file) {
Path name = file.getFileName();
if (name != null && matcher.matches(name)) {
numMatches++;
System.out.println(file);
}
}
// Prints the total number of
// matches to standard out.
void done() {
System.out.println("Matched: "
+ numMatches);
}
// Invoke the pattern matching
// method on each file.
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
find(file);
return CONTINUE;
}
// Invoke the pattern matching
// method on each directory.
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
find(dir);
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file,
IOException exc) {
System.err.println(exc);
return CONTINUE;
}
}
static void usage() {
System.err.println("java Find <path>" +
" -name \"<glob_pattern>\"");
System.exit(-1);
}
public static void main(String[] args)
throws IOException {
if (args.length < 3 || !args[1].equals("-name"))
usage();
Path startingDir = Paths.get(args[0]);
String pattern = args[2];
Finder finder = new Finder(pattern);
Files.walkFileTree(startingDir, finder);
finder.done();
}
}
Best of luck!
Markus
I'm developing software based on eclipse for a big automation company.
Updated on June 04, 2022Comments
-
Markus almost 2 years
what is the best way to get the local file path of a ressource inside of my project?
I have a lib folder containing the file dummy.exe which I want to execute but first I need to know where it is (Which can be different for each user, based on the install directory.
-
fge almost 11 yearsAnd what if the resource in question is found in a jar? How do you want to deal with it?
-
Markus almost 11 yearsIt is in a jar file but I deliver it in a directory structure instead of a jar file for other reasons already.
-
-
Markus almost 11 yearsSince the file dummy.exe is always in the same spot inside my application this aproach will fit me the most. Thank you. Also thanks to the other answers which I will study also.