Get list of all packages loaded by the current classloader

To get the list of known packages loaded by the current classloader, call the method Package.getPackages().

Main.java:

import java.util.*;
 
public class Main {
   public static void main(String args[]) {
      Package packages[] = Package.getPackages();
 
      // sort the list of packages
      Arrays.sort(packages, new Comparator() {
         public int compare(Object c1, Object c2) {
            String n1 = ((Package) c1).getName(); 
            String n2 = ((Package) c2).getName();
            return n1.compareTo(n2);
         }
      });
 
      for (int i=0; i<packages.length; i++) {
         System.out.println(packages[i].getName());
      }
   }
}

outputs:

java.awt
java.io
java.lang
java.lang.ref
java.lang.reflect
java.net
java.security
java.security.cert
java.util
java.util.jar
java.util.zip
sun.io
sun.misc
sun.net.www
sun.net.www.protocol.file
sun.net.www.protocol.jar
sun.security.action
sun.security.provider
sun.security.util