1

Two class loaders CL1 and CL2 load two different classes C1 and C2. I would like to invoke a method in C2 from C1. Is this possible via reflection?

I got a tip saying using a helper class loaded by boot strap class loader can solve this as both classes have access to the boot strap class loader. But do not know how?

Could somebody guide me?

2
  • Do you want to invoke static or instance method? If instance method, then how are you going to declare the variable holding the instance of C2? C2 obj? In both cases, the problem is C2 is absent in the text of C1 and C1 could not compile. If it compiles somehow, then you have another C2 loaded with CL1. Anyway, show us the text and tell what and where an error occur. Commented Sep 18, 2012 at 13:51
  • Currently I am loading my class C1 from classloader CL2. I want to eleminate this step. I am doing it via reflection, CL2.loadClass(C2) from C1 as my class is also loaded by CL2. Commented Sep 20, 2012 at 6:02

2 Answers 2

2

The fact that the classes are loaded by two different classloaders doesn't prevent calls. You don't need reflection (but you may if you wish).

Sign up to request clarification or add additional context in comments.

Comments

1

The helper class must be a common superclass or superinterface, loaded by common parent classloader (in particular, bootstrap classloader). If there are no such helper class, then you have to use reflection.

1 Comment

correction: only C2 has to implement interface which is loaded by common parent classloader. Then just access to C2 as I.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.