-1

I have a method like this:

public IMyInterface factoryMethod(Class<? extends IMyInterface> concreteClass){
    IMyInterface o = concreteClass.newInstance();
    //...some stuff
    return o;
}

If method invoked I get exception: java.lang.NoClassDefFoundError: packages/IMyInterface

Seems that it tries to find class IMyInterface and then find its constructor. Of course it fails.

How to fix this method?

Victor Mezrin
  • 2,797
  • 2
  • 32
  • 48
  • 2
    Are you certain IMyInterface is on your classpath? Usually a NoClassDefFoundError is a runtime error raised due to classpath issues. – Stefan Nuxoll Jan 21 '14 at 22:14
  • 1
    Can you create instance of your class without reflection? `NoClassDefFoundError` usually happens when class that your code depends on was presented at class path while compiling, but is not in classpath while executing code. – Pshemo Jan 21 '14 at 22:15
  • Can you show snippets of the rest of your code. For example, IMyInterface? – Jason Jan 21 '14 at 22:17
  • 1
    @Jason But in generics bounds you use `extends` for interfaces as well as classes. – rgettman Jan 21 '14 at 22:19
  • Class cannot extend interface!! Here you can look at very nice explanations and examples for interfaces --> http://www.tutorialspoint.com/java/java_interfaces.htm – i-bob Jan 21 '14 at 22:22
  • @i-bob Read rgettman's comment (the one above your comment). Also take a look [here](http://stackoverflow.com/q/976441/1393766). – Pshemo Jan 21 '14 at 22:24
  • That was issue with classpath. I work on ear app. And I had issue with links between jar packages. – Victor Mezrin Jan 22 '14 at 13:08

1 Answers1

1

I tested your code and its working fine. But i was also able to reproduce your issue.

Assuming, you are calling

factoryMethod(MyClass.class);

Then, if the class "MyClass" is inside its OWN Classfile its working fine with just providing

concreteClass.newInstance();

When having the implementation of the class as a inner Class Type, the Class not found exception is thrown.

The reason is, that a inner class is actually a type of a nested class, and therefore creating a instance is only possible, when working with the enclosing Class, also.

In the example given bellow, the newInstance() method has been used along with an inner class.

  public class Test {

    public class MyClass implements MyInterface {
    }

    public interface MyInterface {
    }

    public static void main(String[] args){
        MyInterface ret = method(MyClass.class);

        if (ret instanceof MyClass){
            System.out.println("Is MyClass");
        }
    }

    public static MyInterface method(Class<? extends MyInterface> actualClass){
        try {
            Class<?> enclosingClass = Class.forName(actualClass.getDeclaringClass().getCanonicalName());
            Object enclosingInstance = enclosingClass.newInstance();

            Class<?> innerClass = Class.forName(actualClass.getName());
            Constructor<?> constructor = innerClass.getDeclaredConstructor(enclosingClass);

            Object innerInstance = constructor.newInstance(enclosingInstance);
            return (MyInterface) innerInstance;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;    
    }
}
dognose
  • 20,360
  • 9
  • 61
  • 107