java.lang.NoClassDefFoundError: javax/servlet/GenericFilter - spring-boot

I am using jsp as front end in spring boot app. I deployed application on localhost through tomcat8 manager and application runs perfectly. But when I follow same steps of deployment on hosting server I am getting below error
Caused by: java.lang.NoClassDefFoundError: javax/servlet/GenericFilter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at
Hence I included dependency in pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
and after updating project I have javax.servlet in my classpath and in war file also. Please guide me to resolve this issue.

Related

jdiameter-ha-* ClassNotFoundException

I'm attempting introduce high availability mode (via JBoss Cache) in my server implementation (essentially an expanded version of the example server) by configuring my Maven project to use jdiameter-ha-api and jdiameter-ha-impl dependencies instead of jdiameter-api and jdiameter-impl, in addition to adding the following extensions to jdiameter-config.xml:
<Extensions>
<SessionDatasource value="org.mobicents.diameter.impl.ha.data.ReplicatedSessionDatasource"/>
<TimerFacility value="org.mobicents.diameter.impl.ha.timer.ReplicatedTimerFacilityImpl"/>
</Extensions>
Now, when I run the server from Eclipse, it works fine, i.e. it start up in clustered mode (w/ JBoss Cache), however, when I attempt to run the jar produced by mvn install, it throws the following error:
2018-10-11 18:24:13,899 - (-)(-)(-)(-)(-) Starting Mobicents DIAMETER Stack v1.7.0-SNAPSHOT (-)(-)(-)(-)(-)
2018-10-11 18:24:13,959 - Failure creating stack 'Server'
org.jdiameter.api.InternalException: java.lang.reflect.InvocationTargetException
at org.jdiameter.client.impl.StackImpl.init(StackImpl.java:135)
at com.company.charging.diameter.ocf.utilities.StackCreator.<init>(StackCreator.java:37)
at com.company.charging.diameter.ocf.utilities.StackCreator.<init>(StackCreator.java:71)
at com.company.charging.diameter.ocf.server.Ocf.<init>(Ocf.java:187)
at com.company.charging.diameter.ocf.server.Ocf.main(Ocf.java:157)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at org.jdiameter.client.impl.StackImpl.init(StackImpl.java:129)
... 4 more
Caused by: java.lang.ClassNotFoundException: org.mobicents.diameter.impl.ha.timer.ReplicatedTimerFacilityImpl
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:291)
at org.jdiameter.client.impl.helpers.AssemblerImpl.fill(AssemblerImpl.java:139)
at org.jdiameter.client.impl.helpers.AssemblerImpl.<init>(AssemblerImpl.java:91)
... 9 more
Given that it starts up in Eclipse just fine, I'm assuming my POM file isn't managing dependencies properly, so that the final jar is missing these classes. Here's the relevant portion of my pom.xml:
<dependencies>
<dependency>
<groupId>org.mobicents.diameter</groupId>
<artifactId>jdiameter-ha-api</artifactId>
<version>${restcomm.diameter.jdiameter.version}</version>
</dependency>
<dependency>
<groupId>org.mobicents.diameter</groupId>
<artifactId>jdiameter-ha-impl</artifactId>
<version>${restcomm.diameter.jdiameter.version}</version>
</dependency>
<dependency>
<groupId>org.mobicents.diameter</groupId>
<artifactId>restcomm-diameter-mux-jar</artifactId>
<version>${restcomm.diameter.mux.version}</version>
</dependency>
</dependencies>

ClassNotFoundException: DirectoryService with apacheds-all already included

I am implemented LDAP authentication in my Spring Boot Application. The dependencies added are as follows:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-all</artifactId>
</dependency>
However I am still getting following ClassNotFoundException:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain'
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]
Caused by: java.lang.NoClassDefFoundError: org/apache/directory/server/core/DirectoryService
Caused by: java.lang.ClassNotFoundException: org.apache.directory.server.core.DirectoryService
My maven dependency tree clearly shows apacheds-all is added successfully.
Why am I still getting this error?
Spring Security doesn't support Apache DS 2.0. Instead, you need to use 1.5.5. Also, note that later 1.5.x versions are not supported due to breaking API changes.

ElasticSearch 2.0 Java API: java.lang.ClassNotFoundException: org.elasticsearch.common.settings.ImmutableSettings$Builder

I have updated my Spring Boot project to use the new ElasticSearch 2 API and I get this error at startup:
Caused by: java.lang.NoClassDefFoundError: org/elasticsearch/common/settings/ImmutableSettings$Builder
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
My relevant Maven dependencies are:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.3.0.M5</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.0.0-rc1</version>
</dependency>
Do you have any hint on what I am doing wrong?
Thanks
In ES 2.0, the ImmutableSettings class was indeed removed. This issue mentions it and the breaking changes documentation for 2.0 also mention it.
Instead you can now use Settings.builder() instead of ImmutableSettings.builder(). The current implementation of the Settings class can be seen here
All the questions that still use ImmutableSettings are questions about pre-2.0 versions of Elasticsearch.

Spring Boot java.lang.NoClassDefFoundError: javax/servlet/Filter

I Started a new project with Spring Boot 1.2.3. I'm getting error
java.lang.NoClassDefFoundError: javax/servlet/Filter
Gradle Dependencies:
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.codehaus.groovy:groovy")
compile("com.h2database:h2")
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile("org.springframework.boot:spring-boot-starter-test")
compile 'org.webjars:jquery:2.1.4'
compile 'org.webjars:bootstrap:3.3.4'
}
Here is the full stack trace
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/Filter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
at java.lang.Class.getMethod0(Class.java:2813)
at java.lang.Class.getMethod(Class.java:1663)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 29 more
Process finished with exit code 1
for the maven users,
comment the scope provided in the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>
UPDATE
As feed.me mentioned you have to uncomment the provided part depending on what kind of app you are deploying.
Here is a useful link with the details:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging
It's not good to change the scope of your application dependencies. Putting the dependency as compile, will provide the dependency also in your artifact that will be installed somewere.
The best think to do is configure the RUN configuration of your sping boot application by specifying as stated in documentation :
"Include dependencies with 'Provided' scope" "Enable this option to
add dependencies with the Provided scope to the runtime classpath."
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
This should be
compile("org.springframework.boot:spring-boot-starter-tomcat")
It's interesting things with IDE (IntelliJ in this case):
if you leave default, i.e. don't declare spring-boot-starter-tomcat as provided, a spring-boot-maven-plugin (SBMP) put tomcat's jars to your war -> and you'll probably get errors deploying this war to container (there could be a versions conflict)
else you'll get classpath with no compile dependency on tomcat-embed (SBMP will build executable war/jar with provided deps included anyway)
intelliJ honestly doesn't see provided deps at runtime (they are not in classpath) when you run its Spring Boot run configuration.
and with no tomcat-embed you can't run Spring-Boot with embedded servlet container.
There is some tricky workaround: put Tomcat's jars to classpath of your idea-module via UI: File->Project Structure->(Libraries or Modules/Dependencies tab) .
tomcat-embed-core
tomcat-embed-el
tomcat-embed-websocket
tomcat-embed-logging-juli
Better solution for maven case
Instead of adding module dependencies in Idea, it is better to declare maven profile with compile scope of spring-boot-starter-tomcat library.
<profiles>
<profile>
<id>embed-tomcat</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>
</profiles>
while spring-boot-starter-tomcat was declared provided in <dependencies/>, making this profile active in IDE or CLI (mvn -Pembed-tomcat ...) allow you to launch build with embedded tomcat.
Add the following dependency. The scope should be compile then it will work.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
In my case scope of that dependency was provided.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
So, I was getting the same issue.
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
I had to enable the below option in Intellij
Add dependencies with "provided" scope to classpath
You will see below option enabled,
OR
You can add the below property to .intellij/workspace.xml file under your application configuration section.
<option name="INCLUDE_PROVIDED_SCOPE" value="true" />
e.g.,
<component name="RunManager" selected="Application.DemoApp">
<configuration name="DemoApp" type="Application" factoryName="Application">
<option name="INCLUDE_PROVIDED_SCOPE" value="true" /> ```
For Jar
Add pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
That looks like you tried to add the libraries servlet.jar or servlet-api.jar into your project /lib/ folder, but Tomcat already should provide you with those libraries. Remove them from your project and classpath. Search for that anywhere in your project or classpath and remove it.
The configuration here is working for me:
configurations {
customProvidedRuntime
}
dependencies {
compile(
// Spring Boot dependencies
)
customProvidedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
}
war {
classpath = files(configurations.runtime.minus(configurations.customProvidedRuntime))
}
springBoot {
providedConfiguration = "customProvidedRuntime"
}
2023 Solution
Remove provided from maven of tomcat artifactId
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- Here <scope>provided</scope >-->
</dependency>

java.lang.NoClassDefFoundError: org/springframework/web/context/ContextCleanupListener issue

I get this error
SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org/springframework/web/context/ContextCleanupListener
at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:80)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5035)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5687)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1028)
at org.apache.catalina.startup.HostConfig.undeploy(HostConfig.java:1498)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1425)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1646)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:328)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1374)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1546)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1556)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1524)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.springframework.web.context.ContextCleanupListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
... 16 more
I look in my deployed folder however and I see the jar that contains that file, what am I doing wrong?
https://github.com/davidahines/spacechip/tree/spring_security
The issue is that when I try to go to localhost:8080/spacechip I get "The resource is unavailable."
There is my configuration.
try change dependency of spring-web to 3.0.5.RELEASE in your pom, you are currently have 2 version on classpath
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
The jar may be in your deployed folder, but is the jar (or your deployed folder with a wildcard) in the CLASSPATH?
EnvironmentAware is located in the spring-context-3.1.1.RELEASE.jar, so you are missing that one.
Also recheck your Maven POM file so that you are not missing any other Spring library, like spring-web, spring-webmvc (you may have these since the DispatcherServlet class if found), spring-orm if you use an ORM like Hibernate, spring-jms if you use JMS, etc.

Resources