Markus
Markus

Reputation: 1482

Get local file path of a ressource

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.

Upvotes: 2

Views: 23134

Answers (4)

Markus
Markus

Reputation: 1482

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.

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136122

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();

Upvotes: 5

Udanesh N
Udanesh N

Reputation: 171

Try this

   URL loc = this.getClass().getResource("/file"); 
      String path = loc.getPath(); 
          System.out.println(path);

Upvotes: 1

jdero
jdero

Reputation: 1817

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!

Upvotes: 1

Related Questions