0

I am trying to get the path to a file named "policy.local" which I have stored in /com/package/

I want to specify the path to the above file from a class named "com.package.class". (So this class and the file are in the same folder)... How do I specify the path to the policy.local file, in the java code of class com.package.class?

2 Answers 2

2

A simple way: create an empty class (with no methods and fields) in the package "/com/package/" if this package doesn't contain any classes, or use any of them, if they are present. Then do the next:

ClassName.class.getResource("policy.local").toString()

This will give you a form of an absolute path of your file, something like this:

file:/C:/sandbox/xxx/com/package/policy.local
Sign up to request clarification or add additional context in comments.

Comments

1

As your class and the file (resource) are in the same package, you can get at it with:

getClass().getResource("policy.local")

If the class and the resource are not in the same package, you will want to use:

getClass().getClassLoader().getResource("com/package/policy.local");

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.