Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:39:49+00:00 2026-05-24T21:39:49+00:00

As part of an HTTP WebServer project for academic purposes, I’m trying write my

  • 0

As part of an HTTP WebServer project for academic purposes,
I’m trying write my own custom class loader for web application classes, but can’t seem to get it right.

Generally speaking, web applications are located in their own folders, the “.class” file of the web application is the same as it’s direct parent folder name. (e.g. Web1/Web1.class).
The below code works fine until I reach the defineClass() method, and then it throws me out with the following exception:

java.io.FileNotFoundException: C:\inetpub\javawwwroot\WebApps\java\lang\Object\.Object.class (The system cannot find the path specified)

It’s well worth to mention that the C:\inetpub\javawwwroot\WebApps\ part equals m_WebAppsFullPath variable in the code below.

Also, when trying to use

InputStream in = getResourceAsStream(clsFile);

instead of InputStream in = new FileInputStream(clsFile);

I get a null return value…

UPDATE: In short, how can I load a specific class which isn’t located in the “CLASSPATH” nor located in any of the packages of my project?

protected synchronized Class loadClass(String className, boolean resolve) 
                             throws ClassNotFoundException 
{
    log("Loading class: " + className + ", resolve: " + resolve);

    // 1. is this class already loaded?
    Class cls = findLoadedClass(className);
    if (cls != null)
    {
        return cls;
    }

    // 2. get class file name from class name
    String classRelativePath = className.replace('.', '/');
    String classFileName = 
        ((className.lastIndexOf('.') != -1) ? className.substring(className.lastIndexOf('.')) : className) + ".class";

    String clsFile = m_WebAppsFullPath + "\\" + classRelativePath + "\\" + classFileName;

    // 3. get bytes for class
    byte[] classBytes = null;
    try 
    {
        //InputStream in = getResourceAsStream(clsFile);
        InputStream in = new FileInputStream(clsFile);
        byte[] buffer = new byte[BUFFER_SIZE];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int n = -1;
        while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
            out.write(buffer, 0, n);
        }
        classBytes = out.toByteArray();
    }
    catch (IOException e) {
        log("ERROR loading class file: " + e);
    }

    if (classBytes == null) {
        throw new ClassNotFoundException("Cannot load class: " + className);
    }

    // 4. turn the byte array into a Class
    try {
        cls = defineClass(className, classBytes, 0, classBytes.length);
        if (resolve) {
            resolveClass(cls);
        }
    }
    catch (SecurityException e) { 
        // loading core java classes such as java.lang.String
        // is prohibited, throws java.lang.SecurityException.
        // delegate to parent if not allowed to load class
        cls = super.loadClass(className, resolve);
    }

    return cls;
}

Any idea how can I get it to work?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:39:49+00:00Added an answer on May 24, 2026 at 9:39 pm

    At the moment, you’re not only trying to load your custom classes via your own class loader, but all the sytem clases they depend on as well. Like e.g. java.lang.Object, which is your Problem here.

    Generally, classloaders in Java are chained, which means, that your classloader has been defined by another one (most likely the system classloader). Therefore, it is advisable, to NOT overwrite the loadClass method yourself, but instead overwrite the two methods ‘findClass(String):Class and loadClassData(String):Class.

    Here is an excerp from the Javadoc of the Classloader class:

    The ClassLoader class uses a delegation model to search for
    classes and resources. Each instance of ClassLoader has an
    associated parent class loader. When requested to find a class or
    resource, a ClassLoader instance will delegate the search for the
    class or resource to its parent class loader before attempting to find the
    class or resource itself. The virtual machine’s built-in class loader,
    called the “bootstrap class loader”, does not itself have a parent but may
    serve as the parent of a ClassLoader instance.

    The same Javadoc even lists an example, how to define a custom classloader properly:

    class NetworkClassLoader extends ClassLoader {
         String host;
         int port;
    
         public Class findClass(String name) {
             byte[] b = loadClassData(name);
             return defineClass(name, b, 0, b.length);
         }
    
         private byte[] loadClassData(String name) {
             // load the class data from the connection
              . . .
         }
     }
    

    I think you might want to read that: Javadoc for ClassLoader

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple http web server, that (among other features), can
In the Upshot.js library, that is part of the http://www.asp.net/single-page-application feature of ASP.NET MVC
I'm following this tutorial http://blogs.msdn.com/b/astoriateam/archive/2010/07/21/odata-and-authentication-part-6-custom-basic-authentication.aspx . I need to add to the web.config :
Even though it's not part of HTTP 1.1/RFC2616 webapps that wish to force a
http://www.christywhiteheadphotography.com/CWPTEST/ (wordpress site) Part of my theme uses the widgets shown under the slider.
http://www.geocities.com/CapeCanaveral/Hall/2027/alfabeto.txt part of it: procedure f /martina go 20 rt 90 go 10 lt
this is part of my code which reads an http response. It's supposed to
I'm going through the railscast no 73 ( http://railscasts.com/episodes/73-complex-forms-part-1 ), where Ryan Bates explains
In pom.xml I have (taken from http://struberg.wordpress.com/2012/05/10/using-jpa-in-real-projects-part-1/ ): <properties> <openjpa.sql.action>refresh</openjpa.sql.action> <database.driver.name>org.postgresql.Driver</database.driver.name> <database.connection.url>jdbc:postgresql://myURL</database.connection.url> <database.user>user</database.user> <database.password>password</database.password>
I have the following Facebook iframe as part of the template: <iframe allowTransparency='true' expr:src='&quot;http://www.facebook.com/plugins/like.php?href=&quot;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.