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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:20:56+00:00 2026-05-24T22:20:56+00:00

I’m trying to reference an object loaded via a classloader in a class that

  • 0

I’m trying to reference an object loaded via a classloader in a class that is loaded by another classloader. I need to do a cast-down that object to access some methods. I need a way to do this casting avoiding the other classloader.
I know that we can’t cast classes of differnt classloaders, and that is not what I need here. I need to do the casting in the same classloader avoiding the other classloader which has loaded the same class. It turned out really difficult.

Here’s a little context. As you see in the classes below, I receive an object at MyClass.configure() method via the method ComponentResolver.lookup() which is an instance of Mojo but casted to Object.

Unfortunately, Mojo mojo = clazz.cast(o) (in MyClass below) fails with a compilation error, saying the returned type of clazz.cast is Object. Can anyone tell me how to resolve this? May be via the same Class#cast method or via reflection? I’m not much familiar with reflection though!

//This is loaded by, say ClassLoader X
public class ComponentResolver {
    public Object lookup(String role) {

        //do something
        return component;     //component is an instance of Mojo interface.
    }
}

Here’s MyClass that where I invoke the lookup method.

//This class including Mojo in this context is loaded by, say ClassLoader Y
public class MyClass {
    public void configure() {
        Object o = componentResolver.lookup("componentName");

//      Mojo mojo = (Mojo) o;  //causes classcastexception (obviously.)

        Class<?> clazz = Class.forName("org.Mojo", false,
                o.getClassLoader()   );

        Mojo mojo = clazz.cast(o);
    //Causes compiler errors because the returned object is of type Object. 
    //ie incompatible types Required:Mojo, Found:Object

       // Mojo mojo = (Mojo) clazz.cast(o); //again classcastexception.

        mojo.execute();
       }

}
  • 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-24T22:20:58+00:00Added an answer on May 24, 2026 at 10:20 pm

    The Mojo class loaded by your classloader and the Mojo class loaded by the other classloader are completely different classes as far as the JVM is concerned.

    Some options:

    • Do whatever you need to in terms of invoking methods etc via reflection
    • Create a new Mojo based on the existing one via reflection, then using that (it depends what Mojo is really doing)
    • Change your classloader hierarchy so that one classloader delegates to another one, so that you can just cast.

    You’re in a fundamentally nasty position, and there are no easy workarounds that I’m aware of. If you can possibly fix the classloader hierarchy, it’ll make your life a lot easier. Just having Mojo in a classloader which both of the other classloaders have as their parent would be enough.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.