I am using Struts2 framework and using HttpClient class for making put request.
When I try to call httpClient's requestEntity method in LoginAction class I am getting the following error.
SEVERE: Exception starting filter struts2
java.lang.NoClassDefFoundError: org/apache/commons/httpclient/methods/RequestEntity
I have included all the required jar files in the class path.
Kindly help me figure out what could be the problem.
If you are using older version( apache commons less than 3.0) then that class will not be available in old jar. You have to use Apache commons 3.0 library. org/apache/commons/httpclient/methods/RequestEntity class is added since v3.0. Check javadoc.
Generally If we do not have jar added in classpath(or not in project) then we get ClassNotFoundException and if we have jar included in project and it is in classpath but particular class is not available then we get NoClassDefFoundError.
Related
I am using eclipse neon.3 version and tomcat 8.5.15,I tried to execute simple hello world program using spring mvc but I am getting this issue=>
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet.
It happened only with spring mvc projects,rather than spring mvc,other codes executed successfully. Also mentioned the jar files which I have uploaded.
Please help me.
There is no javax.servlet-api.jar in the classpath. Maybe it is not in tomcat's lib folder.
Try to add this jar to your WEB-INF/lib directory. To download a jar go to https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0
I had the same problem when I first tried to use Tomcat. If you imported HttpServlet class from jakarta.servlet.http try importing it from javax.servlet.http instead, that's what worked for me. Since I used jakarta-servlet-api dependency I expected to find the HttpServlet class in the javax.servlet.http package, however I was wrong.
I extracted the jakarta-servlet-api.jar and saw that the package is called javax.
So I imported HttpServlet from javax.servlet.http instead and it worked fine.
UPDATE (I have updated the question since I only experience the following error)
I have replaced a Jena library (to get the newer version) in my Eclipse web application. Though I can use the new library and compile without error, I receive the the following exceptions when my code reaches the methods of Jena.
SEVERE: Servlet.service() for servlet [com.packages.servlets.CreatePatternServlet] in context with path [/TempProject] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: Could not initialize class com.hp.hpl.jena.rdf.model.impl.ModelCom
I have log4j.properties in my project and other solutions I have came across did not seem to help.
Many thanks in advance
The Jena library depends on slf4j, so you need to get the slf4j-api.jar on your classpath. If you use Maven, you can simple add the Jena library with this XML snippet:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
and it would pull all the dependencies without any additional work on your part.
Problem solved when I added the libraries also into lib folder under WEB-INF.
I am trying to add metrics library to existing webservice on WAS 7. I am getting below error
Error 404: javax.servlet.UnavailableException: SRVE0203E: Servlet [AdminServlet]: com.yammer.metrics.reporting.AdminServlet was found, but is missing another required class. SRVE0206E: This error typically implies that the servlet was originally compiled with classes which cannot be located by the server. SRVE0187E: Check your class path to ensure that all classes required by the servlet are present.SRVE0210I: This problem can be debugged by recompiling the servlet using only the classes in the application's runtime class path SRVE0234I
What are the other run-time dependencies required for metrics-servlet-2.2.0?
I have metrics-core-2.2.0.jar and metrics-servlet-2.2.0.jar in my WEB-INF\lib folder.
Threads, ping and healthcheck servlets work fine.
I think your missing some more required jars, are you not using maven or gradle for dependency management
Please refer here to know all required jars that metrics-servlet-2.2.0.jar depends on. http://mvnrepository.com/artifact/com.yammer.metrics/metrics-servlets/3.0.0-BETA1
My suggestion is, it is always difficult to maintain dependencies without Maven/Gradle or any other build tools :).
Getting following exception in Jboss 5.0 EAP but it work fine in JBoss 5.1 GA.
we are using POI 3.7 and jars included are
poi-3.7.jar
poi-ooxml-schemas.jar
poi-ooxml.jar
The stack trace is
ERROR [org.apache.catalina.core.ContainerBase.[jboss.ueb].[localhost].[fesbcon-Fig].[Faces Servlet]]
3;13;44.4g3pM (http-0.0.0.0-8280-1) Servlet.service() -For servlet Faces Servlet threu exception
java.lang.NoClassDe-FFoundError: Could not initialize class org.apache.poi.POIXMLDocument
at org.apache.poi.ss.usermodel.HorkbookFactory.create(HorkbookFactory.java:62)
at com.-Ferguson.esb.con-Fig.controller.AssociationsExcelUploadController.submit(Unknoun Source)
at sun.re-Flect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.re-Flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.re-Flect.DelegatingMethodAccessorImpl.invoke(Delegating?ethodAccessorImpl.java:25)
at java.lang.re-Flect.Method.invoke(Method.java:597)
at org.apache.my-Faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:132)
at org.apache.my-Faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:61)
Please Advise how to solve this issue in JBoss 5.0 EAP
It looks like you're application is throwing the exception you're seeing because an Apache XMLBeans JAR or class is not present when running under JBoss 5.0. It seems Apache POI is trying to load the class org.apache.xmlbeans.XMLOptions but it cannot find this class.
The message Could not initialize class SomeClass indicates that the JVM has twice tried and failed to load and statically initialize the class SomeClass. In this case, the class in question is org.apache.poi.POIXMLDocument.
Static initialization for a class consists of statically initializing its superclass, assigning values to all static fields and running all static initializer blocks. The POIXMLDocument class has a few static String constants, which won't cause any problem, but no static initializer. It is however a subclass of POIXMLDocumentPart, which is a subclass of Object and which has the following static initialization code:
private static POILogger logger = POILogFactory.getLogger(POIXMLDocumentPart.class);
public static final XmlOptions DEFAULT_XML_OPTIONS;
static {
DEFAULT_XML_OPTIONS = new XmlOptions();
DEFAULT_XML_OPTIONS.setSaveOuter();
DEFAULT_XML_OPTIONS.setUseDefaultNamespace();
DEFAULT_XML_OPTIONS.setSaveAggressiveNamespaces();
}
This static initialization will fail if the JVM cannot load all of the POILogger, POILogFactory and XmlOptions classes.
The POILogger and POILogFactory classes are both imported from the package org.apache.poi.util.POILogFactory, and both classes are contained within poi-3.7.jar, so they're not the problem here. So, by elimination, it seems the XmlOptions class, imported from org.apache.xmlbeans.XmlOptions, must be missing.
I found this XMLOptions class within xbean.jar contained within the lib folder of xmlbeans-2.6.0.zip downloadable from one of the mirrors here.
It seems likely to me that adding this JAR will fix the problem on JBoss 5.0 EAP. However, I'm aware you said your application works fine in JBoss 5.1 GA, which implies to me that JBoss 5.1 GA contains a copy of this JAR whereas 5.0 EAP doesn't. As a result I'm not sure what the best way to fix this problem is. I'd be hesitant to add this XMLBeans JAR to your application as doing so may cause issues when you run it under JBoss 5.1. I don't know whether there's a way of adding extra 'library' JARs to JBoss 5.0, though - perhaps that's worth looking at?
I had the same error running on JBoss 8.2 (WildFly 8.2.0.Final) with Apache POI 3.14
Error:
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.POIXMLTypeLoader
I fixed this by including the latest xmlbeans-2.6.0 jar (I previously had 2.4.0) into my deployment which is included with the Apache POI 3.14 distribution in the ooxml-lib folder.
Placing the Following jars in classpath will do the trick:
dom4j-1.6.1.jar
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
xmlbeans-2.3.0.jar
By adding only the xbean.jar alone, you can not solve the problem. It will continue complaining class not found. What I did is import not only xbean.jar but also other jar files which are listed under lib folder of xmlbeans-2.5.0
I need to integrate a REST client into an existing OSGi application implemented using Apache Felix. The REST service is based on RESTeasy implementation (version 2.3.2.Final) of JAX-RS. I created a separate bundle with clients' dependencies, exporting required RESTeasy packages and importing them in the bundle where the client is used, but unfortunately I cannot get it working inside of the OSGi context.
I tried two different approaches. First one using the generic ClientRequest:
ClientRequest request = new ClientRequest(MyService.URL_TEST+"/stats");
request.body(javax.ws.rs.core.MediaType.APPLICATION_XML, stats);
ClientResponse<String> response = request.post(String.class);
The error that I get in this case is pretty weird:
[java] java.lang.RuntimeException: java.lang.ClassCastException:
org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor cannot be cast to
org.jboss.resteasy.client.ClientExecutor
where I it is known for sure that ApacheHttpClient4Executor implements the ClientExecutor interface.
When I try to use my own REST client wrapper around RESTeasy like this:
MyService myService = MyServiceClient.getInstance();
myService.saveStatistics(stats);
I get a different exception:
[java] java.lang.LinkageError: ClassCastException: attempting to
castjar:file:/D:/Development/Eclipses/eclipse_4.2_j2ee_x64/lib/jaxrs-api-2.3.2.Final.jar
!/javax/ws/rs/ext/RuntimeDelegate.classtobundle:
//78.0:1/javax/ws/rs/ext/RuntimeDelegate.class
As far as I understand, the LinkageError most probably has to do with the way RESTeasy initializes the RuntimeDelegate using some classloader tricks, which probably fall under the restrictions of OSGi framework. I get the suspicion that the java.lang.ClassCastException mentioned first has the same source.
Is there any way to get RESTeasy working inside of OSGi?
PS: discussion about a similar issue with RESTeasy, but outside of OSGi: java.lang.LinkageError: ClassCastException
Update:
these are the libraries included into restclient bundle:
activation-1.1.jar commons-codec-1.2.jar commons-httpclient-3.1.jar commons-io-2.1.jar commons-logging-1.0.4.jar flexjson-2.1.jar httpclient-4.1.2.jar httpcore-4.1.2.jar javassist-3.12.1.GA.jar jaxb-api-2.2.3.jar jaxb-impl-2.2.4.jar jaxrs-api-2.3.2.Final.jar jcip-annotations-1.0.jar jettison-1.3.1.jar jsr250-api-1.0.jar junit-4.10.jar log4j-1.2.14.jar resteasy-jaxb-provider-2.3.2.Final.jar resteasy-jaxrs-2.3.2.Final.jar resteasy-jettison-provider-2.3.2.Final.jar scannotation-1.0.3.jar slf4j-api-1.6.4.jar slf4j-log4j12-1.6.4.jar myservice-common-0.1.0.3.jar my-service-client-0.1.0.3-SNAPSHOT.jar stax-api-1.0-2.jar xmlpull-1.1.3.1.jar xpp3_min-1.1.4c.jar xstream-1.4.2.jar
These are the exports from the restclient bundle: javax.ws.rs, javax.ws.rs.ext, javax.ws.rs.core, org.jboss.resteasy.client, org.jboss.resteasy.client.cache, org.jboss.resteasy.client.extractors, org.jboss.resteasy.client.marshallers, org.jboss.resteasy.client.core.executors, javax.xml.bind.annotation, org.jboss.resteasy.plugins.providers, org.jboss.resteasy.plugins.providers.jaxb, org.jboss.resteasy.spi
Have a look at the SpringSource Bundle Repo, it's got some very useful pre-built bundles of common libraries including the Apache HTTP Client which we are using (in conjunction with gson) to do our RESTful comms.
(unfortunately a legacy module of my project still uses OSGi, but using RESTeasy 3.0.16 now)
When I need to OSGify a dependency my preferred solution now is to wrap it using the excellent Apache Ops4j Pax Tipi project.
The project provides a preconfigured Maven setup (parent POM handles the bundling) and you just have to adapt the GAV coordinates of the original project in a Tipi sub module with a org.apache.ops4j.pax.tipi prefix and build the new bundle project which draws in the original dependency, unpacks and wraps it as OSGi bundle.
You can start from an existing Tipi sub project that best matches your project setup (dependencies, etc.) and adapt any OSGi imports/exports missing (most often, these are created automatically by the maven-bundle-plugin anyway).
This worked quite well for me as long as the original project did not contain too many exotic or malformed dependencies.
However you may run into snags like transitive dependencies using the root package, as I currently experience, which can be a real show stopper (finding out which library is a real nightmare).
Unfortunately, RESTeasy seems to be affected by this, as I get exactly the same error (default package , even after declaring non-test and non-provided dependencies as optional:
The default package '.' is not permitted by the Import-Package syntax.
Upgrading the maven-bundle-plugin to the latest release 3.0.1 yields a different error (even less helpful):
[ERROR] Bundle org.ops4j.pax.tipi:org.ops4j.pax.tipi.resteasy-jaxrs:bundle:3.0.16.Final.1 : Can not parse name from bundle native code header:
[ERROR] Error(s) found in bundle configuration
Update seems to be solved by upping Tipi version in POM to 1.4.0, testing...
Is RESTEasy mandatory ?
I personally use jersey in OSGi and it is working perfectly, both as client and server.
This problem isn't limited to RESTeasy. It also occurs with Jersey.
It is occurring because you have two copies of the JAX-RS classes on the classpath.
You can see this in the LinkageError:
[java] java.lang.LinkageError: ClassCastException: attempting to cast jar:file:/D:/Development/Eclipses/eclipse_4.2_j2ee_x64/lib/jaxrs-api-2.3.2.Final.jar!/javax/ws/rs/ext/RuntimeDelegate.class to bundle://78.0:1/javax/ws/rs/ext/RuntimeDelegate.class
i.e. one copy is coming from:
D:/Development/Eclipses/eclipse_4.2_j2ee_x64/lib/jaxrs-api-2.3.2.Final.jar
and the other from the OSGI bundle.
This causes problems for the RuntimeDelegate class, which by default uses the system class loader to create the RuntimeDelegate implementation (see javax.ws.rs.ext.FactoryFinder).
The problem can also occur if the same jar is loaded via two different class loaders.
There are a couple of workarounds:
remove the jaxrs-api-2.3.2.Final.jar from the system class path
set the thread context class loader to that of your bundle, prior to making any JAX-RS calls.
The FactoryFinder will use this to load the RuntimeDelegate.
To avoid polluting your code with calls to Thread.currentThread().setContextClassLoader(myBundleClassLoader), you can wrap your JAX-RS client using a Proxy. e.g. see the Thread context classloader section of https://puredanger.github.io/tech.puredanger.com/2007/06/15/classloaders/