Servlet referencing a Maven project - NoClassDefFoundError - java

I have a Maven project that uses Hibernate and PostgreSQL, and it's working fine on it's own. Now I want to use some classes from this project (essentially the database) in a servlet, but now things break.
I get a java.lang.NoClassDefFoundError: javax/persistence/Persistence error when accessing the servlet in Tomcat.
I'm thinking that somehow the Maven dependencies don't get "forwarded" to the servlet project, but I don't know what to do about it. I've added the Maven project to the servlet build path, and also to its "Web Deployment Assembly"... what more do I need to do?

Please try this dependency.
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final-redhat-3</version>
</dependency>

1.Check for all the jars, make sure that all the dependencies are added.
2.Clean your tomcat
Try adding Runtime dependencies.
3.Right click on your project and select Properties-> Deployment Assembly
click on Add
select Java Build Path Entries
select Maven Dependencies
Finish
Re-run your server.
Hope this helps!!

Related

What is the equivalent of adding a jar to deployment assembly in maven

I am working with a maven project in eclipse which I have been testing using the built-in "run on server" tomcat option. When I try to do this, I get errors stating certain dependencies are unmet from an external jar I reference in the POM. However I have found if I add the jar via the DeploymentAssembly Tab I can run in eclipse without issues:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.company.user.server.UserDetailsAuthoritiesMapper#0'
The issue comes when I try to deploy on an actual tomcat instance. The Maven build runs fine, but when I start the servlet I get the same unmet dependency errors. This to me is indicating that the external jar is not being properly packaged into the war. What is the maven equivalent of adding the package via the DeploymentAssembly tab in eclipse? The entry in the POM.xml:
<dependency>
<groupId>com.company.webapp</groupId>
<artifactId>webapp-user</artifactId>
<version>106</version>
</dependency>
Thanks
You need to locate the maven info for the external jar. If you google the name of the jar and maven you often find a direct link to the block you need. For example if I want version 1.58 of the Bouncy castle jar Google "Maven BouncyCastle" you can find the artifact info. Add that info to your pom.xml as a new dependency in your block.
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
</dependency>
After you rebuild, refresh maven in your IDE 1st after doing a clean, this will tell maven to pull down the jar and added it to your build.
You can also go to the repo directly and search:
Maven Repo: https://mvnrepository.com/

java.lang.NoClassDefFoundError: org/apache/spark/streaming/api/java/JavaStreamingContext [duplicate]

I am getting ClassNotFoundException and NoClassDefFoundError exceptions when I attempt to run my application using a maven defined dependency.
I added my maven dependency for the jar in question to my pom.xml file with the following declaration:
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
This added the relevant JAR file to my Maven Dependencies folder in Eclipse. I can access the classes in code but I get the mentioned exceptions once I run the application.
The jar is referenced in my Java build path under Maven dependencies:
My local maven repository is added to my classpath:
When I attempt to run the application, I get the following two exceptions:
java.lang.NoClassDefFoundError: Lnet/spy/memcached/MemcachedClient;
java.lang.ClassNotFoundException: net.spy.memcached.MemcachedClient
Can anyone point me in the right direction?
Change provided to compile
Provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
<scope>provided</scope>
"Provided" scope implies that the dependencies should be available only during compile phase and they will be available elsewhere during runtime and Maven shouldn't package them with the rest of the jars and classes of the current application.
Your dependency doesn't seem to be of "provided" scope. Remove that scope from your dependency definition and the jars will be present in your packaged jar/war/ear.
I was facing the same issue but i didn't have the tag defined on my POM, it was always working fine until one day all of a sudden started giving me that error in my local machine for no reason, the dependency was correctly set up in the POM and the jar was present in the local maven repository.
I tried cleaning the project and updating maven project but nothing, none of the other solutions suggested on other posts worked for me either.
I was finally able to solve it by going to the servers tab -> right click on Tomcat v8.0 -> browse deployment location
this should lead you to a temp folder like
.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps
then you browse to your project folder -> WEB-INF -> lib -> here i found out that the jar from the library that was giving the error was missing, so i just copied it from .m2\repository
Restarted the server and it started working as usual again.
I hope this helps some body facing the same issue.
Answer somewhat related to your problem, but still can help others.
Adding <scope>compile</scope> to the dependency was not enough in my case. I also had to add <packaging>jar</packaging> to the target module's pom.
I was also facing the same problem, after trying various solutions the issue was resolved by following steps...
click on server >open launch configurations >Arguments (copy
"LocalPath\temp0\wtpwebapps" path).
go to "LocalPath\temp0\wtpwebapps\ProjectName\WEB-INF\lib".
copy the JAR (which was causing ClassNotFoundException) from .m2 and place it in "LocalPath\temp0\wtpwebapps\ProjectName\WEB-INF\lib" path.

Maven copying Tomcat JARs into webapp WEB-INF/lib directory

I have a very simple webapp published as a WAR file, being developed in Eclipse. I'm using m2e for dependency management. My project, in the Project Facets tab, has the Dynamic Web Module enabled, along with Java. In addition, under my Deployment Assembly I have the Maven dependencies listed, but no inclusion/exclusion options:
My WAR builds fine, and runs fine in Eclipse. However, the Dynamic Web Module seems to be resulting in my WAR file containing a number of Tomcat JARs, in addition to all my other dependencies:
I was able to remove tomcat-catalina-7.0.30.jar (not pictured) by including in my POM.xml:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.30</version>
<scope>provided</scope>
</dependency>
But it seems there must be a way to tell Maven that the entire Tomcat container will be provided at runtime. I don't want to add ~20 provided entries to remove the rest of them.
Look at the POMs of your project. Somewhere, someone added them without the proper scope.
In Eclipse, you can use the POM editor. There is a set of tabs at the bottom of the editor. One reads "Dependency Hierarchy". Here you can search for "tomcat" and m2e will tell you which POMs contain such a dependency.

Intelij Spring project to eclipse Not working

I have created a spring web application in intellij idea 12. and now try to configure the same project in eclipse
for that i choose export to eclipse. and when i import that project into eclipse using import project from existing source
It says full of error for spring libray file missing. I have included all the spring mvc libraries from .m2/repository (Intellij)
but it says error in import javax.servlet.http.httpservletrequest cannot be resolved
Please anyone help me to run the idea spring project into eclipse using tomccat 7. Please provide any suggestions. I am using maven
Thanks in advance
HttpServletRequest is a part of Java servlet API. You can use different dependencies to resolve that. In your case, I think the best choice will be
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
It's avaliable from maven central repository. Because, as you said, you're using maven, all you need to do is to put dependency described above to your pom.xml
I have included all the spring mvc libraries from .m2/repository (Intellij)
I'm not sure you're resolving your dependencies via maven. ".m2/repository" has nothing to do with Intellij, it's your local maven repository.
Install the plugin m2e to manage maven project into eclipse m2e
Using "import project from existing source" will not manage the dependencies store into maven

Java ClassNotFoundException with maven dependency

I am getting ClassNotFoundException and NoClassDefFoundError exceptions when I attempt to run my application using a maven defined dependency.
I added my maven dependency for the jar in question to my pom.xml file with the following declaration:
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
This added the relevant JAR file to my Maven Dependencies folder in Eclipse. I can access the classes in code but I get the mentioned exceptions once I run the application.
The jar is referenced in my Java build path under Maven dependencies:
My local maven repository is added to my classpath:
When I attempt to run the application, I get the following two exceptions:
java.lang.NoClassDefFoundError: Lnet/spy/memcached/MemcachedClient;
java.lang.ClassNotFoundException: net.spy.memcached.MemcachedClient
Can anyone point me in the right direction?
Change provided to compile
Provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
<scope>provided</scope>
"Provided" scope implies that the dependencies should be available only during compile phase and they will be available elsewhere during runtime and Maven shouldn't package them with the rest of the jars and classes of the current application.
Your dependency doesn't seem to be of "provided" scope. Remove that scope from your dependency definition and the jars will be present in your packaged jar/war/ear.
I was facing the same issue but i didn't have the tag defined on my POM, it was always working fine until one day all of a sudden started giving me that error in my local machine for no reason, the dependency was correctly set up in the POM and the jar was present in the local maven repository.
I tried cleaning the project and updating maven project but nothing, none of the other solutions suggested on other posts worked for me either.
I was finally able to solve it by going to the servers tab -> right click on Tomcat v8.0 -> browse deployment location
this should lead you to a temp folder like
.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps
then you browse to your project folder -> WEB-INF -> lib -> here i found out that the jar from the library that was giving the error was missing, so i just copied it from .m2\repository
Restarted the server and it started working as usual again.
I hope this helps some body facing the same issue.
Answer somewhat related to your problem, but still can help others.
Adding <scope>compile</scope> to the dependency was not enough in my case. I also had to add <packaging>jar</packaging> to the target module's pom.
I was also facing the same problem, after trying various solutions the issue was resolved by following steps...
click on server >open launch configurations >Arguments (copy
"LocalPath\temp0\wtpwebapps" path).
go to "LocalPath\temp0\wtpwebapps\ProjectName\WEB-INF\lib".
copy the JAR (which was causing ClassNotFoundException) from .m2 and place it in "LocalPath\temp0\wtpwebapps\ProjectName\WEB-INF\lib" path.

Categories

Resources