Load resource from own class JAR file

When using Class.getResource() and providing a fairly common resource name, for example, "license.html" , Java can load the resource "license.html" from another JAR file that was previously specified in CLASSPATH, but may be completely unrelated to my application . Is there a way to get ClassLoader to first try to load a resource from the same JAR file that my class is in, and only if it is not found, look in other JARs for CLASSPATH?

+4
source share
2 answers

Alternatively, you can use JarURLConnection if you know the exact jar your file is in:

 jar:<url>!/{entry} 
+4
source

.. common name of the resource, for example, "license.html"

A good reason is to give it an unusual path, for example, according to the name package .
EG. "/com/our/app/license.html" unlikely to come across any other license.html

+3
source

Source: https://habr.com/ru/post/1411312/


All Articles