Solved

URLClassLoader How To Use?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

706 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

21 Experts available now in Live!

Get 1:1 Help Now