开发者

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

开发者 https://www.devze.com 2023-02-13 05:28 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消