简体   繁体   中英

Java classLoader within a jar file

I work with the follwing project:

Test-Project

/lib/classA.jar
/src/main/java/org/test/classB.java
/src/main/resources/log.txt

In classA.jar , I work with the ClassLoader.getSystemClassLoader() to get the path of the "log.txt" file. In the project of classA.jar , there is no problem with that.

But when I use the jar in the Test-Project I get the follwing path to the "log.txt" file:

/lib/classA.jar!/log.txt

Is there a way to get the classloader from Test-Project into to the jarfile?

For accessing to a resource file you should use Class#getResourceAsStream for reading this file.

InputStream stream = Class.class.getResourceAsStream("/log.txt");

Note: a jar file is an archive, which is meant to be unchanged, if that is really a log file you shouldn't store it inside a jar file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM