简体   繁体   English

查找特定Java类的文件路径

[英]Finding file path for a specific java class

I am currently trying to get the path of a current java class called "WordBase". 我目前正在尝试获取当前Java类“ 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()...? 也许有比使用class.getProtectionDomain()还要简单的方法?

Thanks in advance. 提前致谢。

What you get is encoded URL. 您得到的是编码的URL。

What you need is to decode it 您需要解码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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