title: Programmatically determine list of JRE classes that need not be imported
body: <p>I need to programmatically find out which JRE classes can be referenced in a compilation unit without being imported (for static code analysis). We can disregard package-local classes. <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.3" rel="nofollow noreferrer">According to the JLS</a>, classes from the package <code>java.lang</code> are implicitly imported. The output should be a list of <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#name" rel="nofollow noreferrer">binary class names</a>. The solution should work with plain Java 5 and up (no Guava, <a href="http://code.google.com/p/reflections/" rel="nofollow noreferrer">Reflections</a>, etc.), and be vendor agnostic.</p>
  
  <p>Any reliable Java-based solution is welcome.</p>
  
  <hr>
  
  <p>Here are some notes on what I've tried so far:</p>
  
  <p>At first glance, it seems that the question boils down to "How to load all classes from a package?", which is of course practically impossible, although several workarounds exist (e.g. <a href="https://stackoverflow.com/q/520328/1005481">this</a> and <a href="https://stackoverflow.com/q/1156552/1005481">this</a>, plus the blog posts linked there). But my case is much simpler, because the multiple classloaders issue does not exist. <code>java.lang</code> stuff can always be loaded by the system/bootstrap classloader, and you cannot create your own classes in that package. Problem is, the system classloader does not divulge its class path, which the linked appoaches rely on.</p>
  
  <p>So far, I haven't managed to get access to the system classloader's class path, because on the HotSpot VM I'm using, <code>Object.class.getClassLoader()</code> returns <code>null</code>, and <code>Thread.currentThread().getContextClassLoader()</code> can load <code>java.lang.Object</code> by delegation, but does not itself include the classpath. So solutions like <a href="https://stackoverflow.com/q/9220887/1005481">this one</a> don't work for me. Also, the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties%28%29" rel="nofollow noreferrer">list of guaranteed system properties</a> does not include properties with this kind of classpath info (such as <code>sun.boot.class.path</code>).</p>
  
  <p>It would be nice if I didn't have to assume that there is an <em>rt.jar</em> at all, and rather scan the list of resources used by the system classloader. Such an approach would be safer with respect to vendor specific JRE implementations.</p>