6

I'm making a code editor and I'm working on the autocomplete. I want to programmatically get a list of all the classes that come with the JDK.

Examples include:

java.io.File
java.util.ArrayList
javax.swing.Action

I've found ways to get classes for a specific package. For instance, I can get all classes that start with com.mypackage.foo. The problem is that I'm trying to get classes that were loaded with the Bootstrap ClassLoader. And on the OSX JDK, that classloader shows up as null. For instance, if I do String.class.getClassLoader(), that is null.

Any ideas?

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
satnam
  • 10,719
  • 5
  • 32
  • 42
  • This [SO question](http://stackoverflow.com/questions/2681459/how-can-i-list-all-classes-loaded-in-a-specific-class-loader) might help. – Aniket Thakur May 02 '15 at 07:46
  • Yeah I saw that one when I was searching google for hours lol. Don't want to do instrumentation because it's hacky and requires giving JVM args. The bottom "hackerish" solution only gives classes that have already been loaded. So when I ran it it only showed 2 classes, not thousands. – satnam May 02 '15 at 07:49

1 Answers1

2

You can open rt.jar with java.util.jar.JarFile and iterate over its entries. This is how can get URL to rt.jar : URL url = Object.class.getResource("String.class");

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • How does my program know where rt.jar is located? I want this to work on all OS's and all computers, not just mine. – satnam May 02 '15 at 07:50
  • the getResource(String.class) way of doing it seems to work well. That gives the path to the jar. So i'll use that to index all the classes. Thanks! – satnam May 02 '15 at 07:55