Maven multi modules project [duplicate] - java

When I run my applicaiton from Eclipse it runs without any errors for servlet api 3.1.0 and 3.0.1.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
I use tomcat 8.0.21 for eclipse. I have set up tomcat8 on ubuntu machine which runs on tomcat 8.0.14 stable version.
Unfortunately, I get the following error message if I use servlet api 3.1.0. But it works for the older version 3.0.1.
root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [50] in the generated java file: [/var/lib/tomcat8/work/Catalina/localhost/ROOT/org/apache/jsp/WEB_002dINF/view/templates/login_002dtemplate_jsp.java]
The method getDispatcherType() is undefined for the type HttpServletRequest
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
Why do I get this error? How to fix this?

You're not supposed to provide Servlet API along with the web application archive if the target runtime already provides the API out the box. Tomcat as being a JSP/Servletcontainer already provides JSP, Servlet and EL APIs out the box. When you provide them along with your webapp anyway, then you may run into classloading conflicts caused by duplicate different versioned classes in the runtime classpath coming from both the webapp and the server.
Add <scope>provided</scope> to those dependencies already provided by the target runtime.
See also:
How do I import the javax.servlet API in my Eclipse project?

For Maven users there are several good answers here that you might want to check out.
I'm still in the dark ages, and am not using a dependency manager for my Tomcat project. If you're like me and have this issue, here's how I solved it: It seems tomcat provides the javax.servlet classes, so these don't need to be in your project's lib file. (I originally had the servlet-api-2.5.jar in my /WEB-INF/lib directory.) However you'll probably still need it to compile (I did), so you should move it to a location included in your java classpath. You may also need to tell your IDE where to look.
Hope that helps.

Method ServletRequest#getDispatcherType() was introduced to the Servlet Spec since version 3.0. The following error means that you are using an older version (e.g., 2.5) of javax.servlet-api in your application.
The method getDispatcherType() is undefined for the type HttpServletRequest
To solve this issue, you could follow the following two steps:
First of all, add <scope>provided</scope> to dependency javax.servlet-api
You should add <scope>provided</scope> to the dependency, because your Tomcat container will provide the dependency at runtime. And at the same time, ensure that you are using Tomcat 7 or higher, which supports Servlet Spec 3.0 or higher.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
Second of all, exclude any older version of javax.servlet-api
You need to ensure that any older version (e.g., 2.5) of javax.servlet-api is not included transitively. You can use mvn dependency:tree to find out. See below an example:
$ mvn dependency:tree
...
[INFO] +- com.google.oauth-client:google-oauth-client-servlet:jar:1.20.0:compile
[INFO] | +- com.google.oauth-client:google-oauth-client:jar:1.20.0:compile
[INFO] | +- com.google.http-client:google-http-client-jdo:jar:1.20.0:compile
[INFO] | +- javax.servlet:servlet-api:jar:2.5:compile
[INFO] | \- javax.jdo:jdo2-api:jar:2.3-eb:compile
[INFO] | \- javax.transaction:transaction-api:jar:1.1:compile
...
In this case, javax.servlet-api version 2.5 is included transitively by a dependency called google-oauth-client-servlet. We need to exclude it in pom.xml, like below:
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-servlet</artifactId>
<version>1.20.0</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
That's it.

Related

How to solve dependency problem in maven because of using different versions of same project

I have 4 modules in my project.
Module1 (i.e. com.assign.print:printlog.value:3.0.0-SNAPSHOT) has one class i.e. Foo.java, inside this class, on more class is there which is using com.print.assess: mns.pro:2.0
Module2 , Module2 and Module4 are using com.print.assess: mns.pro:6.2.
In my project main pom.xml, the dependency is added as :
<dependency>
<groupId>com.print.assess</groupId>
<artifactId>mns.pro</artifaxtId>
<version>6.2</version>
</dependency>
In Foo.java, I have one class as DataVal.java which is using older version.
If I don't add
<dependency>
<groupId>com.print.assess</groupId>
<artifactId>mns.pro</artifaxtId>
<version>2.0</version>
</dependency>
to Module1 pom.xml, Redline error is coming for DataVal.java saying "cannot resolve the symbol". So when I added the dependency with version 2.0, the error was resolved but while installing project:
Failed while enforcing releasability the error(s) are [
Dependency convergence error for com.print.assess:mns.pro:6.2 paths to
dependency are:
+-com.assign.print:printlog.value:3.0.0-SNAPSHOT
+-com.app.print:print.sal:1.1.3
+-com.print.assess:mns.pro:6.2
and
+-com.assign.print:printlog.value:3.0.0-SNAPSHOT
+-com.print.assess:mns.pro:2.0
and
+-com.assign.print:printlog.value:3.0.0-SNAPSHOT
+-com.print.assess.over:multi-task.rev:3.1
+-com.print.assess:mns.pro:6.2
How to resolve this issue?
Thanks in advance
If you have the dependencyConvergence enforcer rule active (which you obviously have), you need to determine your versions in the <dependencyManagement> (which is different from the standard <dependencies>).
Then you can declare the dependencies without version in <dependencies>. dependencyManagement entries can be in the main pom and in modules as well. #Bahmut gave you the link to understand dependencyManagement.
You may want to move your 6.2 dependency in your main pom to <dependencyManagement> so it does not get imported by default. Then you can simply import the 6.2 version in your module poms like this:
<dependency>
<groupId>com.print.assess</groupId>
<artifactId>mns.pro</artifaxtId>
</dependency>
and in the module where you need version 2, you can import it like this:
<dependency>
<groupId>com.print.assess</groupId>
<artifactId>mns.pro</artifaxtId>
<version>2.0</version>
</dependency>
More information about dependency management can be found here:
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

What version of Jersey should I use with Glassfish 3.1 and Java 7?

After I added JAX-RS 2.1/Jersey 2.26+ libraries to my dynamic web project I got the next error:
Glassfish error after upload WAR file 1
The server is running Glassfish 3.1 with JDK 1.7.0_80
I tried to use Jersey 1.19.1 ZIP bundle but then Glassfish said:
Glassfish error after upload WAR file 2
The WAR file with the latest Jersey runs perfect with Tomcat 9 and Java 8 but I need this project to run with Glassfish 3.1 and Java 7.
If you can provide a link to the correct version of Jersey I would appreciate!
This is my very fisrt question so excuse any errors.
To run Jersey with Glassfish 3 you really need to exclude all Jersey.2 things from you war and either:
use the bundled Jersey.1 implementation of your Glassfish 3.1.2.2 installation (check the /lib or /modules folders you shall see it, or online documentation)
EDIT: Jersey version should be 1.11.1
<!-- Keep 1.11.1 for Jersey which is Fish's version -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.11.1</version>
<!-- <scope>provided</scope> ... set Provided scope for GF3 deployment -->
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.11.1</version>
<!-- <scope>provided</scope> ... set Provided scope for GF3 deployment -->
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.11.1</version>
<!-- <scope>provided</scope> ... set Provided scope for GF3 deployment -->
</dependency>
<!-- Etc. - Organize your dependencies accordingly ... -->
replace the bundled Jersey.1 libraries of your Glassfish3 installation by another Jersey.1 implementation/libs(if you need a fresher version) - but never expect Jersey.2 to run with GF3.
Nonetheless you can actually write code that works with Jersey.1 and Jersey.2 but when you come to packaging and deployment you must build it with appropriate target JVM, dependencies and deployment descriptors - as soon as your modularization is "ok". I didn't read this post fully personnally but why not have a look here.
From Jersey latest (2.26) documentation
https://jersey.github.io/documentation/latest/modules-and-dependencies.html#d0e560
Since Jersey 2.26, all modules are build using Java SE 8 and there is no support for running it on older Java SE distributions.
So if you really want to use jersey 2.26 +, JDK should be 1.8+. No alternatives.

maven dependency + local jar in build path = slf4j conflict

I try to use the IMPINJ Octane SDK Java which comes as a jar including all needed dependencies together with Spark Framework in a maven project. To include the Spark Framework I use maven and the Octane SDK jar is added to the build path. My pom.xml has only the spark dependency:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5</version>
</dependency>
Every time I try to run the program I get to following error.
Exception in thread "Thread-1" java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
at org.eclipse.jetty.util.log.JettyAwareLogger.log(JettyAwareLogger.java:619)
at org.eclipse.jetty.util.log.JettyAwareLogger.info(JettyAwareLogger.java:314)
at org.eclipse.jetty.util.log.Slf4jLog.info(Slf4jLog.java:74)
at org.eclipse.jetty.util.log.Log.initialized(Log.java:186)
at org.eclipse.jetty.util.log.Log.getLogger(Log.java:298)
at org.eclipse.jetty.util.log.Log.getLogger(Log.java:288)
at org.eclipse.jetty.util.component.AbstractLifeCycle.<clinit>(AbstractLifeCycle.java:35)
at spark.embeddedserver.jetty.EmbeddedJettyFactory.create(EmbeddedJettyFactory.java:34)
at spark.embeddedserver.EmbeddedServers.create(EmbeddedServers.java:57)
at spark.Service.lambda$init$0(Service.java:342)
at java.lang.Thread.run(Thread.java:745)
The Octane SDK comes with slf4j and the Spark Framework also has slf4j as a dependency but they have different versions. I found the following thread NoSuchMethodError with SLF4J API but since I could remove slf4j from the jar I can't resolve the problem. How can I get this working?
I also tried to exclude slf4j in the pom but it did not work either:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
EDIT (SOLUTION):
I extracted the Octane SDK jar, removed slf4j and compressed it back to a jar.
My understanding is that sparks requires slf4j 1.7.13.
Then, you add Octane to the classpath (not through a maven dependency) and this Octane jar contains older classes of slf4j.
I just downloaded Octane to see for myself. I notice it include 2 versions:
OctaneSDKJava-1.22.0.30.jar
OctaneSDKJava-1.22.0.30-jar-with-dependencies.jar
You need to use OctaneSDKJava-1.22.0.30.jar and manually include all other dependencies but NOT the slf4j one (or the opposite, use OctaneSDKJava-1.22.0.30-jar-with-dependencies.jar and remove slf4j).
EDIT to answer a question in the comments:
I opened the latest OctaneSDKJava-1.26.2.0-jar-with-dependencies.zip, which contains a README.txt with the following details:
DEPENDENCIES
RUNTIME DEPENDENCIES
jdom 1.0 http://www.jdom.org
log4j 1.2.15 http://logging.apache.org/log4j/1.2/download.html
jargs 1.0 http://sourceforge.net/project/showfiles.php?group_id=33024
mina 1.1.7 http://mina.apache.org
slf4j-log4j12 1.5.0 http://www.slf4j.org
slf4j-api 1.5.0 http://www.slf4j.org
xerces-j 2.4.0 http://xerces.apache.org/
COMPILE DEPENDENCIES
velocity-dep 1.5 http://velocity.apache.org
jalopy 0.3.1 http://jalopy.sourceforge.net
jdom 1.0 http://www.jdom.org
log4j 1.2.15 http://logging.apache.org/log4j/1.2/download.html
commons-collection 3.2 http://commons.apache.org
commons-configuration 1.5 http://commons.apache.org
commons-lang 2.3 http://commons.apache.org
common-logging 1.1.1 http://commons.apache.org
JAXB RI dependencies including:
jaxb-xjc 2.0 https://jaxb.dev.java.net/
jaxb-impl 2.0 https://jaxb.dev.java.net/
jaxb-api 2.0 https://jaxb.dev.java.net/
activation 2.0 https://jaxb.dev.java.net/
jsr173_1.0_api 2.0 https://jaxb.dev.java.net/
The five above jaxb dependencies are available in a single jar "JAXB
RI" from https://jaxb.dev.java.net/. Execute this jar (doubleclick
windows, "java -jar " all other platforms) and copy the
individual jars to the LTKJava/lib directory) plus above runtime
dependencies
TEST DEPENDENCIES
JUnit 4.4 http://sourceforge.net/project/showfiles.php?group_id=15278
plus above dependencies note that the junit dependency needs to be
placed in the ANT_HOME/lib directory

java.lang.NoClassDefFoundError error when running my project

I have been struggling to get this to work and I think i can use some help. I am working on a Java project where the pom.xml has a bunch of dependencies some of which are themselves indirectly dependent on this jar:
com.sun.jersey:jersey-core:1.17.1 like this:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.0.1</version>
</dependency>
And I need this particular jar in my pom because I want to use the new features in jax-rs api:
javax.ws.rs:javax.ws.rs-api:2.0.The problem is when I build my project I get this error:
Found duplicate classes in [com.sun.jersey:jersey-core:1.17.1,javax.ws.rs:javax.ws.rs-api:2.0] :
[WARNING] javax.ws.rs.ApplicationPath
[WARNING] javax.ws.rs.Consumes
[WARNING] javax.ws.rs.CookieParam
[WARNING] javax.ws.rs.DELETE
[WARNING] javax.ws.rs.DefaultValue
[WARNING] javax.ws.rs.Encoded
[WARNING] javax.ws.rs.FormParam
[WARNING] javax.ws.rs.GET
[WARNING] javax.ws.rs.HEAD
[WARNING] javax.ws.rs.HeaderParam
...
..
I tried to fix this by excluding com.sun.jersey:jersey-core:1.17.1 from dependencies which were including it by checking the dependency tree.
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.0.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Now the project builds fine but when I try to run it I get this error:
java.lang.NoClassDefFoundError: com/sun/jersey/core/util/FeaturesAndProperties
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.7.0_51]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531) ~[na:1.7.0_51]
at java.lang.Class.getDeclaredMethods(Class.java:1855) ~[na:1.7.0_51]
at com.google.inject.internal.ProviderMethodsModule.getProviderMethods
(ProviderMethodsModule.java:81) ~[guice-3.0.jar:na]
at com.google.inject.internal.ProviderMethodsModule.configure
(ProviderMethodsModule.java:73) ~[guice-3.0.jar:na]
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
~[guice-3.0.jar:na]
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:232)
~[guice-3.0.jar:na]
at com.google.inject.spi.Elements.getElements(Elements.java:101) ~[guice-3.0.jar:na]
at com.google.inject.spi.Elements.getElements(Elements.java:92) ~[guice-3.0.jar:na]
It seems like jersey core was needed but how do I get around this problem?
You can't mix JAX-RS / Jersey versions
Jersey version 1 is the reference implementation for JAX-RS 1. Jersey version 1 uses the com.sun.jersey group / package prefix.
Jersey version 2 is the reference implementation for JAX-RS 2. Jersey version 2 uses the org.glassfish.jersey group / package prefix
If you have both Jersey versions, or both JAX-RS versions on your classpath, you'll get lots of NoClassDefFoundError, NoSuchMethodError or similar.
If possible use JAX-RS / Jersey version 2
For me this actually means I had somehow mixed things and was using
$ mvn dependency:list | grep jersey
[INFO] com.sun.jersey:jersey-client:jar:1.11:compile
[INFO] com.sun.jersey:jersey-core:jar:1.0.2:compile
(conflict within jersey 1.x)
Jersey is the reference implementation of JAX-RS, so you don't need the latest version of JAX-RS API (javax.ws.rs:javax.ws.rs-api:2.0) but the latest version of Jersey which is 2.8 (see https://jersey.java.net/).

Calling JSP files gives a NoSuchMethodError when app deployed outside of Eclipse

I have a maven project that works completely as intended inside of eclipse. It also builds and runs outside of eclipse, but when I try to call the frontend (JSP web pages) then I get the following:
Problem accessing /. Reason:
javax.servlet.ServletContext.getJspConfigDescriptor()Ljavax/servlet/descriptor/JspConfigDescriptor;
Caused by:
java.lang.NoSuchMethodError:
javax.servlet.ServletContext.getJspConfigDescriptor()Ljavax/servlet/descriptor/JspConfigDescriptor;
...
I've looked around and it seems that this message is associated with an incompatibility between Servlet 2.5 and Servlet 3.0. But I already have Servlet 3.0 as a dependency in my pom:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
So I can't figure out why this dependency that I need isn't being included when I build and deploy outside of eclipse even though the build itself is successful.
Any idea what could be causing this and how to fix it?
EDIT:
The JSP access isn't configured in web.xml. The index.jsp file is the welcome file set for the jetty server with this snippet:
// configure webapp
WebAppContext webroot = new WebAppContext();
webroot.setResourceBase("src/main/webapp");
webroot.setContextPath("/");
webroot.setWelcomeFiles(new String[] {"index.jsp"});
webroot.setParentLoaderPriority(true);
server.setHandler(webroot);
The remaining few jsp files are in the webapp folder.
EDIT 2:
I've examined the contents of the jar created when packaging my project and it appears that there are multiple copies of the javax/servlet/Servlet.class in the jar. This is a bit perplexing. I'm assuming that these other dependencies (listed below) that I have in my pom must be adding the additional Servlet.class files.
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
But I'm not sure how to fix any of this...
If anyone has any ideas, I'd love to hear them. The only real constraints that I have is that I have to use jetty 8.1.8.v20121106.
It seems to me you might be deploying a servlet 3.0 on tomcat 6 which is not supported and would cause this exception.
An additional possibility is that your exporting additional .classes within your war that are interfering with tomcat's default libraries.

Categories

Resources