How can I display the ways in which the class.getResource class.getResource() , class.getClassLoader().getResource() methods will be class.getResource() for resources. For some class.

    1 answer 1

    If you just need to understand which directories and libraries are included in the classpath, then you can use URLClassLoader.getURLs()

     public static URL[] getClasspath(Class<?> clazz) { ClassLoader cl = clazz.getClassLoader(); return ((URLClassLoader)cl).getURLs(); } 

    This does not bring you the complete tree of the contents of each resource, for this you will have to manually recurse around it. For example, this way you can get the contents of the directory:

     Enumeration<URL> en = Main.class.getClassLoader().getResources("org.package.name"); if ( en.hasMoreElements() ) { URL url = en.nextElement(); File file = new File(url.toURI()); String[] filenames = file.list(); System.out.println(Arrays.asList(filenames)); } 

    Also, for a sample, you can take the implementation in Spring - PathMatchingResourcePatternResolver

    • Do I understand correctly that class.getClassLoader (). getResource (). Will you search for resources along the paths that your first code above prints to me? - voipp