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?
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.
精彩评论