deprecated
deprecated

Reputation: 5242

Determine the location of a Java package

I need to find the jar from a Java project that provides a certain logical Java package (e.g. com.example.functionality), but there are hundreds of them, and their names aren't particularly useful.

How to find out the mappings that are created between dirs/files/jars and packages/classes?

Upvotes: 1

Views: 176

Answers (3)

Ian Roberts
Ian Roberts

Reputation: 122364

For a one-off search, http://www.jarfinder.com/ is handy. It has in impressive index, which seems to know about everything in Maven Central as well as many other download sites around the web, and lets you search by class name to find which JARs contain that class.

Upvotes: 1

vlad
vlad

Reputation: 56

obj.getClass().getProtectionDomain().getCodeSource()

See: javadoc

Upvotes: 4

David Lavender
David Lavender

Reputation: 8311

You can do it in code:

Class myClass = Class.forName("com.example.functionality");

// eg. /com/example/functionality.class
String classfilePath = '/' + myClass.getName().replace(".", "/") + ".class";

URL location = myClass.getResource(classfilePath);

That URL will be the JAR file (or the class folder if it isn't in a jar). Slightly hacky though - may not work for all classloaders.

Upvotes: 1

Related Questions