sendgrid exception java.lang.NoSuchMethodError - java

I am getting exception when I try to use SendGrid sg = new SendGrid(sendGridAPIKEY);
It gives exception because send grid uses HttpClientBuilder.create().build(); internally.
I resolved this issue by adding following lines
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
Client sendGridClient = new Client(defaultHttpClient, false);
SendGrid sg = new SendGrid(sendGridAPIKEY, sendGridClient);
where DefaultHttpClient is deprecated.
But I want to know why HttpClientBuilder.create().build() line gives error.
I am using JDK 1.8 and following POM dependency
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<!-- send Grid dependency -->
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.0.1</version>
</dependency>
Exception:
Nov 06, 2017 10:15:26 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V] with root cause
java.lang.NoSuchMethodError: org.apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:176)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:158)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:973)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
at com.sendgrid.Client.<init>(Client.java:76)
at com.sendgrid.Client.<init>(Client.java:56)
at com.sendgrid.SendGrid.<init>(SendGrid.java:24)

Related

How do I resolve java.lang.NoClassDefFoundError: during Azure Blob Storage initialization

I am getting this java.lang.NoClassDefFoundError: Could not initialize class com.azure.sto rage.blob.implementation.util.ModelHelper when trying to initilalize Azure Blob like this
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(ACCOUNT_NAME, ACCOUNT_KEY);
BlobServiceClient storageClient = new BlobServiceClientBuilder().endpoint(endpoint).credential(credential).buildClient();
How to solve this?
2022-10-28 12:03:30,802 ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServl
et] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.azure.sto
rage.blob.implementation.util.ModelHelper] with root cause
java.lang.NoClassDefFoundError: Could not initialize class com.azure.storage.blob.implementation.util.ModelHelper
at com.azure.storage.blob.BlobUrlParts.parse(BlobUrlParts.java:371) ~[azure-storage-blob-12.20.0.jar!/:12.20.0]
at com.azure.storage.blob.BlobServiceClientBuilder.endpoint(BlobServiceClientBuilder.java:171) ~[azure-storage-blob-12.20.0.jar!/:12.20.0]
My pom.xml has these dependencies
<!-- https://mvnrepository.com/artifact/com.azure/azure-storage-blob -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.20.0</version>
</dependency>
<!-- Needed to avoid java.lang.ClassNotFoundException-->
<!-- https://mvnrepository.com/artifact/com.azure/azure-core -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.33.0</version>
</dependency>
I also see a similar failure but with a different error message sometimes
java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.cfg.MapperBuilder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_111]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_111]
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) ~[data-pipeline-automation-1.0.jar:0.0.1-SNAPSHOT]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_111]
at com.azure.core.implementation.jackson.ObjectMapperShim.createHeaderMapper(ObjectMapperShim.java:135) ~[azure-core-1.33.0.jar!/:1.33.0]
at com.azure.core.util.serializer.JacksonAdapter.<init>(JacksonAdapter.java:80) ~[azure-core-1.33.0.jar!/:1.33.0]
at com.azure.core.util.serializer.JacksonAdapter.<init>(JacksonAdapter.java:58) ~[azure-core-1.33.0.jar!/:1.33.0]
at com.azure.core.util.serializer.JacksonAdapter$SerializerAdapterHolder.<clinit>(JacksonAdapter.java:112) ~[azure-core-1.33.0.jar!/:1.33.0]
at com.azure.core.util.serializer.JacksonAdapter.createDefaultSerializerAdapter(JacksonAdapter.java:121) ~[azure-core-1.33.0.jar!/:1.33.0]
at com.azure.storage.blob.implementation.util.ModelHelper.<clinit>(ModelHelper.java:61) ~[azure-storage-blob-12.20.0.jar!/:12.20.0]
at com.azure.storage.blob.BlobUrlParts.parse(BlobUrlParts.java:371) ~[azure-storage-blob-12.20.0.jar!/:12.20.0]
at com.azure.storage.blob.BlobServiceClientBuilder.endpoint(BlobServiceClientBuilder.java:171) ~[azure-storage-blob-12.20.0.jar!/:12.20.0]
Here, was able to use blobserviceclient to create a container in storage using the following code.
DefaultAzureCredential k = new DefaultAzureCredentialBuilder().build();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("https://<storageaccount>.blob.core.windows.net/")
.credential(k)
.buildClient();
dependencies:
<!-- https://mvnrepository.com/artifact/com.azure/azure-storage-blob -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.20.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure/azure-core -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.33.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure/azure-identity -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.7.0-beta.2</version>
<scope>compile</scope>
</dependency>
Now let's come to the NoClassDefFoundError error, well you get this error when the JVM is calling a class and not being able to find it at run time.
Maybe the ModelHelper and the MapperBuilder class/dependency are not at the same location as everyone else. What you can do is uninstall everything and then reinstall everything. Then recompile the project.
Refer this article on NoClassDefFoundError by Rollbar

servlet.service error, classNotFoundException

I am done building the java spring boot app and now I am trying to deploy it to VPS. Everything works except once I access part of web app that is connected to the database I get this error
ERROR 7379 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
When I put connection url to database to be jdbc:mysql://"ip of server":3306/kuponi i get this error
and when I put connection url to database to be jdbc:mysql://localhost:3306/kuponi
Try adding these dependencies to your pom.xml file
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-ri</artifactId>
<version>2.3.3</version>
</dependency>

NoClassDefFoundError: org/directwebremoting/extend/ConvertUtil

After moving a Tomcat app from one server to the other, we get this error:
SEVERE: Servlet.service() for servlet dwr-invoker threw exception
java.lang.NoClassDefFoundError: org/directwebremoting/extend/ConvertUtil
at org.directwebremoting.dwrp.CallBatch.parseParameters(CallBatch.java:135)
at org.directwebremoting.dwrp.CallBatch.<init>(CallBatch.java:47)
at org.directwebremoting.dwrp.BaseCallHandler.handle(BaseCallHandler.java:72)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:120)
at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:141)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
We have the dwr.jar in the WEB-INF/lib dir (and I opened it and checked, the org/directwebremoting/extend/ConvertUtil is there)
/srv/tomcat6/webapps/APPNAME/WEB-INF/lib/dwr.jar
UPDATE this is how DWR dependency is defined in the pom.xml
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>3.rc1</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/dwr.jar</systemPath>
</dependency>

Issues with Apache HTTP Client and logging

In my Tapestry application, I need to do a GET request to a certain URL at some point. I do it like this (using Apache's HttpClient 4.2.1)
String scheme = getScheme();
String host = getHost();
int port = getPort();
String path = getPath();
String paramKey = getParamKey();
String paramValue = getParamValue();
URIBuilder builder = new URIBuilder();
builder
.setScheme(scheme)
.setHost(host)
.setPort(port)
.setPath(path)
.setParameter(paramKey, paramValue);
URI uri = builder.build();
HttpGet getRequest = new HttpGet(uri);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(getRequest);
When I deploy my WAR on Glassfish (3.1.2.2) and the code in question is executed, the following exception is thrown:
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;#78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category) (Caused by org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;#78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category))
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
I specify this in my pom.xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
And I checked and made sure the Class that is "not being found" is actually in WEB-INF/lib/log4j-1.2.14.
Is there any way to solve this issue?
You have a classloader problem. Maybe another web application, that uses different log4j version, is loaded first. The classloader uses the first version of the class it finds.
Try this:
Remove all other applications from app server
Check that you don't have another log4j in classpath of the server.
Try to install and start your application.

Jersey with Jetty hard time

I can't run up my RESTful service with embedded Jetty 7 and Jersey. When I call my simple hello test I get:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message
body writer for Java class java.lang.String, and Java type class java.lang.String,
and MIME media type text/plain was not found
I saw errors similar posted here on SO, but it was for custom classes, and mine is for just String. Any hints? My init server code:
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
ServletContainer container = new ServletContainer();
ServletHolder h = new ServletHolder(container);
h.setInitParameter("com.sun.jersey.config.property.packages", "api");
context.addServlet(h, "/res/*");
And dependances:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>7.1.6.v20100715</version>
</dependency>
Do you have any hints?
Exchange jersey-server to :
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.11.1</version>
</dependency>

Categories

Resources