简体   繁体   English

jar文件中的Java classLoader

[英]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. classA.jar中 ,我使用ClassLoader.getSystemClassLoader()来获取“ log.txt”文件的路径。 In the project of classA.jar , there is no problem with that. classA.jar的项目中,这没有问题。

But when I use the jar in the Test-Project I get the follwing path to the "log.txt" file: 但是,当我在测试项目中使用jar时,会得到以下指向“ log.txt”文件的路径:

/lib/classA.jar!/log.txt /lib/classA.jar!/log.txt

Is there a way to get the classloader from Test-Project into to the jarfile? 有没有办法将类加载器从Test-Project放入jarfile?

For accessing to a resource file you should use Class#getResourceAsStream for reading this file. 要访问资源文件,应使用Class#getResourceAsStream读取此文件。

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. 注意:jar文件是一个归档文件,它应该保持不变,如果确实是一个日志文件,则不应将其存储在jar文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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