Java : programmatically determine all of the package names loaded on the classpath

后端 未结 3 609
面向向阳花
面向向阳花 2021-01-11 17:59

Any suggestions as to how to approach how I would find out a list of package names that exist on the current classpath?

This needs

3条回答
  •  星月不相逢
    2021-01-11 18:36

    If you do need to mount and scan jar files commons-vfs has this built in. That might make things a little easier if you have to go that route.

    EDIT #1: You can get the classpath like so (from the example here):

    String strClassPath = System.getProperty("java.class.path");
    System.out.println("Classpath is " + strClassPath);
    

    From there you can look at the local file system classes, jars, etc.

    EDIT #2: Here is a solution with VFS:

    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 );
                    }
                }
            }
        }
    }
    

提交回复
热议问题