java.lang.NoSuchMethodError: org.springframework.http.MediaType.getCharset()Ljava/nio/charset/Charset
Asked Answered
B

4

11

When I attempt to run my TestNG tests via ant I am getting java.lang.NoSuchMethodError: org.springframework.http.MediaType.getCharset()Ljava/nio/charset/Charset (see below for full exception)

I have no trouble compiling or running my web application but can't run tests via ant. I'm fairly certain this is a class loading issue but not sure what order class loading should be done at runtime. I'm using Spring 4.3.1 and Spring Security 4.1.1.

Is there a particular order to load my jars such that the "correct" version of org.springframework.http.MediaType is used?

[testng] org.testng.TestNGException: 
   [testng] An error occurred while instantiating class com.avada.rest.api.GroupsIntTest: org.springframework.http.MediaType.getCharset()Ljava/nio/charset/Charset;
   [testng]     at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:388)
   [testng]     at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:290)
   [testng]     at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125)
   [testng]     at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
   [testng]     at org.testng.TestClass.getInstances(TestClass.java:104)
   [testng]     at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:90)
   [testng]     at org.testng.TestClass.init(TestClass.java:82)
   [testng]     at org.testng.TestClass.<init>(TestClass.java:45)
   [testng]     at org.testng.TestRunner.initMethods(TestRunner.java:409)
   [testng]     at org.testng.TestRunner.init(TestRunner.java:247)
   [testng]     at org.testng.TestRunner.init(TestRunner.java:217)
   [testng]     at org.testng.TestRunner.<init>(TestRunner.java:161)
   [testng]     at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:555)
   [testng]     at org.testng.SuiteRunner.init(SuiteRunner.java:168)
   [testng]     at org.testng.SuiteRunner.<init>(SuiteRunner.java:117)
   [testng]     at org.testng.TestNG.createSuiteRunner(TestNG.java:1359)
   [testng]     at org.testng.TestNG.createSuiteRunners(TestNG.java:1346)
   [testng]     at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
   [testng]     at org.testng.TestNG.runSuites(TestNG.java:1124)
   [testng]     at org.testng.TestNG.run(TestNG.java:1096)
   [testng]     at org.testng.TestNG.privateMain(TestNG.java:1425)
   [testng]     at org.testng.TestNG.main(TestNG.java:1394)
   [testng] Caused by: java.lang.NoSuchMethodError: org.springframework.http.MediaType.getCharset()Ljava/nio/charset/Charset;
   [testng]     at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.getSupportedMediaTypes(RestTemplate.java:757)
   [testng]     at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:733)
   [testng]     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:617)
   [testng]     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
   [testng]     at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
   [testng]     at com.avada.rest.api.ApiTestClient.getAll(ApiTestClient.java:81)
   [testng]     at com.avada.rest.api.GroupsIntTest.<clinit>(GroupsIntTest.java:17)
   [testng]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   [testng]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
   [testng]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   [testng]     at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
   [testng]     at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
   [testng]     at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:377)
   [testng]     ... 21 more
   [testng] The tests failed.
Baltic answered 18/7, 2016 at 17:27 Comment(10)
It sounds like a Java version mismatchSpear
@Spear - my ${ant.java.version} and ${java.version} show up as 1.7 and 1.7.0_80. My $JAVA_HOME shows up as /usr/lib/jvm/java-7-oracle - what other places do I need to check for Java versions?Baltic
Have you tried clean and building your project?Sherrod
@NikuHysa - yes...everytime I run the tests I clean and buildBaltic
do you manage your dependencies "by hand" ? do you use v4.3 of spring-core.jar ?Northeast
@JérémieB - my dependencies (jars) are in "lib" folders. I'm using 4.3.1 of spring-coreBaltic
Please post the part of the Ant script that launches TestNG.Epistemology
@ZackMacomber Hi i am gettting same error for spring version 5.0.8. Could you please suggest something to fix it ?Hereunto
@BhagwatiMalav See my answer below, in particular the 2nd paragraphBaltic
@ZackMacomber Thanks for your response. getCharSet() got changed to getCharset() in spring-web 5.0.8. That's why I was getting this error.Hereunto
B
7

Unbelievable...foiled by class loading issues once again...an uber jar named activemq-minimal-5.13.3.jar had references to spring jars in it that was causing the conflict. activemq-minimal-5.13.3.jar comes before spring alphabetically so the classes in that activemq jar were being loaded/used over the spring jars. Updated my ant build to load the spring classes first and now I'm all set.

The way I found out is I put all of my 3rd party libs together in a "Test" project and added/deleted jars (with somewhat of an inclination where the problem was) until I was able to pinpoint where the class loading issue was.

Baltic answered 18/7, 2016 at 20:49 Comment(3)
Thank you for the update! Please "accept" your solution.Spear
"Unbelievable...foiled by class loading issues once again..." -> uff, it's not only me who struggle once and once again with class loading issues with Spring, so it's not so obvious thing ;)Monody
I have encountered this error also. To fix my project setup, in my Maven POM file, all I did was move the <dependency> tag for activemq-all below all of the <dependency> tags for all Spring frameworks (spring-web, spring-core, etc.) I believed this is the same thing as re-ordering the dependencies around so that Spring framework jars are loaded first before activemq-all.Casiano
C
4

org.springframework.http.MediaType.getCharset() was introduced since 4.3 if java did not find it that mean that you have a spring-core version below 4.3.1 in your classpath which get loaded and used.

Crippling answered 18/7, 2016 at 20:34 Comment(2)
It worked for me . I updated the spring core version > 4.3Jamie
@Jamie I am using Spring 5.0.8, and getting same error nosuchmethod for getChaset.Hereunto
Z
4

In spring latest version they have modified

org.springframework.http;

MediaType contentType = headers.getContentType();

Old :

      contentType.getCharSet()

New :

     contentType.getCharset()
Zig answered 21/5, 2019 at 17:27 Comment(1)
:-O Unbelievable! I was getting crazy because I didn't notice the case change from getCharSet() (in Spring 4.2) to getCharset() (in Spring 4.3+)!! And they even removed the old version, at least in Spring 5.2!Cripple
A
0

In my case, the culprit was spring-web-4.2.2.RELEASE, upgrading it to >4.2 fixed it for me

Atalaya answered 22/4, 2022 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.