Solved

URLClassLoader How To Use?

Posted on 2004-04-01
6
465 Views
Last Modified: 2012-06-21
     /* 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
0
Comment
Question by:mtcmedia
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 10731149
Why not call

Class agentClass = loader.loadClass("Agent.class");

?
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 25 total points
ID: 10734351
0
 
LVL 9

Expert Comment

by:doronb
ID: 10820009
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);
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10820964
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
hibernate jars 4 45
Chrome and Firefox Java 5 51
add projects t working set in maven 2 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question