4

I have a runnable jar with two jars in the Class-Path entry of its manifest file:

Class-Path: module1-0.0.1-SNAPSHOT.jar base-0.0.1-SNAPSHOT.jar
Main-Class: test.MySPI

The program runs fine and all dependencies in the referenced jars are met. However, when I try to access the class path, the jars are not there:

String classpath = System.getProperty("java.class.path");
String[] entries = classpath.split(System.getProperty("path.separator"));
for (String entry : entries) {
    System.out.println("Entry: " + entry);
}

Only gives

Entry: .\module2-0.0.1-SNAPSHOT.jar

Is there a way of accessing the actual classpath, since obviously, the system finds those jars on the path?

2 Answers2

6

I think you will need to use the Manifest class to read in the MANIFEST.MF file and extract the Class-Path attribute using that class

  • You are right! This is handy if you need to deal with Java specification line brake/wrapping for Class-Path entry in Manifest. I had this problem and this answer helped me a lot! In deployed web app I used this code fragment: new Manifest((InputStream) httpRequest.getServletContext().getResource("/META-INF/MANIFEST.MF").getContent()); SN: http://stackoverflow.com/questions/7402271/maven-archiver-putting-in-weird-line-breaks-in-classpath-for-manifest – David Jul 10 '15 at 13:13
3

Do you actually need the location of the JAR files, or do you just need to load resources from them?

If you actually just want to load resources, you'll be interested in java.lang.ClassLoader, its static getSystemClassLoader() method, and the static java.lang.Thread.currentThread().getContextClassLoader() method.

If you wanted to find what they were because you wanted to read the version string off the Jar file name ... and if they were under your control anyhow ... you might want to use Package.getPackage("my.package").getImplementationVersion() and organise to have the required values written into the Manifest of the component jars.

David Bullock
  • 6,112
  • 3
  • 33
  • 43
  • 1
    Actually, I wanted to get details about all jar files on my classpath to specify versions for all modules. I solved it by doing this.getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"); – David Göransson Jan 04 '11 at 13:53