<< Back to previous view

[CLASSPATH-6] classpath "empty" when classpath 0.2.2 used with clojure maven plugin 1.3.20+ Created: 26/Jun/14  Updated: 23/Jul/14

Status: Open
Project: java.classpath
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Kevin Livingston Assignee: Stuart Sierra
Resolution: Unresolved Votes: 0
Labels: bug

Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T07:51:42-06:00)
Maven home: /data/hudson/apache-maven-3.2.2
Java version: 1.7.0_05-icedtea, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.7.0-openjdk-
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.9.10-100.fc17.x86_64", arch: "amd64", family: "unix"


I have a project that is storing data in resources that it expects to find on the classpath. When the project uses java.classpath 0.2.2 and clojure-maven-plugin 1.3.20+ calling (java.classpath/classpath) produces unexpected results - it shows some sun stuff, but no dependencies, no clojure, none of my code or resources. Clojure seems to be functioning though, it's just calls in to the classpath api that pose issues.

For comparison:

java.classpath 0.1.0 and c-m-plugin 1.3.9 behaves as expected.

java.classpath 0.1.0 and c-m-plugin 1.3.20 can create problems and sometimes only shows one item on the classpath that appears to be an empty manifest.

crosspost to clojure-maven-plugin here:

Comment by Kevin Livingston [ 26/Jun/14 8:57 PM ]

also with this java version
Java version: 1.7.0_45, vendor: Oracle Corporation

Comment by Stuart Sierra [ 18/Jul/14 2:38 PM ]

I can confirm this behavior with all versions of java.classpath on all versions of clojure-maven-plugin 1.3.13 and later.

Starting with version 1.3.13 of clojure-maven-plugin, instead of specifying the Java classpath on the command line with -cp, the plugin generates a temporary JAR file with a manifest file containing the real classpath and launches Java with -jar.

See clojure-maven-plugin pull request #58 containing commit 8d8e90e4.

clojure.java.classpath/classpath looks at the classloader that loaded Clojure, which in this case is a child classloader of sun.misc.Launcher$AppClassLoader. AppClassLoader is a subclass of URLClassLoader but its getURLs method returns only the URL of the JAR file. There is no obvious API for getting the Class-Path property of the manifest file without opening and reading the contents of the JAR file.

Comment by Stuart Sierra [ 18/Jul/14 4:10 PM ]

On further digging in the OpenJDK source code, URLClassLoader reads JAR manifest files lazily, as needed, to load classes or resources. To find the list of all URLs that a URLClassLoader might open, you would have to reimplement this logic to read all the manifest files. It also has to be recursive (JAR manifest references another JAR with a manifest) and detect cycles.

Comment by Kevin Livingston [ 19/Jul/14 3:52 PM ]

Thank you for looking into this.
Am I understanding that this is the correct / desired behavior for what clojure-maven-plungin and clojure.java.classpath should be doing together?

So I was going into jar files, (although I've realized I don't think I'm looking into nested jar files - does the classloader look into nested files?) and now you are saying there's a third case I will have to cover too, right?: manifest files? Is there a utility method in clojure.java.classpath for reading them?

I was using code like this to identify everything accessible on the classpath:



Comment by Stuart Sierra [ 22/Jul/14 6:58 AM ]

Currently, clojure.java.classpath handles neither JAR Manifest files nor nested JAR files. As I described in my previous comment, implementing support for this would be difficult and is not likely to be a priority for me any time soon. I would be willing to review a patch to add this capability, but it would have to prove that it protects against recursive cycles.

By my reading of the JAR File Specification, the behavior of clojure-maven-plugin could be considered against the spirit of the JAR spec, which decribes the "Class-Path" manifest attribute this way:

"Class-Path: The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs." (emphasis added)

The clojure-maven-plugin is creating a JAR manifest with a "Class-Path" attribute containing absolute URLs to other JAR files which are not embedded in the JAR containing the manifest.

I have always assumed that JAR manifest files with Class-Path attributes are used only when packaging complete applications or JDK extensions, not as a mechanism for specifying the class-path during development. Clearly it is possible, as the current clojure-maven-plugin demonstrates, but it is not a feature I would want to rely on.

In general, the JDK does not support enumerating all files on the classpath, since the classpath can include URLs to remote resources which are downloaded as needed.

Comment by Kevin Livingston [ 22/Jul/14 12:20 PM ]

Interesting. I didn't realize some of this was so flexible.
Thanks for your comments.

So iterating recursively over the CP seem like it might be a bad thing to do?, although I guess it's no worse than if you happened to ask for something to be loaded that's found in the very last, deepest corner of the CP, right?

I have been putting resource files on the CP and then having the application find them with a prefix of their names. e.g., find all "foo.bar" and I get "foo.bar.baz1" and "foo.bar.baz2" etc. without the application needing to be aware of what's there or the need for manifests to be generated or maintained. Is this an known anti-pattern for working with resources on the classpath?

Comment by Stuart Sierra [ 23/Jul/14 7:13 AM ]

Is this an known anti-pattern for working with resources on the classpath?


[CLASSPATH-8] Empty classpath returned on Java 9 Created: 01/Dec/17  Updated: 09/Dec/17

Status: Open
Project: java.classpath
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Stuart Sierra Assignee: Stuart Sierra
Resolution: Unresolved Votes: 0
Labels: None



When running on JDK 9, (classpath) returns an empty list.

Comment by Stuart Sierra [ 01/Dec/17 2:42 PM ]

Some possibly-relevant links:

App and boot classloaders have changed Java 9 (tobias/clojure-java-9)

Clojure can't be loaded from the boot classpath under java 9 (CLJ-2077)

New Class Loader Implementations (Java 9 Migration Guide), specifically, "The platform class loader is not an instance of URLClassLoader, but, rather, of an internal class."

Comment by Benjamin Albrecht [ 09/Dec/17 8:46 AM ]

I'm using the following code as a workaround:

(extend-type java.lang.ClassLoader
  (urls [_]
    (map #(clojure.java.io/as-url (clojure.java.io/file %))
         (clojure.string/split (System/getProperty "java.class.path")
                               (re-pattern java.io.File/pathSeparator)))))

as a result, (classpath) returns the content of the java.class.path system property, which may be sufficient for most use cases.

Generated at Mon Jan 22 14:34:34 CST 2018 using JIRA 4.4#649-r158309.