0

如果我们有一个包名,比如说 ..com.google.foo.bar它有像这样的类

com.google.foo.bar.xyz

com.google.foo.bar.abc

. .

如何在运行时获取这些名称?绝对,我的意思是......包名+类名。有什么术语吗?

4

1 回答 1

1
public String getSourceBase(Class<?> classx)
{
    String cn = classx.getName().replace('.', '/') + ".class";
    // like "packagex/SourceBase.class"

    String s = classx.getResource('/' + cn).toExternalForm();
    // like "file:/javadir/Projects/projectX/build/classes/packagex/SourceBase.class"
    // or "jar:file:/opt/java/PROJECTS/testProject/dist/testProject.jar!/px/SourceBase.class"

    return s.substring(0, s.lastIndexOf(cn));
    // like "file:/javadir/Projects/projectX/build/classes/"
    // or "jar:file:/opt/java/PROJECTS/testProject/dist/testProject.jar!/"

}
于 2012-07-28T15:37:21.733 回答