Reputation: 408
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.
Upvotes: 0
Views: 54
Reputation: 4342
What you get is encoded URL.
What you need is to decode it
URLDecoder.decode(url, "UTF-8");
Upvotes: 1