1. Problem description
Obtain the resources in the jar package through getClassLoader(), which is empty:
this.getClassLoader().getResources("com/xxxx/test/Start.class").hasMoreElements();
this.getClassLoader().getResourceAsStream("test.txt");
Using ClassGraph, you can change it to the package name to see if you can get it:
try (ScanResult scanResult = new ClassGraph().acceptPackages("com.xxx.xxx.xxx").scan()) {
scanResult.getAllResources().forEachByteArrayIgnoringIOException((io.github.classgraph.Resource res, byte[] content) -> {
String path = res.getPath();
System.out.println("文件:" + path);
});
}
Two, the solution
The resource cannot be obtained, basically the path is wrong, or the ClassLoader is wrong. Another problem is that there is a problem with the jar package. There is no directory in the jar package. You can view it through 7zip:
7z l xxxx.jar
Wrong jar package directory:
Correct jar package directory: