简体   繁体   中英

Finding file path for a specific java class

I am currently trying to get the path of a current java class called "WordBase". I am using the following code.

    final File f = new        

    File(WordBase.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    System.out.println(f);

This gives me the following output:

    C:\Users\Rasmus%20J\wordbase\out\production\wordbase

Which is incorrect. Correct would be:

    C:\Users\Rasmus J\wordbase\out\production\wordbase

Is there any way to clean things up and get a correct output? I want the code to be able to run on all different kind of computers from different paths. Perhaps there is an even easier way than using class.getProtectionDomain()...?

Thanks in advance.

What you get is encoded URL.

What you need is to decode it

URLDecoder.decode(url, "UTF-8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM