ronmamo / reflections

Java runtime metadata analysis
Do What The F*ck You Want To Public License
4.73k stars 703 forks source link

"could not create Dir using jarUrl from url" when WebListener scan package #447

Closed RussiaVk closed 1 year ago

RussiaVk commented 1 year ago

I created a class which use for batch register service in specific package and annotation.

@WebListener
public final class RmiServiceRegisterListener implements ServletContextListener {

    private static final Logger LOGGER;
    static {
        LOGGER = LoggerFactory.getLogger(RmiServiceRegisterListener.class);

    }

    public void contextInitialized(ServletContextEvent sce) {
        String scanPackage = sce.getServletContext().getInitParameter(Optional.ofNullable("rmi-service-package").get());
        new Reflections(scanPackage).getTypesAnnotatedWith(RmiService.class).forEach(service -> {
            try {
                service.getDeclaredConstructor().newInstance();
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                LOGGER.error(e.getMessage(), e);
            }
        });
    } 
}

I import it on my web project: image But it error:

2022-12-08 19:00:47.528  WARN [ForkJoinPool.commonPool-worker-16] o.r.Reflections                          : could not create Dir using jarUrl from url jar:file:/C:/Program%20Files/Apache/apache-tomcat-9.0.41/webapps/deking-sms-endpoint/WEB-INF/lib/deking-sms-core-0.0.1-SNAPSHOT.jar!/. skipping.
java.security.AccessControlException: access denied ("java.io.FilePermission" "file:\C:\Program Files\Apache\apache-tomcat-9.0.41\webapps\deking-sms-endpoint\WEB-INF\lib\deking-sms-core-0.0.1-SNAPSHOT.jar!" "read")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:485) ~[?:?]
        at java.security.AccessController.checkPermission(AccessController.java:1068) ~[?:?]
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:416) ~[?:?]
        at java.lang.SecurityManager.checkRead(SecurityManager.java:756) ~[?:?]
        at java.io.File.exists(File.java:829) ~[?:?]
        at org.reflections.vfs.Vfs.getFile(Vfs.java:160) ~[?:?]
        at org.reflections.vfs.Vfs$DefaultUrlTypes$2.createDir(Vfs.java:232) ~[?:?]
        at org.reflections.vfs.Vfs.fromURL(Vfs.java:103) ~[?:?]
        at org.reflections.vfs.Vfs.fromURL(Vfs.java:95) ~[?:?]
        at org.reflections.Reflections.lambda$scan$2(Reflections.java:176) ~[?:?]
        at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[?:?]
        at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1707) ~[?:?]
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[?:?]
        at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290) ~[?:?]
        at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:754) ~[?:?]
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) ~[?:?]
        at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) ~[?:?]
        at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) ~[?:?]
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) ~[?:?]
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) ~[?:?]
2022-12-08 19:00:47.548  WARN [ForkJoinPool.commonPool-worker-16] o.r.Reflections                          : could not create Vfs.Dir from url. ignoring the exception and continuing
org.reflections.ReflectionsException: Could not open url connection
        at org.reflections.vfs.JarInputDir$1.<init>(JarInputDir.java:31) ~[?:?]
        at org.reflections.vfs.JarInputDir.lambda$getFiles$0(JarInputDir.java:28) ~[?:?]
        at org.reflections.Reflections.lambda$scan$2(Reflections.java:177) ~[?:?]
        at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[?:?]
        at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1707) ~[?:?]
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[?:?]
        at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290) ~[?:?]
        at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:754) ~[?:?]
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) ~[?:?]
        at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) ~[?:?]
        at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) ~[?:?]
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) ~[?:?]
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) ~[?:?]
Caused by: java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Program Files\Apache\apache-tomcat-9.0.41\webapps\deking-sms-endpoint\WEB-INF\lib\deking-sms-core-0.0.1-SNAPSHOT.jar" "read")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:485) ~[?:?]
        at java.security.AccessController.checkPermission(AccessController.java:1068) ~[?:?]
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:416) ~[?:?]
        at java.lang.SecurityManager.checkRead(SecurityManager.java:756) ~[?:?]
        at java.util.zip.ZipFile.<init>(ZipFile.java:233) ~[?:?]
        at java.util.zip.ZipFile.<init>(ZipFile.java:172) ~[?:?]
        at java.util.jar.JarFile.<init>(JarFile.java:347) ~[?:?]
        at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:103) ~[?:?]
        at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:72) ~[?:?]
        at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:191) ~[?:?]
        at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:131) ~[?:?]
        at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:175) ~[?:?]
        at org.reflections.vfs.JarInputDir$1.<init>(JarInputDir.java:30) ~[?:?]
        ... 12 more

I also tryed enable tomcat security manager and add permission for my web project into catalina.policy,but still not work.

grant codeBase  "file:war:${catalina.base}/webapps/deking-sms-endpoint/-" {
    permission java.security.AllPermission;
}
RussiaVk commented 1 year ago

replace with classgraph