Link to home
Start Free TrialLog in
Avatar of mtcmedia
mtcmedia

asked on

URLClassLoader How To Use?

     /* Extract Classes From Jar */

        myATP.extractFromJar(fileList1, jarFile);

      /* Load Classes Into JavaVM */

        pathname = jarFile.getPath();
        workingDir = new URL(pathname);
        urls[0] = workingDir;
        try {
            loader = new URLClassLoader(urls);
            loader.findClass("Agent.class");
            loader.findClass("tcpClient.class");
        } catch (Exception e) {
            System.out.println(e);
        }

I get errors with both the loader.findClass lines, the error is:
testReceive.java:59: findClass(java.lang.String) has protected access in java.net.URLClassLoader
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doronb
doronb

The method findClass(..) is a protected method which is a part of the ClassLoader's API to let you build your own customized ClassLoaders, do what CEHJ says, he knows ;)

Use loadClass(..), or better yet, Class.forName(String className, boolean initialize, ClassLoader loader);
Or skip extracting the files from the jar and do the following:

   workingDir = new URL("jar:file:///" + pathname);
   URClassLoader loader = new URLClassLoader(new URL[]{workingDir});
   loader.loadClass("Agent.class");    // provided Agent.class is defined in the default package, otherwise its package is needed as well