Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jar

netbeans

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.)

like image 218
Greg Avatar asked Aug 31 '10 13:08

Greg


People also ask

How can you tell what class a jar is from?

The, the idea is to use find on the root of your classpath to locate all jars, then runs findclass.sh on all found jars to look for a match. It doesn't handle multi-directories, but if you carefully choose the root you can get it to work.

How do I find the Java JAR file?

It is often located in the "Program Files\Java" or "Program Files (x86)\Java" folder, within a possible subfolder below the Java folder. Once you find the file, select it and click OK.

Which Java file opens JAR files?

The most common program Java Runtime Environment (JRE) is available to download for free for Windows and macOS operating systems from the Java website. After installing the program, run the . jar file by double-clicking on it.

Where does Java Store JAR files?

All the compiled binaries loaded at JVM start up will be in JAVA_HOME\jre\lib or JAVA_HOME\jre\lib\ext .


2 Answers

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());
// ...
like image 162
BalusC Avatar answered Sep 30 '22 18:09

BalusC


Have you tried doing a 'Open Declaration' on the class? In Eclipse, when you do it, it opens a window that shows the name of the jar and tells you that this jar has 'No Source Attachment'. I am hoping something similar should happen for NetBeans.

Thanks, R

like image 40
Roy Avatar answered Sep 30 '22 18:09

Roy