0

If we have a package name, say .. com.google.foo.bar and it has classes like

com.google.foo.bar.xyz

com.google.foo.bar.abc

. .

How can I get those names at runtime? By absolute, I mean... package name+class name. Is there any term for that?

Community
  • 1
  • 1
questions
  • 2,337
  • 4
  • 24
  • 39
  • 1
    Do you mean: how can I get the list of all classes contained in a specific package at runtime? Or do you simply want the fully qualified class name (package.ClassName) of a specific object at runtime? – assylias Jul 28 '12 at 15:29
  • @assylias - list of all classes.. – questions Jul 28 '12 at 15:33
  • [This post](http://stackoverflow.com/questions/205573/at-runtime-find-all-classes-in-a-java-application-that-extend-a-base-class) could be a starting point. – assylias Jul 28 '12 at 15:34

1 Answers1

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!/"

}
Michael Besteck
  • 2,415
  • 18
  • 10
  • The problem is I'll be read a package name from config file, and based on that I need to find all the classes with their package name. – vidit Jul 29 '12 at 19:28