10

I use very simple code that returns XML

RestTemplate restTemplate = new RestTemplate();

Source oResponse = restTemplate.postForObject(url, entity, Source.class,
            vars);

XPathOperations xpathTemplate = new Jaxp13XPathTemplate();
String address = xpathTemplate.evaluateAsString("//status", oResponse);

However, I get the following error

java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava/lang/String;)Ljava/lang/Class;
at org.springframework.xml.JaxpVersion.<clinit>(JaxpVersion.java:51)
at org.springframework.xml.transform.TraxUtils.isStaxSource(TraxUtils.java:70)
at org.springframework.xml.xpath.Jaxp13XPathTemplate.evaluate(Jaxp13XPathTemplate.java:131)
at org.springframework.xml.xpath.Jaxp13XPathTemplate.evaluateAsString(Jaxp13XPathTemplate.java:91)

Please help. Thanks, Vladimir

Vladimir
  • 630
  • 3
  • 12
  • 26
  • You may also face this while running/testing with a "fat JAR" in the classpath; in my case I had `activemq-all` which bundles its own versions of Spring (and other dependencies) without any repackaging (same `org.springframework.` parent) - and these built-in/bundled Spring classes were conflicting with the other "main" Spring JARs on the classpath, causing a `ClassUtils.isInnerClass()` method to go "missing" (newer Spring classes expecting it, but old classes from `activemq-all` missing it). – Janaka Bandara Sep 19 '21 at 04:48

3 Answers3

21

A NoSuchMethodError at runtime indicates that you are using a different version of a library than the version the code was built against.

In your case, Spring is the culprit. Check what is on the classpath at runtime and ensure the following:

  • the version is the same as the compile time jar
  • if there is more than one version, remove the one not required

Looking at http://docs.spring.io/spring/docs/3.2.0.M1/api/org/springframework/util/ClassUtils.html, it would appear that ClassUtils.forName(String) is deprecated as of Spring 3. My guess would be you have built your code against a version which had this method but are running it with a version where the method has been removed.

The ClassUtils class is contained within the spring-core jar so I would ensure the same version of this jar is used at both compile and run time.

JamesB
  • 7,774
  • 2
  • 22
  • 21
6

We had a similar issue when we are migrating to Spring 4.1.1

In our case, we had a dependency as follows

<dependency>
<groupId>org.springframework.javaconfig</groupId>
<artifactId>spring-javaconfig</artifactId>
<version>1.0.0.m3</version>
</dependency>

We added this dependency inorder to support Post Processing of a bean

<bean class="org.springframework.config.java.process.ConfigurationPostProcessor" />

There is a class from the above dependency "ProcessUtils.java", Which is invoking ClassUtils.forName with a Single argument (String). However this method has been deprecated. So the Exeception is being thrown.

I have learnt from another post in stackoverflow that we do not need a ConfigurationPostProcessor with Spring 4.1.1 (am not sure of exact version, when this support is enabled)

Once we removed the following from our Configuration (Spring Context file), the problem is resolved.

Community
  • 1
  • 1
3

You may have already figured this out but still in my case i was using a spring-orm whose version was different from spring-core and thus was getting similar error message when running unit tests. I have learnt from my experience that all the spring jar files included in a project should have the same version to avoid unnecessary issues.

Neeraj
  • 1,163
  • 2
  • 18
  • 32