codereviewanskquestions
codereviewanskquestions

Reputation: 14008

java.lang.ClassNotFoundException error using enum as a key in Map

<util:map id="myMap" key-type="com.myClass.Foo.myEnum" value-type="com.myClass.Foo">
    <entry>
        <key>
            <value type="com.myClass.Foo.myEnum">ONE</value>
        </key>
        <ref bean="myObj"/>
    </entry>
</util:map>

Java:

package com.myClass
public class Foo {
    public enum myEnum {ONE, TWO;}
}

I am trying to create a map from Spring 2.5.

Map<myEnum, Foo> myMap;

I am getting

nested exception is java.lang.ClassNotFoundException:com.myClass.Foo.myEnum

I definitely have com.myClass.Foo.myEnumin com.myClass.Foo I don't know why I am getting java.lang.ClassNotFoundException.

Upvotes: 1

Views: 5144

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280181

Your enum class' fully qualified name is

com.myClass.Foo$myEnum

not

com.myClass.Foo.myEnum

Switch it and it will work. Spring uses reflection, with Class.forName() to get the Class object for your class and to instantiate an object. forName() expects a fully qualified name. Read this to understand why your class name contains a $.

Note that in newer versions of Spring, there's a catch block that catches the ClassNotFoundException and tries forName again after replacing the last . with a $.

Upvotes: 3

Related Questions