Search code examples
javaosgiapache-felix

package does not exsist in Apache Felix OSGI


I have been trying out some examples with apache felix and osgi. I made service (Service provider) interface and implemented it. After that I manage to create a jar file with the relevant information provided through a manifest file. Next I need to crate a jar file for the consumer part. But when ever I try to compile the consumer part it gives an error as package does not exist . I need to import the interface to the consumer (Service consumer).

This is my code (Service Consumer's Activator.java)

package mtitassignmentone.serviceconsumer;

import java.util.Scanner;
import java.util.StringTokenizer;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

**import mtitassignmentone.serviceprovider.service.BookService;**

public class Activator implements BundleActivator {
    private BundleContext m_context = null;
    private ServiceTracker m_tracker = null;

    public void start(BundleContext context) throws Exception {
        m_context = context;

        // Create a service tracker to monitor dictionary services.
        m_tracker = new ServiceTracker(m_context, m_context.createFilter(BookService.class.getName()), null);
        m_tracker.open();

        BookService book= (BookService) m_tracker.getService();
            book.getName();

    }

    public void stop(BundleContext context) {
    }
}

import mtitassignmentone.serviceprovider.service.BookService; it the error that throws when compiling. but that file exist. How to overcome this?


Solution

  • It seems as if your Activator.java files has some issue wit the import of the package. Sometimes this happens when you don't keep a blank line s\pace at the end of you manifest file that you use to create the service provider.

    Another reason is an issue with the packages you have created(obviously :D). This issue can be resolved by creating packages from an IDE like eclipse. or else you can do it from cmd for eg:

    From the project's root directory:

    javac src/com/osgi/services/*.java
    

    To run, assuming no other dependencies:

    java -cp ./src com.osgi.services.MyService
    

    (Assuming MyService has the normal main function.)

    The javac command compiles all the .java files in the package's directory. Since they're all in the same package/directory, this works. It also puts the generated .class files in the same directory, which may or may not be what you want.

    To put them in a different directory, use the -d option and supply a path.

    javac -d bin src/com/osgi/services/*.java
    

    Then to run:

    java -cp ./bin com.osgi.services.MyService