问题
I have a Java project with a large classpath - many of the items are imported from different .jar files that have been added as dependencies (via Maven).
I'm looking for a programmatic way to list all the items on the classpath that come from the current project, i.e. excluding all the imported classpath items from other libraries and the Java runtime environment itself.
I need to do this at runtime, i.e. it should work off the actual current classpath.
Is there a good way to do this, and if so how?
回答1:
There is no way to do this unless you create your own metadata.
To the JVM, there is no distinction between items in the classpath. A jar is a jar is a jar.
You can use the maven-dependency-plugin to write out a list of the dependencies that are local, and not transitive, and write that into a file, and put that file in your application.
来源:https://stackoverflow.com/questions/13811981/listing-items-on-the-java-classpath-from-the-current-project-only