Can someone help me on how to print the version of a library to the console? For example, my dependency in pom.xml is as follows:
<dependency>
<groupId>com.abc.client</groupId>
<artifactId>AbcClient</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>xyz</classifier>
</dependency>
And the exact library that is downloaded is "AbcClient-1.0-20171107.000833-111.jar". I want the full name of the library with its version to be printed to the console.
Example console output:
AbcClient-1.0-20171107.000833-111.jar
If you only want the dependency jar and its version then did you try Apache Maven Dependency Plugin?
https://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html
Sample Usage on one of my project
mvn dependency:tree | grep jackson-jaxrs-json-provider
[INFO] +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.8.8:provided
[INFO] | | +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.8.8:compile
Related
In my project there is a Java Class which does:
import javax.activation.DataHandler;
My POM only has this dependency:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
Build (mvn clean install) is working, because:
mvn dependency:tree
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli)
[INFO] \- javax.mail:mail:jar:1.5.0-b01:compile
[INFO] \- javax.activation:activation:jar:1.1:compile
But why does
https://mvnrepository.com/artifact/javax.mail/mail/1.5.0-b01
and also
https://repo1.maven.org/maven2/javax/mail/mail/1.5.0-b01/mail-1.5.0-b01.pom
say there are no dependencies ???
Another question:
Why does javax.mail:mail:jar:1.5.0-b01 (from 2013) not depend on the latest javax.activation:activation:jar:1.1.1 (from 2009)?
Because mvnrepository has it wrong, "javax.mail:mail:jar:1.5.0-b01" version does have dependency to "javax.activation:activation:jar:1.1", it's in it's parent:
https://repo1.maven.org/maven2/com/sun/mail/all/1.5.0-b01/all-1.5.0-b01.pom
Don't depend on mvnrepository very much, the truth is always in pom files. Btw. javalibs shows you the dependency:
https://javalibs.com/artifact/javax.mail/javax.mail-api
Why the newest javax.mail version doesn't contain dependency to activation-api is probably due to transition to jakarta artifacts ... old javax dependencies cannot depend on new jakarta dependencies because Oracle said so ... :-( Newest javax.mail and activation artifacts are these:
https://javalibs.com/artifact/javax.mail/javax.mail-api
https://javalibs.com/artifact/jakarta.mail/jakarta.mail-api
I am running a Maven project which is also a dynamic web project. I have used all Spring libraries in Maven. I created web.xml, but when I start my Tomcat 7 server I am getting the following message:
INFO: validateJarFile(C:\Users\mibvzd0\workspace\.metadata\.plugins\
org.eclipse.wst.server.core\tmp2\wtpwebapps\hapi_hl7\WEB-INF\lib\
servlet-api-2.4.jar) - jar not loaded.
See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
I tried deleting the servlet from webapp/lib, but it didn't work. Let me know what should be done in my case.
The servlet API .jar file must not be embedded inside the webapp since, obviously, the container already has these classes in its classpath: it implements the interfaces contained in this jar.
The dependency should be in the provided scope, rather than the default compile scope, in your Maven pom:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
You get this warning message when the servlet api jar file has already been loaded in the container and you try to load it once again from lib directory.
The Servlet specs say you are not allowed to have servlet.jar in
your webapps lib directory.
Get rid of the warning message by simply removing servlet.jar from your lib directory.
If you don't find the jar in the lib directory scan for your build path and remove the jar.
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\project\WEB-INF\lib
If you are running a maven project, change the javax.servlet-api dependency to scope provided in you pom.xml since the container already provided the servlet jar in itself.
To fix it, set the scope to provided. This tells Maven use code servlet-api.jar for compiling and testing only, but NOT include it in the WAR file. The deployed container will “provide” the servlet-api.jar at runtime.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
You may find the following windows command line useful in tracking down the offending jar file. it creates an index of all the class files in all the jars in the folder. Execute from within the lib folder of your deployed app, then search the index.txt file for the offending class.
for /r %X in (*.jar) do (echo %X & jar -tf %X) >> index.txt
Maven Dependency Scope
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.
I've been struggling with this issue and I've tried numerous "solutions".
However, in the end, the only one that worked and it actually took a few seconds to do it was to: delete and add back new server instance!
Basically, I right clicked on my Tomcat server in Eclipse under Servers and deleted it. Next, I've added a new Tomcat server. Cleaned and redeployed the application and I got rid of this error.
Check Inside the Following Directory for the jar file el-api.jar :C:\apache-tomcat-7.0.39\lib\el-api.jar if it exists then in this directory of your web application WEB-INF\lib\el-api.jar the jar should be removed
when your URL pattern is wrong, this error may be occurred.
eg. If you wrote #WebServlet("login"), this error will be shown. The correct one is #WebServlet("/login").
Exclusions and provided dependencies will not work in child projects.
If you are using inheritance in Maven projects you must include this configuration on the parent pom.xml file. You will have a <parent>...</parent> section in your pom.xml if you are using inheritance. So you will have something like this in your parent pom.xml:
<groupId>some.groupId</groupId>
<version>1.0</version>
<artifactId>someArtifactId</artifactId>
<packaging>pom</packaging>
<modules>
<module>child-module-1</module>
<module>child-module-2</module>
</modules>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
The JAX-WS dependency library “jaxws-rt.jar” is missing.
Go here http://jax-ws.java.net/.
Download JAX-WS RI distribution.
Unzip it and copy “jaxws-rt.jar” to Tomcat library folder “{$TOMCAT}/lib“.
Restart Tomcat.
Typically when you see this message, it is benign.
If it says
INFO: validateJarFile(/<webapp>/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded.
See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
It means it is ignoring your servlet-api-2.5.jar because tomcat already has a built-in version of that jar, so it isn't going to be using yours. Typically this doesn't cause an issue.
If however it says WEB-INF/lib/my_jar.jar - jar not loaded...Offending class: javax/servlet/Servlet.class
then what you can do (in my instance, it's a shaded jar) is run
$ mvn dependency:tree
and discover that you have a transitive dependency on "something" that depends on a jar that is either servlet-api or something like it (ex: tomcat-servlet-api-9.0.0). So add an exclusion to that to your pom, ex: (in my case, tomcat, in your case, probably the ones mentioned in the other answers):
<dependency>
...
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet</artifactId>
</exclusion>
</exclusions>
</dependency>
from https://stackoverflow.com/a/9941668/778517
I did mvn dependency:tree and result was
(...)
[INFO] +- org.zkoss.zk:zkspring-core:jar:3.2.0:compile
[INFO] | +- org.springframework:spring-beans:jar:3.0.7.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:3.0.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-asm:jar:3.0.7.RELEASE:compile
[INFO] | +- org.springframework:spring-web:jar:3.0.7.RELEASE:compile
[INFO] | \- org.reflections:reflections:jar:0.9.5-RC2:compile
[INFO] | +- com.google.collections:google-collections:jar:1.0:compile
[INFO] | +- ch.qos.logback:logback-classic:jar:0.9.9:runtime
[INFO] | | \- ch.qos.logback:logback-core:jar:0.9.9:runtime
[INFO] | +- com.google.code.gson:gson:jar:1.4:compile
[INFO] | \- javax.servlet:servlet-api:jar:2.5:compile
(...)
so in pom.xml, zkspring-core I added the exclusions tag
(...)
<groupId>org.zkoss.zk</groupId>
<artifactId>zkspring-core</artifactId>
<version>3.2.0</version>
+ <exclusions>
+ <exclusion>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
(...)
after I ran mvn dependency:tree and javax.servlet:servlet-api:jar:2.5:compile is clean
(...)
[INFO] +- org.zkoss.zk:zkspring-core:jar:3.2.0:compile
[INFO] | +- org.springframework:spring-beans:jar:3.0.7.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:3.0.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-asm:jar:3.0.7.RELEASE:compile
[INFO] | +- org.springframework:spring-web:jar:3.0.7.RELEASE:compile
[INFO] | \- org.reflections:reflections:jar:0.9.5-RC2:compile
[INFO] | +- com.google.collections:google-collections:jar:1.0:compile
[INFO] | +- ch.qos.logback:logback-classic:jar:0.9.9:runtime
[INFO] | | \- ch.qos.logback:logback-core:jar:0.9.9:runtime
[INFO] | \- com.google.code.gson:gson:jar:1.4:compile
(...)
Remove servlet.jar from source web-inf/lib folder as it is available in tomcat lib folder then it works fine
This has been very annoying. I have 2 projects, project A and B, B with dependency on A as a JAR file. So a 3rd library X in A has a dependency on another 3rd party library Y, the problem is maven resolves Y in project A to a version, but in project B to another version, like the following:
commons-beanutils:jar:1.9.2 vs. commons-beanutils:jar:1.8.0
And the version below is specified in project A:
net.sf.json-lib:json-lib:jar:jdk15:2.4
In the POM.xml of project B, there isn't really any explicitly specified version of dependencies.
In project A:
[INFO] +- net.sf.json-lib:json-lib:jar:jdk15:2.4:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO] | +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] | \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
In project B that only introduces the dependency on Y because of A:
[INFO] +- A.jar
[INFO] | +- net.sf.json-lib:json-lib:jar:jdk15:2.4:compile
[INFO] | | +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] | | +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] | | \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
The ideal solution is to only make changes in pom.xml in project B to solve the duplicate versions dependency hell. Any idea? Thanks!
Is it possible to make project B inherit dependencies from A.jar instead of introducing those of its own.
EDIT
In the end I found something that worked, so I'd just put it here in case someone may be facing the same problem later.
The key is to put the dependencies come with project A into exclusions, so maven won't just use the versions of libraries defined in project A, but work out a version based on the current context. Below is the example:
<dependency>
<groupId>com.wonderland</groupId>
<artifactId>project-a</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</exclusion>
<!-- ... everything else to be excluded -->
<exclusions>
</dependency>
Let me put the main points into an answer.
B is a war and depends on the jar A. So it inherits all the transitive dependencies of A.
net.sf.json-lib:json-lib:jar:jdk15:2.4:compile has actually a dependency on commons-beanutils:commons-beanutils:jar:1.8.0:compile (I looked that up). So your project B correctly resolves this dependency (and puts it into the war).
The tree of project A shows a dependency of commons-beanutils on version 1.9.2. This version number must have come from some other place inside project A. It may be a dependencyManagement, it may be some other dependency. Track down where the version 1.9.2 comes from and you know more.
In any case, the war will only contain the version 1.8.0 and not 1.9.2 as you can never have two artifacts with same groupId/artifactId in one war.
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.
I'm developing a GWT application. It's using RPC to collect information from an internal system. It does so by using a library jar, lets call it alpha.jar. We are using this jar in many application so it works fine, and btw its built with ANT, outside eclipse.
Some classes in alpha.jar references LOG4J2 and also lots of other external jars, so when we run an application we pass a classpath to all those, and everything works out fine. Please note that this is not a simple beginners problem. The alpha.jar is working as it should, including calls to Log4J.
The problem:
In Eclipse, I have this GWT application project and also the Alpha.jar project (with source code of course). The server part needs to instatiate alpha objects and communicate to the alpha system.
When do this in GWT by adding a build-path-reference to the Alpha project, my GWT app runs fine.
When I instead of the project reference include (in war/WEB-INF/lib) the alpha.jar and runs the app, I get the error in the title the first time I instantiate a class from alpha.jar.
There are no peculiarities in how the alpha.jar is built, so basically it should be the same thing as the project in eclipse, right?
Note the following:
*) The alpha.jar's dependent jars are also in war/WEB-INF/lib. log4j2-core, log4j-api as well as a bunch of others (apache common for example)
*) If I remove the alpha.jar (and the code that calls it) and instead just add code that called LOG4J2, that code also works fine!
How come I get this weird error when using the JAR?? Note also the NoClassDefFoundError, its not the more common ClassNotFoundException. Pls see What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?
If you need more info let me know.
org.apache.log4j.LogManager is a class from log4j 1.2 (not log4j2).
Therefore, one of your web app jars must be referencing it. The culprit should be visible in the stack trace.
Depending upon your circumstances, you may want to just add a log4j 1.2 jar to the web app as the two versions are completely independent of each other.
If you have use log4j 2 please do not forget to name your log4j2.xml instead of log4j.xml
as refered before:
org.apache.log4j.LogManager is a class from log4j 1.2 (not log4j2).
this problem meets when you combine use log4j 1.2 and log4j 2.x, maybe. so you have to add bridge api to you project.
this is a Migrating problem.
Log4j – Migrating from Log4j 1.x - Apache Log4j 2
add those to you pom.xml
<log4j2.version>2.7</log4j2.version>
<disruptor.version>3.3.6</disruptor.version>
<!--log4j2 dependencies -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${disruptor.version}</version>
</dependency>
then, you could use mvn dependency:resolve to see no log4j 1.2
[INFO] The following files have been resolved:
[INFO] org.springframework.data:spring-data-redis:jar:1.7.2.RELEASE:compile
[INFO] org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO] org.apache.logging.log4j:log4j-slf4j-impl:jar:2.7:compile
[INFO] com.lmax:disruptor:jar:3.3.6:compile
[INFO] org.apache.logging.log4j:log4j-1.2-api:jar:2.7:compile
[INFO] javax.mail:mail:jar:1.4.5:compile
[INFO] org.springframework:spring-tx:jar:4.3.1.RELEASE:compile
[INFO] org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO] org.apache.logging.log4j:log4j-jcl:jar:2.7:compile
[INFO] javax.activation:activation:jar:1.1:compile
[INFO] org.springframework:spring-beans:jar:4.3.1.RELEASE:compile
[INFO] org.springframework:spring-web:jar:4.3.1.RELEASE:compile
[INFO] org.springframework:spring-webmvc:jar:4.3.1.RELEASE:compile
[INFO] org.springframework:spring-oxm:jar:4.2.6.RELEASE:compile
[INFO] org.springframework:spring-jdbc:jar:4.3.1.RELEASE:compile
[INFO] com.alibaba:fastjson:jar:1.2.4:compile
[INFO] mysql:mysql-connector-java:jar:5.1.21:compile
[INFO] org.apache.tomcat:tomcat-servlet-api:jar:7.0.54:provided
[INFO] org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO] org.springframework:spring-context-support:jar:4.3.1.RELEASE:compile
[INFO] commons-beanutils:commons-beanutils:jar:1.8.3:compile
[INFO] org.springframework:spring-context:jar:4.3.1.RELEASE:compile
[INFO] org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] redis.clients:jedis:jar:2.8.1:compile
[INFO] org.springframework:spring-expression:jar:4.3.1.RELEASE:compile
[INFO] org.springframework.data:spring-data-commons:jar:1.12.2.RELEASE:compile
[INFO] org.springframework.data:spring-data-keyvalue:jar:1.1.2.RELEASE:compile
[INFO] junit:junit:jar:4.12:test
[INFO] org.springframework:spring-core:jar:4.3.1.RELEASE:compile
[INFO] commons-logging:commons-logging:jar:1.2:compile
[INFO] org.springframework:spring-aop:jar:4.3.1.RELEASE:compile
[INFO] org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO] org.slf4j:jcl-over-slf4j:jar:1.7.21:runtime
refers:
Log4j 1.x Adaptor – Log4j 1.2 Bridge - Apache Log4j 1.x Compatibility API
Log4j Commons Logging Adaptor – Commons Logging Bridge - Apache Log4j Commons Logging Bridge
SLF4J Binding Using Log4j – Log4j 2 SLF4J Binding - Apache Log4j SLF4J Binding