Announcement Announcement Module
No announcement yet.
Class Path Scanning from JAR files Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Class Path Scanning from JAR files

    I am attempting to use the new 2.1 Class Path Scanning functionality, but have hit a problem when running aginst JAR files - this may be the fault of the underlying class loader, but would appreciate confirmation.

    Alll the following code has been run within the IDE, with a class path referencing the java output, together with the a JAR file containing the same classes. (If I just run against the JAR no output is generated).

    If I directly reference a class using the Spring PathMatchingResourcePatternResolver as follows:

            PathMatchingResourcePatternResolver scanner = new PathMatchingResourcePatternResolver();
            Resource[] resources = scanner.getResources("classpath*:basepackage/level1/level2/domain/DomainObject.class");
            for (int i = 0; i < resources.length; i++) {
                System.out.println("Direct Reference to class: "+resources[i]);
    I get the folloing output:
    Direct Reference to class: URL [jar:file:/C:/Eclipse/testApp/alib/platform-common.jar!/basepackage/level1/level2/domain/DomainObject.class]
    Direct Reference to class: URL [file:/C:/Eclipse/testApp/java/classes/basepackage/level1/level2/domain/DomainObject.class]
    If I try to use an ant style pattern, I only get the class from the directory structure (and not the JAR).

            PathMatchingResourcePatternResolver scanner = new PathMatchingResourcePatternResolver();
            Resource[] resources = scanner.getResources("classpath*:basepackage/level1/level2/**/DomainObject.class");
            for (int i = 0; i < resources.length; i++) {
                System.out.println("Wildcard Reference to class: "+resources[i]);
    Wildcard Reference to class: URL [file:/C:/Eclipse/testApp/java/classes/basepackage/level1/level2/domain/DomainObject.class]
    I've traced this down to the "findAllClassPathResources" within "PathMatchingResourcePatternResolver".
    When using a wildcard the path passed to this method is that below the wildcard (i.e. "basepackage/level1/level2/") which does not return resources from the JAR file, as per the following code:

            String path = "basepackage/level1/level2/";
            Enumeration resourceUrls = getClass().getClassLoader().getResources(path );
            Set result = CollectionFactory.createLinkedSetIfPossible(16);
            while (resourceUrls.hasMoreElements()) {
                URL url = (URL) resourceUrls.nextElement();
                System.out.println("Direct to class loader:" +url);
    Which just returns:
    Direct to class loader:file:/C:/Eclipse/testApp/java/classes/basepackage/level1/level2/
    Is there any way known to allow the loading of resources using wildcards, aginst JAR files? (I was going to use this within aa standalone app actually started from Web Start, and a web server based app).

    Last edited by baronludwig; Sep 13th, 2007, 02:18 AM. Reason: Resolved

  • #2
    Maybe you have the same problem as I have had:

    In my case the problem was the jar generation. How are you generating it? Using Ant? Eclipse?
    Last edited by leopinho; Sep 11th, 2007, 03:07 PM.


    • #3
      I'd looked everywhere!

      I was generating using ANT, but was also using iajc (Aspectr J Ant Tasks) to weave an Aspect Jar with my Jar. This produces an output Jar without the directory structure.

      Following resolution of this, the class path scanning is successfully as the class loader successfully resolves the directory aginst the JAR.