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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:21:56+00:00 2026-05-25T03:21:56+00:00

I have a tricky question and I’m not sure if it is even possible

  • 0

I have a tricky question and I’m not sure if it is even possible in Java. I’m in the following
situation:

I got a class A that uses another class, let’s call it B. Now, I’m trying to write a third class (and I don’t call it C), I call it B again (to be sure which class ‘B’ I mean, I will call it B1 and B2 in the rest of this post, ok?). All three classes resides in three different JAR-files. So far, so good.

Normaly, class A finds class B(1) on the classpath and will use it. But now I’m putting the JAR-file that contains B(2) at the very beginning of the classpath, so class A will find this class instead of the old one. So my class B(2) can act like the old class B(1) (which is a library in reality, that I can’t customize in any other way. that’s why I am doing that…).

And here comes my problem: In my class B(2) I want to load the real class B(1) and use it. I can do this so far by using reflection. I can even invoke methods via reflection, but I can’t cast an instance of the loaded class to B reference. Here is the exception:

java.lang.ClassCastException: my.a.ClassB incompatible with my.a.ClassB

Has anybody an idea how I can use class B(1) in class B(2)? I am happy with any workarround…

Thanx, Thomas.

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

    You can only do this through reflection.

    As far as the runtime system is concerned, your two ClassB classes are complete separate entities and cannot be cast to each-other.

    This sometimes happens in OSGi environments (which have complex classloader setups) or if you somehow manage to pass data between web application contexts.

    If you want a common interface to call methods on both without reflection, then you need to create just that: a common interface (or parent class). And that interface needs to reside in a jar file that both these ClassB can see.

    Example:

    first jar: interface I

    second jar: class B implements I

    third jar: class B implements I

    Now you have two versions of class B, but they can both be cast to I.

    Needless to say, you should find a better solution to whatever problem you have here.

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

Sidebar

Related Questions

I have the following tricky problem: I have implemented a (rather complicated) class which
I have a tricky CSS situation here. Basically I have nested span tags that
I have a very tricky question for you all Suppose I have the following
I have a very tricky assignment. There is a webpage that uses frameset. I
Just a small question. Say I have the following HTML: <div class=foo> {text} </div>
I have a tricky problem that I've been messing about with for a few
I have a tricky problem that has had me scratching my head for a
I have a very tricky situation (for my standards) in hand. I have a
We've got into a very tricky scenario in a project. We have used lot
I have a little tricky question. I have a Telerik Grid with some ClientSide

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.