[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

URLClassLoader How To Use?

Posted on 2004-04-01
6
Medium Priority
?
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 86

Accepted Solution

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

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

?
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 100 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

650 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