0

I'm trying to invoke a method via reflection. The method in question, let's say

public void someMethod(someInterface<someObject> arg1)

I do not have access to someMethod and someInterface at runtime, and have to invoke by

someclass.getMethod("someMethod", new Class[]{Class.forName("someInterface")})
         .invoke(...)

But it fails with a ClassNotFound exception for someInterface. How do I get the Class object for interfaces?

1

2 Answers 2

2

I believe that you forgot the interface's package. You have to use fully qualified class name when you are calling Class.forName(), i.e. Class.forName('com.mycompany.MyClass')

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

1 Comment

You are so right. I was pulling my hairs out trying to figure out how to get Class for interface, while I just forgot that I need a fully qualified name!
2

That looks correct to me, conceptually. Check these things:

  • Is the interface on the classpath at run time?
  • Is the interface public (not package private)
  • Is the interface really in the default package (you have to fully qualify it)

Last but not least ;-)

  • Check your spelling (case-sensitive)

Comments

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.