Search code examples
javanetbeansjar

Java - How to find out which Jar file an import comes from?


The project I'm working on has about 10 jar files as libraries. At the top of one of the files there's an import statement like:

import jpe.nar.crat.maker.ObjectMakerFactory;

Is there a way to tell which Jar file it comes from?

(I'm using Netbeans if that matters.)


Solution

  • You can use CodeSource#getLocation() for this. The CodeSource is available by ProtectionDomain#getCodeSource(). The ProtectionDomain in turn is available by Class#getProtectionDomain().

    URL location = ObjectMakerFactory.class.getProtectionDomain().getCodeSource().getLocation();
    System.out.println(location.getPath());
    // ...