Question

I have an OSGI bundle (awesome.test) that calls code from a jar file (testlibrary.jar). I have included the bundle as a Liberty feature (awesome.test.feature) and I have it installed on a WebSphere Application Server Liberty Profile V8.5.5. I also have an OSGI bundle (awesometest) that is a part of an OSGI application (awesometest.app) and it has an Activator class.

Here is a picture of my workspace setup

What I want to do is call methods in testlibrary.jar through methods in awesome.test, which includes testlibrary.jar in its build path. My awesome.test.feature is available to any applicaions running on my Liberty server. I want my applications to be able to use that feature to gain access to the functionality in testlibrary.jar through what I provide in awesome.test. I don't want OSGI applications to directly import packages from testlibrary.jar.

The following error appears in the binary logs when I run the application on the server:

CWWKZ0402E: A bundle exception was generated when trying to install the application awesometest.app into an OSGi framework. The error text from the OSGi framework is: Exception in awesometest.Activator.start() of bundle awesometest.

Debugging the problem finds that this exception is thrown:

java.lang.ClassNotFoundException: testlibrary.test.TestAPI

Source from TestLibraryRunner.java in awesome.test:

package awesome.test;

import testlibrary.test.TestAPI;

public class TestLibraryRunner { 

    public static void runNonLibTest() {
        System.out.println("No Library code is being called");
    }

    public static void runLibTest() {
        TestAPI ta = new TestAPI("This is a message.");
        ta.display();
    }
}

Note that runNonLibTest() will work when called from the OSGI application. Calling the TestAPI code in runLibTest from the OSGI application will cause the error above.

Source from Activator.java in awesometest:

public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
        System.out.println("Starting...");
        TestLibraryRunner.runNonLibTest();
        TestLibraryRunner.runLibTest();
        System.out.println("Finishing...");
    }

    public void stop(BundleContext context) throws Exception {
        System.out.println("Stopping...");
    }

}

Source from MANIFEST.MF in awesometest:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: awesometest
Bundle-SymbolicName: awesometest
Bundle-Version: 1.0.0
Bundle-Activator: awesometest.Activator
Import-Package: awesome.test,
 org.osgi.framework
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Export-Package: awesometest

In summary, my OSGI application cannot touch code from the jar in the build path of the bundle included in my Liberty feature. Is there something fundamental I'm missing here? Is what I'm trying to do even possible?

Thanks

Était-ce utile?

La solution 4

The problem with my project was that I was not including the testlibrary.jar correctly. While it was in the build path of awesome.test, it wasn't being included with the OSGI feature bundle.

There are two possible solutions:

1.) In Eclipse, go to File > Import and choose OSGi > Java Archive into an OSGi Bundle and create a new bundle. This will put the jar in its own bundle and then awesome.test can import the packages it needs from that new bundle.

2.) In Eclipse, go to File > Import and choose OSGi > Java Archive into an OSGi Bundle and include it in an existing bundle. This has the same effect as making the jar its own bundle, but it's less modular. The advantage of this approach is that you can not export the packages from the jar and just export your own interfaces.

There is also the BND Tool. It can help automate a lot of this process. I haven't used it myself.

Autres conseils

Having had the same runtime exceptions in my educational project, the simplest solution I have found is to add the used JARs under Runtime - Classpath of the imported bundle (in your case awesome.test).

I hope this late answer helps someone else.

You use the package testlibrary.test but you do not import it. You should add that package to the Import-Package statement of the bundle.

It looks like the problem is in the packaging of awesome.test. The awesometest bundle (application bundle) can find your feature code fine, but then problems occur in the feature bundle. Have you confirmed that testlibrary.jar is packaged within awesome.test (feature bundle) and that the manifest of awesome.test includes the embedded jar?

You'll also need to list your exported feature packages in your feature manifest using the IBM-API-Package header, but I assume you've already done that or your application bundle activator wouldn't be able to see the feature bundle's TestLibraryRunner.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top