Traverse .jar File by Using Eclipse JDT

This code example belongs to the Eclipse JDT tutorial series.

If you want to get all class names, method names and field names from a Java jar file, you can use the code below to get them.

This code example is for getting information(e.g. field in a class, method name, method return type, etc.) from Jar files. There is no doubt that we can use eclipse JDT to parse jave source code, but also we can use it to traverse pre-compiled .class files inside of any jar files.

The following code only works under an eclipse plugin project, if you don't know how to do an eclipse go to "How to create a simple Plug-in project".

For parsing source code, we use ICompilationUnit. Here we should use IClassFile instead. The following method shows how to loop each package(i.e., IPackageFragment).

private void processRootDirectoryJar() throws JavaModelException,CoreException {
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	System.out.println("root" + root.getLocation().toOSString());
	IProject[] projects = root.getProjects();
	// process each project
	for (IProject project : projects) {
		System.out.println("project name: " + project.getName());
		if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
			IJavaProject javaProject = JavaCore.create(project);
			IPackageFragment[] packages = javaProject.getPackageFragments();
			// process each package
			for (IPackageFragment aPackage : packages) {
				// We will only look at the package from the source folder
				// K_BINARY would include also included JARS, e.g. rt.jar
				// only process the JAR files
				if (aPackage.getKind() == IPackageFragmentRoot.K_BINARY
						&& aPackage.getElementName().equals("java.lang")) {
					System.out.println("inside of java.lang package");
					for (IClassFile classFile : aPackage.getClassFiles()) {
						System.out.println("----classFile: "
								+ classFile.getElementName());
						// A class file has a single child of type IType.
						// Class file elements need to be opened before they
						// can be navigated. If a class file cannot be
						// parsed, its structure remains unknown.
						// Use IJavaElement.isStructureKnown to determine
						// whether this is the case.
						// System.out.println();
						for (IJavaElement javaElement : classFile
								.getChildren()) {
							if (javaElement instanceof IType) {
								System.out.println("--------IType "
										+ javaElement.getElementName());
								// IInitializer
								IInitializer[] inits = ((IType) javaElement)
								for (IInitializer init : inits) {
											.println("----------------initializer: "
													+ init.getElementName());
								// IField
								IField[] fields = ((IType) javaElement)
								for (IField field : fields) {
											.println("----------------field: "
													+ field.getElementName());
								// IMethod
								IMethod[] methods = ((IType) javaElement)
								for (IMethod method : methods) {
											.println("----------------method: "
													+ method.getElementName());
											.println("----------------method return type - "
													+ method.getReturnType());

Output will contain something like the following. Notice that the return type has capital letter, such as V, L, Z, I. In case you are interested with those capital letters, go to reference 1 to see what they represent.

----classFile: Object.class
--------IType Object
----------------method: Object
----------------method return type - V
----------------method: registerNatives
----------------method return type - V
----------------method: getClass
----------------method return type - Ljava.lang.Class<*>;
----------------method: hashCode
----------------method return type - I
----------------method: equals
----------------method return type - Z
----------------method: clone
----------------method return type - Ljava.lang.Object;
----------------method: toString
----------------method return type - Ljava.lang.String;
----------------method: notify
----------------method return type - V
----------------method: notifyAll
----------------method return type - V
----------------method: wait
----------------method return type - V
----------------method: wait
----------------method return type - V
----------------method: wait
----------------method return type - V
----------------method: finalize
----------------method return type - V
----------------method return type - V

In addition, there is no doubt that you can find similar and more information from source code by parsing each file.


Category >> Java Development Tools (JDT)  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Great post, thanks for sharing!

  • Micky

    Looks like this only looks for JARs and class files in the classpath. What if I wanted to read the class files correspond to the src files of the java project in question? In this case, javaProject.getClassFiles() only returns an empty array. Any idea how I’d get that to work?