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 9026691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:35:03+00:00 2026-06-16T06:35:03+00:00

Test case below. Output: custom loading of: pkg.TestRun ran. wish this would run in

  • 0

Test case below. Output:

custom loading of: pkg.TestRun
ran.
wish this would run in my custom classloader

What I want to happen is to see “custom loading of: pkg.TestRun” show up between the second and third lines of the output.

How can I get it to load the dependencies of a class from my custom classloader, even when the first class is loaded from the parent? Note, that in my real case, I get a class not found exception because the equivalent of OtherClass is not known to the parent classloader.

I know one solution is to have the custom class loader explicitly load TestRun. However, how to load TestRun is already known to the parent classloader and I don’t want to have to manage finding it separately since it’s already done and it might be tricky for me to somehow figure that out when it’s already being managed without me doing anything. And I’ve tried to do something like super.getResource (returns null) or findClass (already sets parent as classloader for it) but neither worked.

So, can I let the parent find the class, but the custom loader define it? Or, is there just a way to make it so that it will always use my custom loader to look for dependencies?

package pkg;

public class TestCL {

    static class MyCL extends ClassLoader {
        MyCL(ClassLoader parent) {
            super(parent);
        }

        @Override
        public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
            System.out.println("custom loading of: " + name);
            return getParent().loadClass(name);
        }
    }

    public static void main(String[] args) throws Exception {
        MyCL cl = new MyCL(Thread.currentThread().getContextClassLoader());
        Thread.currentThread().setContextClassLoader(cl);
        cl.loadClass("pkg.TestRun").getMethod("run", new Class[] {}).invoke(null);
    }
}

class TestRun {
    public static void run() {
        System.out.println("ran.");
        OtherClass.runAlso();
    }
}

class OtherClass {
    public static void runAlso() {
        System.out.println("wish this would run in my custom classloader");
    }
}
  • 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-06-16T06:35:04+00:00Added an answer on June 16, 2026 at 6:35 am

    One solution is to use the parent classloader (or the one that already knows where it is) to read the bytes of the class, then define it in the custom class loader.

    So, something like this:

    public static byte[] loadBytesForClass(ClassLoader loader, String fqn) throws IOException {
        InputStream input = loader.getResourceAsStream(fqn.replace(".", "/") + ".class");
        if (input == null) {
            System.out.println("Could not load bytes for class: " + fqn);
        }
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        StringUtil.copy(input, output);
        return output.toByteArray();
    }
    

    Then you can call defineClass(name, bytes, 0, bytes.length) with those bytes to define it in the custom class loader.

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

Sidebar

Related Questions

We found some strange values being produced, a small test case is below. This
I am trying to write a test case for the depot application. Below is
Why doesn't this test case work? <?php // cards with cyrillic inidices and suits
I tried this dummy code below to test unnamed namespace. I have the following
See the test case below: $val = floatval(336.00); $result = 300*1.12; header(content-type: text/plain); echo
I have the following test case: @ContextConfiguration(/spring/test-context.xml) @TransactionConfiguration(transactionManager=txManager) @Transactional() public class MyEntityDaoTestCase extends AbstractJUnit4SpringContextTests
I'm writing a test case where I'm trying to use mockito to avoid entering
In writing Test case as I know, first step/task is to identify the Test
The following minimal test case throws an error in powershell: [reflection.assembly]::LoadWithPartialName('system.xml.linq') [system.xml.linq.xdocument]::parse('<node>&</node>') and frankly,
I have a simple test case failing in Django: Model (app/models.py): from django.db import

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.