Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » How to use external jar?
How to use external jar? [message #1005358] Thu, 24 January 2013 23:55 Go to next message
kilosi mesdor is currently offline kilosi mesdor
Messages: 6
Registered: January 2013
Junior Member
Hello,

I have a bunch of external jars(around 10) that I use for one of my bundles. For example, I use the commons-lang3-3.1.jar for various things. When I run my OSGi framework in Eclipse - it all works great. I just need to make sure I add them to my bundle's class path.

However, when I execute it outside of Eclipse, it throws java.lang.NoClassDefFoundError: errors. I'm not sure why this is happening.

I've done a bit of research and it seems that I have really two options:

1. Place the jar file in the root directory of my bundle (in this case, the bundle's jar file), and then make sure that the manifest file has a proper Export-Package on it.
I have tried this - and it still cannot find the jar.
2. Make a bundle of the jar file, and install that, ensuring all dependencies are resolved. THIS does NOT work for jar files. However, if I copy the actual source and use that instead of a jar file within the bundle, then it works. But I won't be able to do this for all of my jars since the source isn't present and I don't want to have to build every single external jar I use...

I'm pretty new to OSGi, and any help would be useful.

The below is from option 1:
Stacktrace:
java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
        at ats.server.ATSActivator.start(ATSActivator.java:58)
        at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:783)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:774)
        at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:755)
        at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:370)
        at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:284)
        at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:276)
        at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java:252)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute(FrameworkCommandInterpreter.java:155)
        at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:156)
        at org.eclipse.osgi.framework.internal.core.FrameworkConsole.runConsole(FrameworkConsole.java:141)
        at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:105)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
        at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:506)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:422)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
        at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 18 more


Here is the manifest file:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: ATS
Bundle-SymbolicName: ATS
Bundle-Version: 1.0.0.1
Bundle-Activator: ats.server.ATSActivator
Private-Package: org.apache.commons.lang3,
 org.apache.commons.lang3.builder,
 org.apache.commons.lang3.concurrent,
 org.apache.commons.lang3.event,
 org.apache.commons.lang3.exception,
 org.apache.commons.lang3.math,
 org.apache.commons.lang3.mutable,
 org.apache.commons.lang3.reflect,
 org.apache.commons.lang3.text,
 org.apache.commons.lang3.text.translate,
 org.apache.commons.lang3.time,
 org.apache.commons.lang3.tuple
Import-Package: ats_socketer,
 org.apache.commons.lang3,
 org.apache.commons.lang3.builder,
 org.apache.commons.lang3.concurrent,
 org.apache.commons.lang3.event,
 org.apache.commons.lang3.exception,
 org.apache.commons.lang3.math,
 org.apache.commons.lang3.mutable,
 org.apache.commons.lang3.reflect,
 org.apache.commons.lang3.text,
 org.apache.commons.lang3.text.translate,
 org.apache.commons.lang3.time,
 org.apache.commons.lang3.tuple,
 org.osgi.framework;version="1.3.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ClassPath: .,
 ../.,
 akuma-1.8.jar,
 ATSMailer.jar,
 commons-lang3-3.1.jar,
 junit-4.3.1.jar,
 mail.jar,
 pewParser.jar,
 platform-3.4.0.jar
Require-Bundle: ATS_Socketer;bundle-version="1.0.0"
Export-Package: org.apache.commons.lang3,
 org.apache.commons.lang3.builder,
 org.apache.commons.lang3.concurrent,
 org.apache.commons.lang3.event,
 org.apache.commons.lang3.exception,
 org.apache.commons.lang3.math,
 org.apache.commons.lang3.mutable,
 org.apache.commons.lang3.reflect,
 org.apache.commons.lang3.text,
 org.apache.commons.lang3.text.translate,
 org.apache.commons.lang3.time,
 org.apache.commons.lang3.tuple


The .jar file is in the root directory of this bundle. It is in included in the class path and the export...I also see it in the packages command:

org.apache.commons.lang3; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.builder; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.concurrent; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.event; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.exception; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.math; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.mutable; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.reflect; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.text; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.text.translate; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.time; version="0.0.0"<ATS_1.0.0.1 [14]>
org.apache.commons.lang3.tuple; version="0.0.0"<ATS_1.0.0.1 [14]>


I am confused as to why this doesn't work - it can be seen by the framework - my bundle just can't load it!!

Thanks.

[Updated on: Fri, 25 January 2013 00:19]

Report message to a moderator

Re: How to use external jar? [message #1005376 is a reply to message #1005358] Fri, 25 January 2013 02:47 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 335
Registered: December 2010
Location: Germany
Senior Member
Hi

I'm not sure what exactly is happening, but I see in your MANIFEST, that you are importing and exporting the same packages. That does not make sense, since you are either importing a package which is provided by another bundle OR you are exporting a package, so it can be imported by another bundle.

You are also defining the same packages as private (Privat-Package) and exportet (Export-Package). I have never used the header "Private-Package" and I don't see a reason for this, since all packages which are not exported are to be considered as private. Is it even part of the OSGi specification? I would try to remove "Private-Package" completely.

Hope this helps,
Christoph
Re: How to use external jar? [message #1005418 is a reply to message #1005358] Fri, 25 January 2013 06:23 Go to previous messageGo to next message
BJ Hargrave is currently offline BJ Hargrave
Messages: 55
Registered: July 2009
Member
You say external jars in the subject. Are the jar files part of your bundle? The Bundle-Classpath refers to them which means they need to be included in your bundle jar. Bundle-Classpath does not refer to anything external to the bundle. You refer to external packages with Import-Package (or, heaven forbid, Require-Bundle).
Re: How to use external jar? [message #1005419 is a reply to message #1005376] Fri, 25 January 2013 06:28 Go to previous messageGo to next message
BJ Hargrave is currently offline BJ Hargrave
Messages: 55
Registered: July 2009
Member
Christoph Keimel wrote on Fri, 25 January 2013 02:47
Hi

I'm not sure what exactly is happening, but I see in your MANIFEST, that you are importing and exporting the same packages. That does not make sense, since you are either importing a package which is provided by another bundle OR you are exporting a package, so it can be imported by another bundle.


This is an acceptable practice which allows for package substitution. See http://blog.osgi.org/2007/04/importance-of-exporting-nd-importing.html

Quote:

You are also defining the same packages as private (Privat-Package) and exportet (Export-Package). I have never used the header "Private-Package" and I don't see a reason for this, since all packages which are not exported are to be considered as private. Is it even part of the OSGi specification? I would try to remove "Private-Package" completely.


Private-Package is not an OSGi defined header. It is from the bnd tool and is non-harmful.
Re: How to use external jar? [message #1005467 is a reply to message #1005419] Fri, 25 January 2013 09:54 Go to previous messageGo to next message
kilosi mesdor is currently offline kilosi mesdor
Messages: 6
Registered: January 2013
Junior Member
Well, you guys were very helpful - it turns out I wasn't actually putting the jars in my main jar file. DOH! I was also able to clean up the manifest file to be better.

At least it was an easy fix Smile

I have ran into another issue, that I do not understand and would like to fix.

During Eclipse, it seems to find everything from the SDK just fine, for example, various classes from org.xml.sax. However, when I run the bundle outside of Eclipse, I run into issues where the bundle can't find it suddenly! Like this:
Exception in thread "Thread-11" java.lang.NoClassDefFoundError: org/xml/sax/helpers/DefaultHandler
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.defineClass(DefaultClassLoader.java:188)
        at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClass(ClasspathManager.java:580)
        at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findClassImpl(ClasspathManager.java:550)



So I have to import all of these specific packages on the manifest editor and then do a rebuild. But it seems like bnd could solve this issue for me, after looking at the top link. I'll check it out!

Thanks!

[Updated on: Fri, 25 January 2013 10:03]

Report message to a moderator

Re: How to use external jar? [message #1005502 is a reply to message #1005467] Fri, 25 January 2013 12:39 Go to previous messageGo to next message
BJ Hargrave is currently offline BJ Hargrave
Messages: 55
Registered: July 2009
Member
kilosi mesdor wrote on Fri, 25 January 2013 09:54
But it seems like bnd could solve this issue for me


It should. Do you have
Import-Package: *
in your bnd file? Then bnd should automatically generate all the imports you need.
Re: How to use external jar? [message #1005526 is a reply to message #1005502] Fri, 25 January 2013 20:55 Go to previous messageGo to next message
kilosi mesdor is currently offline kilosi mesdor
Messages: 6
Registered: January 2013
Junior Member
BJ Hargrave wrote on Fri, 25 January 2013 12:39
kilosi mesdor wrote on Fri, 25 January 2013 09:54
But it seems like bnd could solve this issue for me


It should. Do you have
Import-Package: *
in your bnd file? Then bnd should automatically generate all the imports you need.


I will give that a try!

So far, even though this is frustrating at times, OSGi is very cool.

Thanks!
Re: How to use external jar? [message #1005587 is a reply to message #1005526] Sat, 26 January 2013 14:58 Go to previous message
kilosi mesdor is currently offline kilosi mesdor
Messages: 6
Registered: January 2013
Junior Member
I am having major issues with bnd. I am going to post on their google groups for help.

I just wanted to say thanks a lot for your replies.

[Updated on: Sat, 26 January 2013 16:43]

Report message to a moderator

Previous Topic:Equinox Issue and Question
Next Topic:P2 update mechanism under Indigo
Goto Forum:
  


Current Time: Fri Aug 01 10:14:44 EDT 2014

Powered by FUDForum. Page generated in 0.02477 seconds