You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by simplemind <pa...@gmail.com> on 2011/05/17 06:36:10 UTC

strange class cast exception

I am new to this OSGi stuff and karaf, and I could really use some help
getting started.

I am trying to deploy a war bundle that uses resteasy servlet.  Here are
steps I took:

1. installed karaf 2.2.1 on linux ubuntu machine, latest java, latest
everything
2. built hello.war with MANIFEST.MF headers as described in Pax Web Extender
docs
3. added jsr311-api-1.1.1.jar, resteasy-jaxrs-2.1.0.GA.jar, and
servlet-api-2.5.jar to karaf/lib/ and karaf/deploy/
4. added org.osgi.framework.system.packages.extra= javax.ws.rs.core,
org.jboss.resteasy.plugins.server.servlet, javax.servlet.http, javax.servlet
in karaf/etc/config.properties
5. dropped hello.war into karaf/deploy/

3 and 4 was to get rid of NoClassDefFound errors on trying to start
hello.war when I did 5.  But then it got a class cast exception.  The
problem is that the class it is trying to cast from implements the interface
it is trying to cast to, so I can't understand why the cast is failing? 

Here is proof of that:
http://docs.jboss.org/resteasy/docs/2.0.0.GA/javadocs/org/jboss/resteasy/plugins/server/servlet/HttpServletDispatcher.html

And here is the exception from the karaf log file:

java.lang.ClassCastException:
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher cannot be
cast to javax.servlet.Servlet
        at
org.ops4j.pax.web.extender.war.internal.RegisterWebAppVisitorWC.visit(RegisterWebAppVisitorWC.java:200)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
        at
org.ops4j.pax.web.extender.war.internal.model.WebApp.accept(WebApp.java:583)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
        at
org.ops4j.pax.web.extender.war.internal.WebAppPublisher$HttpServiceListener.register(WebAppPublisher.java:170)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
... 

--
View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2951187.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: strange class cast exception

Posted by Achim Nierbeck <bc...@googlemail.com>.
Hi Like Freeman said,
if it's optional it's not resolved at a later stage.
But you are able to start the resolving again via the shell.
Do a restart or refresh of Bundle B and it should get all imports correctly.
So no need to re-deploy your bundle :-)

regards, Achim

2011/5/18 Freeman Fang <fr...@gmail.com>:
> Hi,
> I believe B.jar OPTIONAL import package c which export by C.jar,right? If
> the optional import isn't there, it won't affect the B.jar resolve and
> start.
> The key point is that you need deploy C.jar before B.jar, so that the
> optional import could be resolved correctly.
> Another way is just put A/B/C.jar into one feature, then use feature:install
> feature-name, it can ensure optional import could be resolved if it's
> available from this feature.
> Freeman
> On 2011-5-18, at 上午6:56, simplemind wrote:
>
> Ok, I am making progress on this.  Still haven't got it to work, but I am
> farther.  Wanted to follow up so no one puts much thought into my previous
> post.  The following issue was why I was having this error:
>
> 1. drop B.jar that exports package b and imports package c into deploy
> 2. drop A.jar that imports package b into deploy
>
> - get a NoClassDefFound c by B.jar
>
> 3. look at B.jar headers, c import is red
> 4. drop C.jar that exports c into deploy
> 5. look at B.jar headers, c import is white
> 6. drop A.jar into deploy
>
> - get a NoClassDefFound c by B.jar
>
> (so here is the workaround)
>
> 7. delete B.jar from deploy
> 8. drop B.jar into deploy
> 9. drop A.jar into deploy
>
> Now everything is fine.
>
> Is this how OSGi is supposed to work?  It seems to me that it shouldn't
> matter that I add C.jar after B.jar as the package import is resolved in
> either case.  Why should I have to redeploy B.jar before A will work?
>
> --
> View this message in context:
> http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2954762.html
> Sent from the Karaf - User mailing list archive at Nabble.com.
>
> ---------------------------------------------
> Freeman Fang
> FuseSource
> Email:ffang@fusesource.com
> Web: fusesource.com
> Twitter: freemanfang
> Blog: http://freemanfang.blogspot.com
> Connect at CamelOne May 24-26
> The Open Source Integration Conference
>
>
>
>
>
>
>
>



-- 
--
*Achim Nierbeck*


Apache Karaf <http://karaf.apache.org/> Committer & PMC
OPS4J Pax Web <http://wiki.ops4j.org/display/paxweb/Pax+Web/>
Committer & Project Lead

Re: strange class cast exception

Posted by Freeman Fang <fr...@gmail.com>.
Hi,

I believe B.jar OPTIONAL import package c which export by C.jar,right?  
If the optional import isn't there, it won't affect the B.jar resolve  
and start.
The key point is that you need deploy C.jar before B.jar, so that the  
optional import could be resolved correctly.

Another way is just put A/B/C.jar into one feature, then use  
feature:install feature-name, it can ensure optional import could be  
resolved if it's available from this feature.
Freeman
On 2011-5-18, at 上午6:56, simplemind wrote:

> Ok, I am making progress on this.  Still haven't got it to work, but  
> I am
> farther.  Wanted to follow up so no one puts much thought into my  
> previous
> post.  The following issue was why I was having this error:
>
> 1. drop B.jar that exports package b and imports package c into deploy
> 2. drop A.jar that imports package b into deploy
>
> - get a NoClassDefFound c by B.jar
>
> 3. look at B.jar headers, c import is red
> 4. drop C.jar that exports c into deploy
> 5. look at B.jar headers, c import is white
> 6. drop A.jar into deploy
>
> - get a NoClassDefFound c by B.jar
>
> (so here is the workaround)
>
> 7. delete B.jar from deploy
> 8. drop B.jar into deploy
> 9. drop A.jar into deploy
>
> Now everything is fine.
>
> Is this how OSGi is supposed to work?  It seems to me that it  
> shouldn't
> matter that I add C.jar after B.jar as the package import is  
> resolved in
> either case.  Why should I have to redeploy B.jar before A will work?
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2954762.html
> Sent from the Karaf - User mailing list archive at Nabble.com.

---------------------------------------------
Freeman Fang

FuseSource
Email:ffang@fusesource.com
Web: fusesource.com
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
Connect at CamelOne May 24-26
The Open Source Integration Conference









Re: strange class cast exception

Posted by simplemind <pa...@gmail.com>.
Ok, I am making progress on this.  Still haven't got it to work, but I am
farther.  Wanted to follow up so no one puts much thought into my previous
post.  The following issue was why I was having this error:

1. drop B.jar that exports package b and imports package c into deploy
2. drop A.jar that imports package b into deploy
 
- get a NoClassDefFound c by B.jar

3. look at B.jar headers, c import is red
4. drop C.jar that exports c into deploy
5. look at B.jar headers, c import is white
6. drop A.jar into deploy

- get a NoClassDefFound c by B.jar

(so here is the workaround)

7. delete B.jar from deploy
8. drop B.jar into deploy
9. drop A.jar into deploy

Now everything is fine.

Is this how OSGi is supposed to work?  It seems to me that it shouldn't
matter that I add C.jar after B.jar as the package import is resolved in
either case.  Why should I have to redeploy B.jar before A will work?

--
View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2954762.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: strange class cast exception

Posted by simplemind <pa...@gmail.com>.
I started over with clean karaf to show exactly what happens.

1. features:install war
2. drop resteasy-jaxrs.jar (bundle) into karaf/deploy/

All imports are resolved cleanly.  The exports include:
...
        org.jboss.resteasy.core,
        org.jboss.resteasy.logging,
        org.jboss.resteasy.plugins.server.servlet,
...

3. drop hello.war into karaf/deploy/

karaf@root> osgi:headers 72

Apache Felix Test 3 ReST Hello World (72)
-----------------------------------------
Manifest-Version = 1.0
Bnd-LastModified = 1305626104737
Archiver-Version = Plexus Archiver
Tool = Bnd-0.0.238
Originally-Created-By = 1.6.0_24 (Sun Microsystems Inc.)
Built-By = root
Web-ContextPath = /hello
Build-Jdk = 1.6.0_24
Created-By = 1.6.0_24 (Sun Microsystems Inc.)

Bundle-Name = Apache Felix Test 3 ReST Hello World
Bundle-Description = A test application for Felix + ReST.
Bundle-SymbolicName = hello
Bundle-Version = 1.0.0
Bundle-ManifestVersion = 2
Bundle-ClassPath = .,WEB-INF/classes

Private-Package =
        ebento.rest.helloworld;version=1.0.0
Import-Package =
        javax.servlet,
        javax.servlet.http,
        org.jboss.resteasy.core,
        org.jboss.resteasy.logging,
        org.jboss.resteasy.plugins.server.servlet

---------------------------------------------------------------------------

The log file says:

10:18:58,518 | INFO  | raf-2.2.1/deploy | WebXmlObserver                   |
nder.war.internal.WebXmlObserver  118 | 68 -
org.ops4j.pax.web.pax-web-extender-war - 1.0.3 | Using [hello] as web
application context name
10:18:58,520 | INFO  | raf-2.2.1/deploy | WebXmlObserver                   |
nder.war.internal.WebXmlObserver  133 | 68 -
org.ops4j.pax.web.pax-web-extender-war - 1.0.3 | Using [] as web application
root path
10:18:58,533 | INFO  | raf-2.2.1/deploy | HttpServiceFactoryImpl           |
.internal.HttpServiceFactoryImpl   33 | 59 -
org.ops4j.pax.web.pax-web-runtime - 1.0.3 | Binding bundle: [hello [72]] to
http service
10:18:58,543 | INFO  | raf-2.2.1/deploy | log                              |
.eclipse.jetty.util.log.Slf4jLog   55 | 46 - org.eclipse.jetty.util -
7.3.1.v20110307 | started
HttpServiceContext{httpContext=org.ops4j.pax.web.extender.war.internal.WebAppWebContainerContext@27738a7c}
10:18:58,582 | ERROR | raf-2.2.1/deploy | RegisterWebAppVisitorWC          |
internal.RegisterWebAppVisitorWC  215 | 68 -
org.ops4j.pax.web.pax-web-extender-war - 1.0.3 | Registration exception.
Skipping.
java.lang.ClassNotFoundException:
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)[:1.6.0_24]
        at java.security.AccessController.doPrivileged(Native
Method)[:1.6.0_24]
        at
java.net.URLClassLoader.findClass(URLClassLoader.java:190)[:1.6.0_24]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)[:1.6.0_24]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)[:1.6.0_24]
        at
org.apache.felix.framework.ExtensionManager$ExtensionManagerModule.getClassByDelegation(ExtensionManager.java:697)[org.apache.felix.framework-3.0.9.jar:]
        at
org.apache.felix.framework.resolver.WireImpl.getClass(WireImpl.java:99)[org.apache.felix.framework-3.0.9.jar:]
...

--
View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2953389.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: strange class cast exception

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
What's your MANIFEST, especially the Import-Package statement ?

You should have something like:

Import-Package: javax.servlet*, ...*

Regards
JB

On 05/17/2011 12:06 PM, simplemind wrote:
> I left out that I have the war, jetty, and http features installed already
> when that happens.
>
> If I don't put any jars in the lib directory I get NoClassDefFoundError(s).
> For instance, I just took servlet-api jar out of there and then got this:
>
> java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2951890.html
> Sent from the Karaf - User mailing list archive at Nabble.com.

Re: strange class cast exception

Posted by simplemind <pa...@gmail.com>.
I left out that I have the war, jetty, and http features installed already
when that happens.

If I don't put any jars in the lib directory I get NoClassDefFoundError(s). 
For instance, I just took servlet-api jar out of there and then got this:

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet

--
View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2951890.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: strange class cast exception

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi,

David is right: you have interfaces/implementations present in two 
classloaders.

 From a general point of view, you shouldn't put any jar into the 
karaf/lib folder.
If you install the war features:

features:install war

you will have the javax.servlet.* packages available (provided by 
Geronimo Spec bundle).
So you can use Import-Package: javax.servlet*

For non-OSGi jar, you can:
- find the OSGified version of the jar. ServiceMix provides a large set 
of wrap bundles: 
http://repo1.maven.org/maven2/org/apache/servicemix/bundles/
- you can use the wrap deployer which create an OSGi bundle on the fly 
using URL like wrap:mvn:.... or wrap:http:.... You can find more 
information about the wrap bundle here: 
http://karaf.apache.org/manual/2.1.99-SNAPSHOT/users-guide/deployer.html

Regards
JB

On 05/17/2011 07:48 AM, David Jencks wrote:
> Your problem is that the servlet class is available from at least two bundles or classloaders and the implementation in your app is wired up to a different one from pax web.  There may be easier ways to see this but you can run investigate this in a debugger.
>
> I'm pretty sure your problems are caused by putting the jars in karaf/lib and modifying the packages exported by the framework.  Instead, try to find or create bundleized versions of the jars and deploy them as bundles.  For instance, you won't need the servlet api jar since pax web already needs them installed...  Some ee spec jars need to be modified to work in osgi: both geronimo and servicemix have versions of these.
>
> Others can probably give you more detailed advice on how to fix this...
>
> david jencks
>
> On May 16, 2011, at 9:36 PM, simplemind wrote:
>
>> I am new to this OSGi stuff and karaf, and I could really use some help
>> getting started.
>>
>> I am trying to deploy a war bundle that uses resteasy servlet.  Here are
>> steps I took:
>>
>> 1. installed karaf 2.2.1 on linux ubuntu machine, latest java, latest
>> everything
>> 2. built hello.war with MANIFEST.MF headers as described in Pax Web Extender
>> docs
>> 3. added jsr311-api-1.1.1.jar, resteasy-jaxrs-2.1.0.GA.jar, and
>> servlet-api-2.5.jar to karaf/lib/ and karaf/deploy/
>> 4. added org.osgi.framework.system.packages.extra= javax.ws.rs.core,
>> org.jboss.resteasy.plugins.server.servlet, javax.servlet.http, javax.servlet
>> in karaf/etc/config.properties
>> 5. dropped hello.war into karaf/deploy/
>>
>> 3 and 4 was to get rid of NoClassDefFound errors on trying to start
>> hello.war when I did 5.  But then it got a class cast exception.  The
>> problem is that the class it is trying to cast from implements the interface
>> it is trying to cast to, so I can't understand why the cast is failing?
>>
>> Here is proof of that:
>> http://docs.jboss.org/resteasy/docs/2.0.0.GA/javadocs/org/jboss/resteasy/plugins/server/servlet/HttpServletDispatcher.html
>>
>> And here is the exception from the karaf log file:
>>
>> java.lang.ClassCastException:
>> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher cannot be
>> cast to javax.servlet.Servlet
>>         at
>> org.ops4j.pax.web.extender.war.internal.RegisterWebAppVisitorWC.visit(RegisterWebAppVisitorWC.java:200)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
>>         at
>> org.ops4j.pax.web.extender.war.internal.model.WebApp.accept(WebApp.java:583)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
>>         at
>> org.ops4j.pax.web.extender.war.internal.WebAppPublisher$HttpServiceListener.register(WebAppPublisher.java:170)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
>> ...
>>
>> --
>> View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2951187.html
>> Sent from the Karaf - User mailing list archive at Nabble.com.
>

Re: strange class cast exception

Posted by David Jencks <da...@yahoo.com>.
Your problem is that the servlet class is available from at least two bundles or classloaders and the implementation in your app is wired up to a different one from pax web.  There may be easier ways to see this but you can run investigate this in a debugger.

I'm pretty sure your problems are caused by putting the jars in karaf/lib and modifying the packages exported by the framework.  Instead, try to find or create bundleized versions of the jars and deploy them as bundles.  For instance, you won't need the servlet api jar since pax web already needs them installed...  Some ee spec jars need to be modified to work in osgi: both geronimo and servicemix have versions of these.

Others can probably give you more detailed advice on how to fix this...

david jencks

On May 16, 2011, at 9:36 PM, simplemind wrote:

> I am new to this OSGi stuff and karaf, and I could really use some help
> getting started.
> 
> I am trying to deploy a war bundle that uses resteasy servlet.  Here are
> steps I took:
> 
> 1. installed karaf 2.2.1 on linux ubuntu machine, latest java, latest
> everything
> 2. built hello.war with MANIFEST.MF headers as described in Pax Web Extender
> docs
> 3. added jsr311-api-1.1.1.jar, resteasy-jaxrs-2.1.0.GA.jar, and
> servlet-api-2.5.jar to karaf/lib/ and karaf/deploy/
> 4. added org.osgi.framework.system.packages.extra= javax.ws.rs.core,
> org.jboss.resteasy.plugins.server.servlet, javax.servlet.http, javax.servlet
> in karaf/etc/config.properties
> 5. dropped hello.war into karaf/deploy/
> 
> 3 and 4 was to get rid of NoClassDefFound errors on trying to start
> hello.war when I did 5.  But then it got a class cast exception.  The
> problem is that the class it is trying to cast from implements the interface
> it is trying to cast to, so I can't understand why the cast is failing? 
> 
> Here is proof of that:
> http://docs.jboss.org/resteasy/docs/2.0.0.GA/javadocs/org/jboss/resteasy/plugins/server/servlet/HttpServletDispatcher.html
> 
> And here is the exception from the karaf log file:
> 
> java.lang.ClassCastException:
> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher cannot be
> cast to javax.servlet.Servlet
>        at
> org.ops4j.pax.web.extender.war.internal.RegisterWebAppVisitorWC.visit(RegisterWebAppVisitorWC.java:200)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
>        at
> org.ops4j.pax.web.extender.war.internal.model.WebApp.accept(WebApp.java:583)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
>        at
> org.ops4j.pax.web.extender.war.internal.WebAppPublisher$HttpServiceListener.register(WebAppPublisher.java:170)[75:org.ops4j.pax.web.pax-web-extender-war:1.0.1]
> ... 
> 
> --
> View this message in context: http://karaf.922171.n3.nabble.com/strange-class-cast-exception-tp2951187p2951187.html
> Sent from the Karaf - User mailing list archive at Nabble.com.