Follow the Stackoverflow question about: Spring/Java error: namespace element ‘annotation-config’ … on JDK 1.5 and higher to find the solution for fixing the error: java.lang.IllegalStateException: Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher

From Andrei Stefan

The class that’s throwing the exception is using this code to check for Java version:

static {
        javaVersion = System.getProperty("java.version");
        // version String should look like "1.4.2_10"
        if (javaVersion.indexOf("1.7.") != -1) {
            majorJavaVersion = JAVA_17;
        }
        else if (javaVersion.indexOf("1.6.") != -1) {
            majorJavaVersion = JAVA_16;
        }
        else if (javaVersion.indexOf("1.5.") != -1) {
            majorJavaVersion = JAVA_15;
        }
        else {
            // else leave 1.4 as default (it's either 1.4 or unknown)
            majorJavaVersion = JAVA_14;
        }
    }

So, when Spring 2.5 was first released, the code didn’t assume it will be run in a Java version that’s later than 1.7. For Java 8 and beyond, the code above will assume default 1.4 version. Because of this, the annotations part will complain.

I think you either need to upgrade your Spring version or use Java 7. Spring 2.5 has been EOLed for quite some time now, anyway.

  • Stefan, That was it. I downloaded JRE 1.7 and all is well. Thank you for your help. Robert 
  • It appears like support for Java 8 was added in Spring >= 3.2.3. See the code change in JdkVersion.class.

From Dmitry

I have the similar problem. Old Spring MVC/Spring Faces application under spring 2.5.5 doesn’t run on Java 8.

I spent few days trying to find solution because we need to run Java 8.

First idea was: to upgrade complete Spring package up to 4.1.6. I used Maven. Problem of this method that after that it is necessary to rework nearly the whole project. It is because for example in Spring 4 were removed JSF implementation and some special taglibs where completely removed, like <sf:..>. And were some more major and minor problems with configuration, adapters, handlers, mappings….

Second approach was to replace Spring JARs partially.One by one. Again no success. Impossible to replace any jar without touching dependencies.

I believe that after few weeks or monthes of struggling I can get success on both approaches. But have no so much time. And I gave up. My solution is:

I found source file JdkVersion.java from org.springframework.core package. http://www.java2s.com/Code/Jar/o/Downloadorgspringframeworkcoresources300releasejar.htm. I created org.springframework.core package in my project with only one class JdkVersion. After that made simple change of the code to check Java 8 version. Smth like that:

public static final int JAVA_18 = 5;

    javaVersion = System.getProperty("java.version");
    // version String should look like "1.4.2_10"
    if (javaVersion.contains("1.8.")) {
        majorJavaVersion = JAVA_18;         
        System.out.println("JAVA_VERSION: "+javaVersion);

    } else if (javaVersion.contains("1.7.")) {
        majorJavaVersion = JAVA_17;
    }

Even this code change is not really necessary, only just for fun.It is because this source came from Spring 3.0.0 package where Spring guys already changed the Java version check. Versions higher than 7 are not considered as old java.

Now application starts properly. It call JdkVersion class from my project instead of jar.

Works so far! Thanks for all from this thread who gave this idea.

Thanks

From vallismortis

I need to support Spring 2.5.5 on Java 8, so I used the approach from this answer to provide a future-proof drop-in replacement for JdkVersion.class with as few side-effects as possible (nobody else posted a complete class, and I didn’t want to hijack the other answer). There is no need to check for Java 8, simply default to Java 7, which is the highest version the class cared about:

Extract the jar file:

mkdir spring
cd spring
jar xvf ../spring.jar

Check the Spring version in META-INF/MANIFEST.MF (you should see something like version=2.5.5). Look up the appropriate version of JdkVersion.java and use that as a starting point (the example below is for Spring 2.5.5 and you don’t want to change any method signatures from the version you’re working with).

Check the major and minor version of the JdkVersion.class file:

javap -verbose org/springframework/core/JdkVersion.class

We see that the class was original compiled as target 48.0 (looking this up, we find that is Java 1.4):

Classfile /tmp/spring/org/springframework/core/JdkVersion.class
  Last modified Jun 23, 2008; size 1286 bytes
  MD5 checksum 98e6ea08ce9186c50cc71d14465fc3dd
  Compiled from "JdkVersion.java"
public abstract class org.springframework.core.JdkVersion
  minor version: 0
  major version: 48
  flags: ACC_PUBLIC, ACC_SUPER, ACC_ABSTRACT
Constant pool:
...

Create org/springframework/core/JdkVersion.java with the following content:

package org.springframework.core;

public abstract class JdkVersion
{
    public static final int JAVA_13 = 0;
    public static final int JAVA_14 = 1;
    public static final int JAVA_15 = 2;
    public static final int JAVA_16 = 3;
    public static final int JAVA_17 = 4;

    private static final String javaVersion;
    private static final int majorJavaVersion;

    static
    {
        javaVersion = System.getProperty("java.version");

        if (javaVersion.indexOf("1.7.") != -1)
        {
            majorJavaVersion = JAVA_17;
        } else if (javaVersion.indexOf("1.6.") != -1) {
            majorJavaVersion = JAVA_16;
        } else if (javaVersion.indexOf("1.5.") != -1) {
            majorJavaVersion = JAVA_15;
        } else if (javaVersion.indexOf("1.4.") != -1) { // new
            majorJavaVersion = JAVA_14;                 // new
        } else {
            majorJavaVersion = JAVA_17;                 // changed from JAVA_14
        }
    }

    public static String getJavaVersion()
    {
        return javaVersion;
    }

    public static int getMajorJavaVersion()
    {
        return majorJavaVersion;
    }

    public static boolean isAtLeastJava14()
    {
        return true;
    }

    public static boolean isAtLeastJava15()
    {
        return getMajorJavaVersion() >= JAVA_15;
    }

    public static boolean isAtLeastJava16()
    {
        return getMajorJavaVersion() >= JAVA_16;
    }
}

Then compile the new class as Java 1.4:

javac -source 1.4 org/springframework/core/JdkVersion.java

You can check the major.minor version again as above if needed.

Create the modified jar file (without overwriting the original manifest):

jar Mcf ../spring-modified.jar *

Copy the modified jar file where needed (as spring.jar or as appropriate).

From MAFAIZ

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>

TO =>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

OR

  1. Right click on project property.
  2. Go to/ Click on “Java Build Path”.
  3. Select Libraries tab and double click on “JRE System Libry”
  4. On that pop up select the JDK 1.7 (If you dont have JDK 1.7 ,first install it, then select it from “Execution environment”)

From Juan David Grisales Garzon

i had the same problem, but i have a solution:

in your project file pom.xml replace:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6</version>
</dependency>

for:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

and say good bye to your problems!!

This two dependecies are the replacement for the firts dependency in the most part.

From arun

I happen to be another unfortunate user of a very old project (developed in 2008!) which is still using Spring 2.5. And it is not a Maven project either, so upgrading to later versions of Spring was taking a long time with dependencies failing and giving build errors. I downgraded tomcat JRE to 1.7 and it worked fine.

Just documenting how to do that in eclipse in case someone needs help:

1 – If you do not already have Java 7, download JDK 7 and install it somewhere like C:\Program Files\Java\jdk1.7.0_71

2 – Then go to eclipse and double click on Servers > Tomcat vX.Y server

3 – Click on Runtime Environment, then Installed JREs

4 – Then Add > Standard VM and click Next. You see a window like this:

5 – Click on Directory... and browse to the folder where you installed the JDK (i.e. C:\Program Files\Java\jdk1.7.0_71. JDK is fine, no need for JRE). Click Finish.

6 – Then make sure to select the JRE for Java 7 and you are all set.

From Amandeep Singh Bhatia

Migrate your spring version from 2.5 to >=3.2.3.

For Spring migration you need to do following changes –

1) In your pom.xml remove dependency for spring 2.5.6 and add dependency for spring new version.

	<properties>
		<spring.version>3.2.3.RELEASE</spring.version>
	</properties>
	<dependencies>
		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>

2) Update ‘xsi:schemaLocation’ in beans tag of applicationcontext.xml file of your project.

e.g update http://www.springframework.org/schema/beans/spring-beans-2.5.xsd to http://www.springframework.org/schema/beans/spring-beans-3.2.xsd for spring 3.2.3 version.

3) Clean,build and re-deploy your project.

Leave a Reply

Your email address will not be published. Required fields are marked *