0

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.

1
  • This technique does not work under all circumstances - see here for a more resilient solution. Commented Mar 28, 2014 at 16:33

1 Answer 1

1

What you get is encoded URL.

What you need is to decode it

URLDecoder.decode(url, "UTF-8");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.