2

I'm using a JAR which contains only a ClassPath element to build the classpath for my Java app and I've encountered a weird behavior: I can load classes from the JARs mentioned in the ClassPath element but some (or all) resources are missing.

Example: My full-classpath.jar contains just a META-INF/MANIFEST.MF with the usual content and this line:

ClassPath: foo-service.jar bar-service.jar service-main.jar

service-main.jar contains com.pany.project.Main.

When I run java -cp full-classpath.jar com.pany.project.Main, that works.

But foo-service.jar and bar-service.jar contain service definitions in META-INF/services/....

When I invoke java.util.ServiceLoader from a Maven unit test, it can see both the foo and the bar service. When I java -cp full-classpath.jar com.pany.project.Main -service foo, I see an empty service list in my log.

Worse, when I call Main.class.getClassLoader().getResources("META-INF/MANIFEST.MF"), I get only the manifest from full-classpath.jar. The manifest files from the other three JARs are missing which doesn't make sense since I can load classes from those JARs! It also works when I build the whole classpath manually. It only fails when I use the ClassPath element in the full-classpath.jar.

Any ideas what could be wrong or how I could debug this?

I'm using Java 8.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820

1 Answers1

0

The manifest attribute Class-Path is handled in sun.misc.URLClassPath.JarLoader.parseClassPath(URL, String). This method is called once from sun.misc.URLClassPath.JarLoader.getClassPath(). At the start of getClassPath(), you can find this code:

        if (index != null) {
            return null;
        }

        if (metaIndex != null) {
            return null;
        }

which means: If your classpath JAR contains an index or "meta index" (whatever that is), the Class-Path attribute is ignored.

I can't tell why Java can still load classes from JARs mentioned in the Class-Path attribute but deleting the index fixes the resource loading problem.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820