Jonah
Jonah

Reputation: 10091

How to Load File Outside of, but Relative to, the JAR?

I need to load a file outside of the JAR, but relative to it (lib/config/config.ini to be exact). I used that exact path, and it works fine as long as the working directory is where the JAR is, i.e.

/path/to/jar$ java -jar JAR.jar

If it's run like this:

~$ java -jar /path/to/jar/JAR.jar

It can't find it. How can I correctly load a file relative to the location of the JAR?

Upvotes: 5

Views: 6182

Answers (1)

Chris Nash
Chris Nash

Reputation: 3051

Try using getClassLoader().getResource("classname") to find the URL of a class in your jar file. You'll find it's delimited with a ! between the path to the jar, and the path within a jar, which you can easily slice.

Upvotes: 3

Related Questions