Java: programmatically determine the names of all packages loaded on the classpath
Any suggestions on how to handle how to find the list of package names that exist in the current classpath?
This needs to be done in the program at run time by loading (and executing) one of the classes on the classpath (i.e. internal, not external)
More details:
One approach I consider is to use reflection on each class loaded by the class loader so far and extract the package name from it However, my application already has thousands of courses, so I need a more effective method
I think another thing is similar to finding what jar files are in the classpath, and then executing a parallel directory list for each jar However, I don't know if / how this can be done within the application
Bonus points
Anyone who proposes points that can be filtered through top-level packets For example Show com All packages under XYZ = = > com xyz.*, com. xyz.*.*
thank you!
Solution
If you really need to install and scan jar files, this is built-in If you have to go that route, it may make things easier
Edit #1: you can get the classpath like this (from the example here):
String strClassPath = System.getProperty("java.class.path"); System.out.println("Classpath is " + strClassPath);
From there you can see the local file system classes, jars, etc
Editor #2: This is a VFS solution:
import java.util.HashSet; import org.apache.commons.lang.StringUtils; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystemManager; import org.apache.commons.vfs.FileType; import org.apache.commons.vfs.VFS; public class PackageLister { private static HashSet< String > packageNames = new HashSet< String >(); private static String localFilePath; /** * @param args * @throws Throwable */ public static void main( final String[] args ) throws Throwable { FileSystemManager fileSystemManager = VFS.getManager(); String[] pathElements = System.getProperty( "java.class.path" ).split( ";" ); for( String element : pathElements ) { if ( element.endsWith( "jar" ) ) { FileObject fileObject = fileSystemManager.resolveFile( "jar://" + element ); addPackages( fileObject ); } else { FileObject fileObject = fileSystemManager.resolveFile( element ); localFilePath = fileObject.getName().getPath(); addPackages( fileObject ); } } for( String name : packageNames ) { System.out.println( name ); } } private static void addPackages( final FileObject fileObject ) throws Throwable { FileObject[] children = fileObject.getChildren(); for( FileObject child : children ) { if ( !child.getName().getBaseName().equals( "Meta-INF" ) ) { if ( child.getType() == FileType.FOLDER ) { addPackages( child ); } else if ( child.getName().getExtension().equals( "class" ) ) { String parentPath = child.getParent().getName().getPath(); parentPath = StringUtils.remove( parentPath,localFilePath ); parentPath = StringUtils.removeStart( parentPath,"/" ); parentPath = parentPath.replaceAll( "/","." ); packageNames.add( parentPath ); } } } } }