I need to pass some parameters from felix launcher class (Main) to bundle and I followed this advice. What I did
Launcher project
test.launcher package contains:Main.java,Temp.java,TempI.java
Bundle project
test.bundle package contains:Activator.java
test.launcher package contains:TempI.java
As you can see I copied TempI.java to bundle project as launcher is not bundle and can't export its packages.
In Main
BundleContext context = felix.getBundleContext();
ServiceRegistration serviceRegistration=context.registerService(TempI.class.getName(), new Temp(), null);
In Activator
ServiceReference serviceReference = context.getServiceReference(TempI.class.getName());
TempI service = (TempI) context.getService(serviceReference);
System.out.println(service.testService());
but I get in Activator
java.lang.NullPointerException: Specified service reference cannot be null.
What is the problem? Again different classloaders? Or what?
You state that both the launcher and the bundle contain their own copies of the TempI class. One is loaded by the classloader of the launcher and the other by the classloader of the bundle. To the VM and therefore the OSGi service registry, these are different Class objects. So when you bundle looks for a TempI service which is of the TempI class known to your bundle, it finds none.
You need to not put TempI in your bundle and instead import the package. You also need to configure the framework to export the package using org.osgi.framework.system.packages.extra. Then you bundle will import the package from the system bundle and the service registered in the launcher will be visible to the bundle.