Java:以编程方式确定类路径上加载的所有包名称 [英] Java : programmatically determine all of the package names loaded on the classpath

查看:116
本文介绍了Java:以编程方式确定类路径上加载的所有包名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关如何查找当前类路径上存在的包名称列表的任何建议?

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 to be done programmatically at run time by one of the classes loaded (and executing) on the classpath (i.e. inside out, not outside in).

更多详情:

我考虑的一种方法是对类加载器到目前为止加载的每个类使用反射,并从中提取包名称。但是,我的应用程序已经运行到数千个类中,因此我需要一种更有效的方法。

One approach I considered was to use reflection on each of the classes loaded thus far by the class loader, and extract the package names from them. However, my application already runs into thousands of classes, so I need a more efficient approach.

我考虑的另一件事是类似于找出JAR文件中的内容。类路径,然后为每个JAR并行执行目录列表。但是,我不知道这是否可以从应用程序/如何做到这一点。

Another thing I considered was something analogous to finding out what JAR files were in the class path, and then do the directory listing parallel for each JAR. However, I don't know if this is possible from within the application/ how to do it.

奖励积分

任何建议可以按顶级套餐过滤的方式的人的奖励积分。例如。显示 com.xyz ==> com.xyz。* 下的所有软件包com.xyz。*。*

Bonus points for anyone who suggests a way that can filter by top-level packages. E.g. show all packages that come under com.xyz ==> com.xyz.*, com.xyz.*.*

谢谢!

推荐答案

如果您确实需要安装和扫描jar文件,请 commons-vfs 如果你必须走这条路,这可能会让事情变得容易一些。

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.

编辑#1:你可以像这样得到类路径(来自例子) 此处):

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.

编辑#2:以下是VFS的解决方案:

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

这篇关于Java:以编程方式确定类路径上加载的所有包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆