NoClassDefFoundError. How to set dependencies and deploy a java app? - java

My Java app requires org.objectweb.asm library. I specified 'asm' dependency in pom. That deploys the library together with the app. Still the app throws exception NoClassDefFoundError: org/objectweb/asm/ClassVisitor.
java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:112)
at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78)
... many more
How can I fix the problem?
Details:
I am using Glassfish 2.1.1. The app requires jersey 1.1.4, jersey requires asm 3.1. I assume 1.1.4 version is required by glassfish 2.1.1
If I run Glassfish updatetool and install Jersey on the server then my app loads and runs with no problems. My client doesn't have Jersey installed on their server and they can not use updatetool.
Glassfish 2.1.1 updatetool installs jersey 1.1.4 and asm-3.1.jar in glassfish/lib directory.
When jersey is uninstalled, updatetool removes asm too.
If I include jersey and asm as dependencies and deploy my war file then jersey and asm jars go into local location, e.g. glassfish/domains/domain1/applications/j2ee-modules/MYAPPNAME/WEB-INF/lib/asm-3.1.jar.
Glassfish updatetool puts asm into lib folder: glassfish/lib directory the app start deploying and working correctly.
Here is my maven pom file dependency section:
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>1.17.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

I had a conflict with an older jersey library stored in glassfish/lib folder:
glassfish/lib/jersey-bundle-1.0.3.1.jar
It was loaded and used instead of jars placed in a folder local to the app:
glassfish/domains/domain1/applications/j2ee-modules/MYAPPNAME/WEB-INF/lib/
Jersey jar from 'lib' folder was looking for 'asm' library in the same 'lib' folder, hence my locally placed asm-3.1.jar was never found.
I expected that 'local' jars were loaded first. Appeared that the search order is different. Is that really the case? Please tell me if I am wrong.
I found the problem by verifying which library is used with the following code:
logger.debug(PackagesResourceConfig.class.getResource("PackagesResourceConfig.class"));
Note PackagesResourceConfig class listed in the exception thrown.
Hopefully this answer will help someone else to save their time.

Try something like that in your pom:
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>

Related

Apache HTTPClient dependency issue : POM

I'm having issue while using Apache HttpClient in my java application.
2019-02-11 07:09:18,270 ERROR [Call-Dequeue-Delegator] (HibernateUtil.java:275) - Building SessionFactory failed.
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
at java.lang.Class.getDeclaredMethods0(Native Method)
It runs fine on my local machine but not on servers. Since it is a client application I'm not having details of server etc.
Following is my Code:
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(20)
.setConnectionRequestTimeout(30).build();
// Creating client with request configuration(timeouts) & disabling redirect following
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).disableRedirectHandling().build();
String endpoint = this.getSOAPEndPoint();
String queryParamWithSurveyData = addDataToQueryParam(endpoint, customRequestModel);
endpoint += queryParamWithSurveyData;
HttpRequestBase httpRequestWithoutBody = null; // Failing at this
In the above code it's failing at the last line & I'm surprised why not on first one since both (RequestConfig & HttpRequestBase) are imported from HTTPClient.jar
Following is my POM snippet
//Existing sample dependency
<dependency> //Existing sample dependency
<groupId>com.connectfirst.intelliqueue</groupId>
<artifactId>gson</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/couchbase/gson-2.3.jar</systemPath>
<version>1</version>
</dependency>
// New dependencies added
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
I saw somewhere that HttpClient is dependent on some other JARs as well & therefore added the following dependencies as well but had no luck.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/commons-codec-1.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/commons-logging-1.2.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/fluent-hc-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-cache-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-osgi-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-win</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-win-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpmime-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna-4.5.2.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna-platform-4.5.2.jar</systemPath>
<version>1</version>
</dependency>
The JAR gets created successfully on my local with maven clean install.
Any lead would be helpful.
Thanks.
Why are you scoping to system? That would imply that the jars are on the classpath of the running application. If you switch that to compile, assuming you're packaging as a spring-boot JAR or WAR file, they will be included in the resultant artifact. Without that the application cannot find the dependencies you are working with.
The scope attribute dictates where dependencies should be placed on the classpath.
compile - the artifact is available for reference by the project code and should be bundled (ostensibly) with the resultant artifact (e.g. embedded in WAR under WEB-INF/lib or spring-boot as BOOT-INF/lib)
provided - the artifact is available for reference by the project source code but is not included in the resultant artifact (think JSP libraries in a web app, you want to defer to the container implementation).
runtime - the dependency classes are not available for direct reference by the project source code but is referenced at runtime (think JDBC driver, you don't specifically use that you load it, traditionally, by reflection using the name).
test - the dependency classes are available to sources in src/test/java (etc). IDEs are supposed to separate the references but not always the case (e.g. Eclipse). If you try to reference a test scoped dependency from src/main/java your code will not compile because the dependency is not available.
system - The artifact in this case is explicitly located by path and is expected to be available at runtime within the consuming application's classpath.
import - This is only supported within the dependencyManagement elements.
You can see the description from the official documentation.
Please make sure you are using correct java version on server.
Please note that as of 4.4, HttpClient requires Java 1.6 or newer. for httpClent after 4.4 version you need to use newer java versions (say java 11) .

Maven Spring Boot 2.0.4 - and elastic search 6.2.2 transport jar not in dependency tree

I am writing a SpringBoot 2.0.4 application, (Executable .jar) and I am trying to interface with an elastic search that I do not control. It is 6.2.2
Now I know Spring does not support that, and that's fine as I am not using any Spring classes, I am interfacing with elastic directly. However, The problem comes in trying to exclude elastic from spring in my pom. Here is my pom fragment:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.2.2</version>
</dependency>
Now Eclipse has a yellow line under the versions for elasticsearch and transport, saying that the version is being overridden. But when I look at the dependency tree, I see all the elastic jars with the correct version of 6.2.2 -- EXCEPT for transport. That jar is not there at all. I looked in my executable jar, and it's definitely not there. Shockingly, my app throws a class not found error for org/elasticsearch/common/transport/InetSocketTransportAddress
How do I get that jar into my dependencies?
EDIT
So I added:
<properties>
<elasticsearch.version>6.2.2</elasticsearch.version>
</properties>
to my pom, and overrode the spring version, but the transport jar is not showing up in my dependency tree! How do I get this file into my executable jar?

conflict in dependencies using Maven for 3rd party library

I'm using Maven in my project and I have a 3rd party library that uses Xerces. In my project there are some other Maven modules which has in their dependencies some others XML libraries.
So my problem is, when I use this third party library in an dependent maven project it works fine. And when I use it in a maven module in my project it generate some exceptions.
org.w3c.dom.ls.LSException: unknown protocol: c
at com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl.writeToURI(DOMSerializerImpl.java:1010)
at generators.Generator.setConf(Generator.java:1567)
When I debug both projects, I remarked that in the second one it instantiate DOMSerializerImpl class form com.sun.org.apache.xml.internal.serialize package, and in the first one uses org.apache.xml.serialize package. Despite the same dependencies are specified in my pom files of both projects.
The dependencies of this this party library are:
<dependency>
<groupId>xml-resolver</groupId>
<artifactId>xml-resolver</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>serializer</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesSamples</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
Can anyone help me?

When deploying a web application I get the exception NoClassDefFoundError:LocalizableImpl

I have a standard J2EE web application that includes web services. I'm using the webservices-rt library to host the services. [See the maven dependency below]. However, I get the following exception at run time:
SEVERE: Exception sending context initialized event to listener instance of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener
java.lang.NoClassDefFoundError: com/sun/xml/ws/util/localization/LocalizableImpl
at com.sun.xml.ws.util.exception.JAXWSExceptionBase.<init>(JAXWSExceptionBase.java:63)
at com.sun.xml.ws.transport.http.servlet.WSServletException.<init>(WSServletException.java:47)
at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:118)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [...]
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.ws.util.localization.LocalizableImpl
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 33 more
Maven WS Dependency
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>webservices-rt</artifactId>
<version>1.4</version>
<scope>compile</scope>
</dependency>
Am I missing a library? I've tried adding jaxws-rt. However, that requires an additional repo [jboss]. I'm a bit leery of that, as that it introduces a lot of new libraries into the project.
If you are using Maven, add below to your project should solve your problem:
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.2</version>
</dependency>
You don't have to add others jar because it will automatically pull the rest of dependencies. I prefer to add the jar to my war instead of Tomcat lib. I think it is more portable.
try
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.
For a maven, tomcat application try these dependencies in your pom.xml
<dependencies>
<!-- jax-ws maven dependency -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
</dependency>
<!-- servlet provided by tomcat -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.stream.buffer/streambuffer -->
<dependency>
<groupId>com.sun.xml.stream.buffer</groupId>
<artifactId>streambuffer</artifactId>
<version>1.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.ws/policy -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>policy</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.gmbal/gmbal-api-only -->
<dependency>
<groupId>org.glassfish.gmbal</groupId>
<artifactId>gmbal-api-only</artifactId>
<version>3.2.0-b003</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.ha/ha-api -->
<dependency>
<groupId>org.glassfish.ha</groupId>
<artifactId>ha-api</artifactId>
<version>3.1.9</version>
</dependency>
</dependencies>
I hope this works for new people who will face this issue
Regards
Declaring the JBoss repo doesn't automatically import the repo libraries. It simply makes the libraries available for importing.
Bottom line is that if you want to use a class that's in a library, then you have to pull the library into your project. If the library is in the JBoss repo, then you have to declare the JBoss repo.

What is the right Maven dependency for javax.jms.* classes?

I need to import javax.jms.* classes. What is the right dependency to include into a Maven project? I'm trying javax.jms:jms:1.1, but no luck (it's pom, not jar).
ps. The only workaround I've found so far is: javax:javaee-api:6.0 (from Maven Central).
In ActiveMQ as well as some other projects like Qpid JMS we pull in the JMS spec classes from Apache Geronimo JARs, the 1.1 APIs are available in this dependency:
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1.1</version>
</dependency>
For JMS 2 APIs you'd need to use a different dependency, for instance
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<version>1.0-alpha-2</version>
</dependency>
These are both Apache 2.0 licensed dependencies.
Another option which is not Apache licensed is here as others have pointed out.
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
The Sun license doesn't allow maven repositories to host this (and other) artifacts.
Here is the documentation explaining this and what you should do instead...
Maven - Guide to coping with Sun JARs
What it says is you need to download the JAR manually and then install it into your own local repository or nexus server.
The pom.xml files hosted at maven central for these artifacts contain information on where you can download the JARs from.
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
I have successfully used this one:
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
Go to Maven Search site and search for javax. Open the latest version for groupId javax and artifactId javaee-api
The current version is 7.0 [Maven dependency information]
If you just want the JMS libs, without the rest of javaee, use the following:
https://mvnrepository.com/artifact/javax.jms/javax.jms-api/2.0.1
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
According to mvnrepository, the dependency to add in the pom of your project is the following:
<dependency>
<groupId>jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
Check out the dependencies listed on grepcode.com.
I only discovered this site recently, and it rocks!
http://grepcode.com/search/?query=javax.jms.*
It looks like the Geronimo jars on maven central should sort your issues out.
This worked for myself
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>

Categories