Java class finder

"Agh for crying out loud, I know that that Java class is in one of these two hundred JARs, just tell me which one!"

Python 3 because that was quickest

findclass.py:

import os, sys, zipfile

top = sys.argv[1]
# e.g. "C:\\Program Files (x86)"

classname = sys.argv[2]
# e.g. "javax.wsdl.factory.WSDLFactory"

member = "/".join(classname.split(".")) + ".class"
# e.g. "javax/wsdl/factory/WSDLFactory.class"

for dirpath, _, filenames in os.walk(top):
	for filename in filenames:
		if not filename.endswith(".jar"):
			continue
		filename = os.path.join(dirpath, filename)

		with zipfile.ZipFile(filename) as jar:
			if member in jar.namelist():
				print(filename)

Usage:

python findclass.py "C:\\Program Files (x86)" javax.wsdl.factory.WSDLFactory

Back to Code
Back to Things Of Interest

Discussion (4)

2013-06-11 22:20:51 by qntm:

I spent 15 seconds Googling for something like this and found nothing that I could just copy, paste and use immediately. So, here it is.

2013-06-15 15:16:56 by David:

So, I realise that this may be a stupid question; but why not perform a once only extraction of all the details from your jar files, into a text file; which you can leave open in your editor, and search at will?
Seems computationally less expensive.

2013-06-15 21:10:58 by qntm:

Walking the filesystem isn't actually that intensive, and ZIP files are designed to let you inspect their table of contents without unzipping the entire archive, so it's really pretty fast. In my case, a major problem is that I'm constantly uninstalling and reinstalling software - a particular piece of software which is under continuous development - so any index that I created ahead of time would go out of date quite quickly.

2015-12-25 23:19:21 by neo:

In that case, try this. When the program is run:

If there is no index, create an index. If there is an index, search through it for the class. If it is not there, re-index and start over. If it is there, look in the filesystem where the index says it should be. If it is not there, re-index and start over. If it is there, return its location.

The index will speed searching, while avoiding the issue of re-indexing by doing it whenever necessary. The one issue I can see is multiple copies of one class in several directories, which would cause (and indicate) more severe problems anyways.