java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava

一曲冷凌霜 提交于 2019-12-28 06:35: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


回答1:


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.




回答2:


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.




回答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.



来源:https://stackoverflow.com/questions/22465906/java-lang-nosuchmethoderror-org-springframework-util-classutils-fornameljava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!