Interface is not visible from ClassLoader when using a proxy?

I am seeing following exception when I try to use dynamic proxy

 com.intellij.rt.execution.application.AppMain DynamicProxy.DynamicProxy
Exception in thread "main" java.lang.IllegalArgumentException: interface Interfaces.IPerson is not visible from class loader
    at java.lang.reflect.Proxy.getProxyClass(
    at java.lang.reflect.Proxy.newProxyInstance(
    at DynamicProxy.Creator.getProxy(
    at DynamicProxy.DynamicProxy.main(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(

Any idea what I need to do to resolve it

Asked by: Sienna957 | Posted: 28-01-2022

Answer 1

If this is web application, then you should use the web application classloader when creating dynamic proxy. So, for example instead of:

  new Class < ? >[] {MyInterface.class},
  new InvocationHandler() {
    // (...)


  this.getClass().getClassLoader(), // here is the trick
  new Class < ? >[] {MyInterface.class},
  new InvocationHandler() {
    // (...)

For instance, hierarchy of tomcat class loaders (other web containers have similar) is following:

       /     \
  Webapp1   Webapp2 ... 

And it is the webapp classloader which contains classes and resources in the /WEB-INF/classes directory of your web application, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application.

Answered by: Darcy906 | Posted: 01-03-2022

Answer 2

When your DynamicProxy tries to do Class.forName(youInterfaceClass.getName()) the resulting java.lang.Class instance is different from the one you passed when you created the proxy. In other words you have two class objects with the same name and the proxy is not sure which one is the right one (doesn't matter whether they are the same).

Usually, this happens when the interface you are trying to proxy is in a library loaded through two different classloaders (i.e. Tomcat's 'common' and 'application').

If this doesn't help, please post more info on your application - especially if you are using any application server, Spring or OSGi.

Answered by: Adelaide221 | Posted: 01-03-2022

Answer 3

I had the same problem using spring boot, I solve it injecting ResourceLoader, getting its class loader.

private ResourceLoader resourceLoader;

ClassLoader classLoader = resourceLoader.getClassLoader();

  classLoader, //for example...
  new Class < ? >[] {MyInterface.class},
  new InvocationHandler() {
  // (...)

Answered by: Elian108 | Posted: 01-03-2022

Similar questions

classloader - Why can't the Java class loader find my interface?

In the code below I generate a class dynamically using I will create a new instance of this class using Reflection. The problem is, I want to avoid using Reflection to invoke the method I defined for this class, so I created a ProxyInvoker that references an interface I defined in my project. In order for the classloader to see this, I add the classpath to the Executable interface to m...

classloader - Why does a class containing a method call to a missing Interface within unused code cause a Java class loading error?

I'm seeing some class loading behavior that appears to be inconsistent with the JVM spec and am wondering if this is a bug. Or if not, hoping someone can explain why. The example code found below simply prints hello from its main method. It has an unused method that contains a method call to a method that declares it takes a 'C' (which is an interface) as an argument. When the main is executed (withou...

java - Use object created by Classloader without interface call or reflect invoke?

I'm using java to do some thing as same as C++ Dynamic Library usage. I didn't find the way to directly use the Same Class Object without reflect invoke style code. this is my dynamic library code, I make it a jar. package com.demo; public class Logic { public String doWork() { System.out.println("Hello from Dll"); return "Dll"; } } In my main appli...

classloader - In Java, is it possible to know whether a class has already been loaded?

Is it possible to know whether a Java class has been loaded, without attempting to load it? Class.forName attempts to load the class, but I don't want this side effect. Is there another way? (I don't want to override the class loader. I'm looking for a relatively simple method.)

java - My own classloader?

Here is the problem I'm running into. There's a huge legacy application that runs on java 1.3 and uses external API, say, MyAPI v1.0. The exact implementation of MyAPI 1.0 is located somewhere in the classpath used by the app. There's also a mechanism that allows that app to use external code (some kind of plugin mechanism). Now I have another java library (MyLib.jar) that uses MyAPI v2.0 (which is NOT 100% backward compat...

java - What reasons have people had to write their own classloader

Closed. This question needs to be more focused. It ...

java - Using the ClassLoader method to retrieve all resources under classes as Input Streams

My problem is one that you would think is quite common, but I haven't so far managed to find a solution. Building a Java web app under Tomcat 5.5 (although a requirement is that it can be deployed anywhere, like under a WebLogic environment, hence the loading resources as streams requirement). Good practice dictates that resource files are placed under WEB-INF/classes and loaded using the ClassLoader's

java - Use a custom classloader at compile time

Is it possible to specify a custom classloader for javac (or some alternative java compiler)? I'd love such a feat because it would allow me to compile classes that use classes that are only found by my special classloader. For the curious ones: I'd write a classloder that connects to a database and creates classes based on the tables it finds.

properties - How do I use the Java ClassLoader to load a file fromthe classpath?

I want to use the ClassLoader to load a properties file for the Properties class. I've simplified the below code to remove error handling for the purposes of this discussion: loader = this.getClass().getClassLoader(); in = loader.getResourceAsStream(""); result = new Properties(); result.load(in); In the same directory as this class I have the file "" but th...

java - Loading files with ClassLoader

This problem has been bugging me for a while. I have to load a couple files in my java app, and the only way I got working so far looks like this: URL hsURL; if(System.getProperty("").toLowerCase().contains("windows")) { hsURL = new URL("file:/" + System.getProperty("user.dir") + "/helpsets/helpset.hs"); } else { hsURL = new URL("file://" + System.getProperty("user.dir") + "/helpsets/helpset....

classloader - Number of classes loaded in java

I have program that the number of classes loaded there is constantly rising. How could this actually be ? Or do i misunderstand something in java about classloading? Here is a snippet from jConsole overnight : alt text Could someone please tell me wh...

Java Classloader - how to reference different versions of a jar

This is a common problem. I'm using 2 libraries A.jar and B.jar and these depend on different versions of the same jar. Let's say that at runtime I need THIS.x.x.x.jar MY.jar -&gt; A.jar -&gt; THIS.1.0.0.jar -&gt; B.jar -&gt; C.jar -&gt; THIS.5.0.0.jar I can compile the specific jar (A.jar/B.jar) against its dependency but...

classloader - java: is there a framework that allows dynamically loading and unloading of jars (but not osgi)?

I want a mechanism that will allow to dynamically load and unload jars as well as calling an activator class in the jar. I don't want to use OSGi because of the cumbersome import/export mechanism.

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)