Hi all,

  I am storing the required test.jar file in the /sdcard. I want to
load it dynamically at runtime and want to execute a function xyz()
resides in that. For this purpose

  I had written following code ,

But got ClassCastException : dalvik.system.PathClassLoader

Following is my code ,

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Collabera extends Activity {
    /** Called when the activity is first created. */

        private static final Class[] parameters = new Class[] { URL.class };

         public static void loadURLClass(String classPathURL) throws
IOException {

          File f = new File(classPathURL);
          URL url = f.toURL();

          URLClassLoader systemLoader = (URLClassLoader) ClassLoader
            .getSystemClassLoader();
          Class systemLoaderClass = URLClassLoader.class;
          try {
           Method method = systemLoaderClass.getDeclaredMethod("addURL",
             parameters);
           method.setAccessible(true);
           method.invoke(systemLoader, new Object[] { url });
          } catch (Throwable t) {
           t.printStackTrace();
           throw new IOException(
             "Error, could not add URL to system classloader");
          }
         }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       Log.i("See","**************Before Loading Class
Path**************");
     try {
      Class.forName("test.Test1");
     } catch (ClassNotFoundException e) {
      System.out.println(" Test Class Not Found ....");
     }

     Log.i("See","**************After Loading Class
Path**************");

     try {
      loadURLClass("//sdcard//test.jar");
      Class c = Class.forName("test.Test1");
      Log.i("See"," Test Class Found ....");

      Method method = c.getMethod("xyz", null);

      Object o = c.newInstance();
      String s = (String) method.invoke(o);
      Log.i("See","Got method: " + s);
     } catch (ClassNotFoundException e) {
      System.out.println(" Test Class Not Found ....");
     } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }     }
}


Please help. Thanks in advance.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to