سؤال

I'm developing an android test app and i'm going to access all internal class of android.view package. android.view is a package that is present in jar file. I tried by loading package name but it doesn't display the classes if any one tried this already, please help.

Here's what I tried so far:

public static void main() throws ClassNotFoundException{
    Class o =Class.forName("android.view");
    Class[] C=o.getDeclaredClasses();
    for(int i=0;i<C.length;i++) {
    Classname = C[i].getName();
    ClassesDisplayActivity.your_array_list3.add(Classname);
   Log.i("Ramu","classname "+ C[i].getName());
 }
}
}
هل كانت مفيدة؟

المحلول 2

Package is not a class. You cannot call Class.forName() for package and access classes that belong to class using getDelcaredClasses().

I do not know what do you really need, so I'd recommend you to explain this in separate question. probably you will receive better solutions.

However if you really need this you have to do the following:

  1. Get your classpath by calling System.getProperty(java.class.path)
  2. split this property to its elements by colon
  3. iterate over the list and read each resource. If resource is jar you can use ZipInputStream, if it is a directory use File class.
  4. filter list of resources you got at #3.

Fortunately you can use 3rd party library named Reflections that helps you to do all this without writing code.

نصائح أخرى

It is not possible to determine at runtime all of the classes that are in a package using a standard class loader.

You might have some luck with this library though:

https://code.google.com/p/reflections/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top