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

  • Home
  • SEARCH
  • 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 6018189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:14:20+00:00 2026-05-23T03:14:20+00:00

Let’s say I have three classes, example.ClassA, example.ClassB, and example.ClassLoader. ClassA prints out HelloWorld

  • 0

Let’s say I have three classes, example.ClassA, example.ClassB, and example.ClassLoader. ClassA prints out HelloWorld and ClassB imports example.ClassA and invokes its main() method. If I do this:

java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassA

It works and uses my class loader. However, if I do this:

java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassB

ClassB uses my class loader, but ClassA (which was imported by ClassB) is loaded using a default class loader.

Is there any way to force Java to always use my class loader (unless another class loader is specified explicitly)?

EDIT: Thanks to Paŭlo Ebermann’s answer below, I figured the problem is that I’m calling the parent class loader (URLClassLoader) to load the classes that I don’t need to touch, and those loaded classes set that as it’s context class loader, so classes imported from it uses the parent class loader of my custom loader. (confusing, sorry) Now I can get it to work by manually reading in EVERY class, however it seems redundant as I’ve copied URLClassLoader’s code directly. Is there a way to tell the parent class loader to find and define the class, BUT set the Class’s context class loader to your custom one?

  • 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-23T03:14:21+00:00Added an answer on May 23, 2026 at 3:14 am

    If your class loader is implemented right, it will first ask its parent class loader about any classes that should be loaded.

    The parent class loader of your loader will likely be the usual application class loader. This means that every class your class loader loads will first searched on the application class loader, and only if not found, on your one.

    All classes which are defined by your class loader will also search their needed classes on your classloader. If they don’t do, your ClassA is not really loaded by your loader.

    If this does not help, you will need to show some code on how you got to your results.


    An idea on what to do:

    class ModifyingClassLoader extends URLClassLoader {
    
        // TODO: add constructors
    
        private boolean needsModifying(String name) {
            // TODO
        }
    
        private byte[] modifyClass(InputStream original) throws IOException {
            // TODO
        }
    
        public Class<?> findClass(String name) throws {
            if(needsModifying(name)) {
                try {
                    InputStream classData = getResourceAsStream(name.replace('.', '/') + ".class");
                    if(classData == null) {
                        throw new ClassNotFoundException("class " + name + " is not findable");
                    }
                    byte[] array = modifyClass(classData);
                    return defineClass(name, array, 0, array.length);
                }
                catch(IOException io) {
                    throw new ClassNotFoundException(io);
                }
            }
            else {
                return super.findClass(name);
            }
        }
    }
    

    To your question:

    Is there a way to tell the parent class loader to find and define the class,
    BUT set the Class’s context class loader to your custom one?

    No. The ClassLoader of a class is always the one whose defineClass method was called to create the class. (The context class loader is something else – it is thread specific, and only used by classes which explicitly want to use it, not by classes resolving their own direct dependencies.)

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

Sidebar

Related Questions

let's say i have three tables, each one relates to another, when i need
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a class like this: public class Person { private String
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i

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.