Solved

URLClassLoader How To Use?

Posted on 2004-04-01
6
441 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now