0

I am using all latest jar and Java 8 and Java EE 6 version, getting java.lang.NoSuchMethodError.

Tried with jersey-client-2.27.jar, 2.26, 2.28 version, still having the same problem, even checked that the method is available in the corresponding jar.

    Client client = ClientBuilder.newClient();
    WebTarget sendWT = client.target(blueMailUrl);

    Invocation.Builder builder = sendWT
        .request(MediaType.APPLICATION_JSON)
        .header(
            "Authorization", 
            "Basic " + DatatypeConverter.printBase64Binary((byte[]) (blueMailUserid + ":" + blueMailPassword).getBytes("UTF-8"))
        );

It should not throw this error.

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
  • Please post a whole exception you get. – Dmitriy Popov Jul 03 '19 at 15:06
  • Also see https://stackoverflow.com/questions/18574145/nosuchmethoderror-multivaluedmap-addall-in-jersey-client, seems to be related. – Dmitriy Popov Jul 03 '19 at 15:11
  • @DmitriyPopov ,please check the error."java.lang.NoSuchMethodError: javax/ws/rs/core/MultivaluedMap.addAll(Ljava/lang/Object;[Ljava/lang/Object;)V (loaded from file:/C:/WAS_8.5/dev/JavaEE/6.0/j2ee.jar by sun.misc.Launcher$AppClassLoader@f0c30cb) called from class org.glassfish.jersey.client.ClientRequest (loaded from file:/C:/OSCWI/BluemailService/OSCWI_eEnablement/WebContent/WEB-INF/lib/jersey-client-2.27.jar by sun.misc.Launcher$AppClassLoader@f0c30cb). at org.glassfish.jersey.client.ClientRequest.accept(ClientRequest.java:335) " – Hemalata Mohanta Jul 04 '19 at 05:07

1 Answers1

2

Most likely some other jar that is "in front" of the jersey-jar in the classpath contains an older version of MultivaluedMap.

Add the following line to your code (before where the error occurs)

System.out.println(getClass().getClassLoader().getResource("javax/ws/rs/core/MultivaluedMap.class"));

This will output the location of the class where it's found, something like

jar:file:/D:/myide/workspace/MyProject/lib/jars/someotherlibrary.jar!/javax/ws/rs/core/MultivaluedMap.class

To solve this depends on the actual result of above call and the whole thing is called Jar-Hell. It works in your IDE because the classpath being set there seems to differ from the classpath that is set when running the application.

Edit (as the location is now known): The j2ee.jar for EE6 contains a MultvaluedMap without the addAll method and because j2ee.jar seems to be in the classpath before the jersey-jar you have this problem. As discussed in NoSuchMethodError: MultivaluedMap.addAll in Jersey Client the solution is to use Jersey 1.8 or use J2EE 7. Another solution not discussed there is to not use the j2ee.jar altogether but only the jars of the sub project you're actually using, e.g. activation.jar and javamail.jar if you're only using it to send mails, etc.

Lothar
  • 5,323
  • 1
  • 11
  • 27
  • I tried as you told ,its coming like this – Hemalata Mohanta Jul 04 '19 at 05:50
  • jar:file:/C:/WAS_8.5/dev/JavaEE/6.0/j2ee.jar!/javax/ws/rs/core/MultivaluedMap.class – Hemalata Mohanta Jul 04 '19 at 05:50
  • @HemalataMohanta So you have the same problem as being discussed in SO-question Dimitry linked to: https://stackoverflow.com/questions/18574145/nosuchmethoderror-multivaluedmap-addall-in-jersey-client – Lothar Jul 04 '19 at 08:43
  • If I am using jersy -client 1.8 version as discussed we are using java ee6 ,here i am getting ClientBuilder class not found in that version.but with latest version it is there.So could any one please tell that which exactly jar i need to use for this. – Hemalata Mohanta Jul 04 '19 at 11:03
  • @HemalataMohanta If you downgrade to Jersey 1.8 you have to implement your code against its API. ClientBuilder is part of Jersey 2, so you have to do the changes accordingly. Without knowing anything about your actual project, I doubt that somebody will be able to tell you what jars to use, etc. – Lothar Jul 04 '19 at 19:54
  • ,thank you so much for your suggestion ,I am using now jersy 1.8 jar and related class ,but here i am getting one issue related to type of response .Please look once the error "com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, application/json, was not found". – Hemalata Mohanta Jul 04 '19 at 22:30
  • And my code is I am adding below ,please check once. Client client =Client.create(); WebResource webTarget = client.resource(blueMailUrl); Builder builder = webTarget.getRequestBuilder().header("Authorization", "Basic" + DatatypeConverter.parseBase64Binary(blueMailUserid + ":" + blueMailPassword));builder.type(MediaType.APPLICATION_JSON); Map, ?> response =builder.post(HashMap.class, email); – Hemalata Mohanta Jul 04 '19 at 22:30
  • And response is returning in our method.But in last line (response ) it is throwing above error .Could you please check and guide me. – Hemalata Mohanta Jul 04 '19 at 22:34
  • @HemalataMohanta I can't guide you here because I haven't done that much with Jersey. I answered your `NoSuchMethodError`-question which I seem to have helped you to solve. What you're asking now is essentially a new question where you might create a new one on StackOverflow (after of course using the search function to check if others already have asked them ;-) – Lothar Jul 05 '19 at 15:05