peveuve
peveuve

Reputation: 770

How to compile Eclipse plugin project referring extra system packages from underlying webapp?

I am working on a webapp where I start the osgi framework in a servlet context listener (Eclipse Equinox version 3.10.0). The webapp runs in Tomcat 8 with Java 7. I have developed and installed 2 simple bundles working well as long as they use their internal classes. But when I try to import webapp packages into the bundles with the system.package.extra config, bundles are unable to resolve these packages:

java.lang.Error: Unresolved compilation problems: 
    The import xxx.xxxxxxxx cannot be resolved

The framework declares the extra system packages this way:

public void contextInitialized(ServletContextEvent cntxt) {
Iterator<FrameworkFactory> frameworkFactoryIt = ServiceLoader.load(FrameworkFactory.class).iterator();
boolean frameworkStarted = false;
while (frameworkFactoryIt.hasNext() && !frameworkStarted) {
  FrameworkFactory frameworkFactory = frameworkFactoryIt.next();
  Map<String, String> config = new HashMap<String, String>();
  config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "xxx.xxxxxxxx,...");
  config.put(Constants.FRAMEWORK_STORAGE, "/users/peveuve/osgi-cache");
  config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
  framework = frameworkFactory.newFramework(config);
  framework.start();
  ....

The bundle MANIFEST looks like that:

Import-Package: org.osgi.framework;version="1.3.0",
 xxx.xxxxxxxx;resolution:=optional,

The imported package is made optional to avoid error in the Eclipse plug-in project. I don't use versioning.

When listing the framework headers, I noticed there is a space character between the system packages and my extra system packages in the Export-Package:

...,org.xml.sax.helpers, xxx.xxxxxxxx,...

Since it is added by the framework itself, I suppose it is not a problem, but who knows.

Here is the full stacktrace:

!SESSION 2016-02-26 16:07:35.175 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.7.0_79
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=fr_FR

!ENTRY com.amfinesoft.kheops.service.user 4 0 2016-02-26 16:07:35.176
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.ServiceException: Exception in com.amfinesoft.kheops.service.user.UserServiceFactory.getService()
    at org.eclipse.osgi.internal.serviceregistry.ServiceFactoryUse.factoryGetService(ServiceFactoryUse.java:222)
    at org.eclipse.osgi.internal.serviceregistry.ServiceFactoryUse.getService(ServiceFactoryUse.java:111)
    at org.eclipse.osgi.internal.serviceregistry.ServiceConsumer$2.getService(ServiceConsumer.java:45)
    at org.eclipse.osgi.internal.serviceregistry.ServiceRegistrationImpl.getService(ServiceRegistrationImpl.java:496)
    at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.getService(ServiceRegistry.java:461)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.getService(BundleContextImpl.java:619)
    at com.amfinesoft.kheops.consumer.user.UserConsumerActivator.start(UserConsumerActivator.java:24)
    at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:771)
    at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:764)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:721)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:936)
    at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:319)
    at org.eclipse.osgi.container.Module.doStart(Module.java:571)
    at org.eclipse.osgi.container.Module.start(Module.java:439)
    at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:454)
    at org.eclipse.osgi.container.ModuleContainer.applyDelta(ModuleContainer.java:717)
    at org.eclipse.osgi.container.ModuleContainer.resolveAndApply(ModuleContainer.java:491)
    at org.eclipse.osgi.container.ModuleContainer.resolve(ModuleContainer.java:437)
    at org.eclipse.osgi.container.ModuleContainer.resolve(ModuleContainer.java:427)
    at org.eclipse.osgi.container.Module.start(Module.java:416)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
    at org.eclipse.osgi.container.SystemModule.startWorker(SystemModule.java:242)
    at org.eclipse.osgi.container.Module.doStart(Module.java:571)
    at org.eclipse.osgi.container.Module.start(Module.java:439)
    at org.eclipse.osgi.container.SystemModule.start(SystemModule.java:172)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:393)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:412)
    at org.eclipse.osgi.launch.Equinox.start(Equinox.java:115)
    at com.amfinesoft.kheops.service.starter.OSGIServletContextListener.contextInitialized(OSGIServletContextListener.java:60)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.Error: Unresolved compilation problems: 
    The import com.eclipsesource cannot be resolved
    The import com.eclipsesource cannot be resolved
    The import kheops cannot be resolved
    The import kheops cannot be resolved
    The import kheops cannot be resolved
    The import system cannot be resolved
    The type UserServiceImpl must implement the inherited abstract method UserServiceIntf.listOfUsers(KCommand, KBrowser)
    KCommand cannot be resolved to a type
    KCommand cannot be resolved to a type
    KBrowser cannot be resolved to a type
    JsonArray cannot be resolved to a type
    JsonArray cannot be resolved to a type
    The method getTrees() is undefined for the type DBUserFinder
    The method getTrees() is undefined for the type DBUserContainer
    JsonValue cannot be resolved
    Logger cannot be resolved
    KCommandProcessor cannot be resolved

    at com.amfinesoft.kheops.service.user.impl.UserServiceImpl.<init>(UserServiceImpl.java:7)
    at com.amfinesoft.kheops.service.user.UserServiceFactory.getService(UserServiceFactory.java:14)
    at com.amfinesoft.kheops.service.user.UserServiceFactory.getService(UserServiceFactory.java:1)
    at org.eclipse.osgi.internal.serviceregistry.ServiceFactoryUse$1.run(ServiceFactoryUse.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.internal.serviceregistry.ServiceFactoryUse.factoryGetService(ServiceFactoryUse.java:210)
    ... 41 more

Any idea why such an error occurs?

How do you compile an Eclipse plugin project when it refers extra system packages from the underlying web application?

Upvotes: 0

Views: 225

Answers (1)

Neil Bartlett
Neil Bartlett

Reputation: 23958

This is not an OSGi error at all. It is a result of deploying code that has not compiled correctly.

Eclipse uses its own compiler called ECJ, rather than javac from the JDK, which behaves differently when it encounters errors. Whereas javac will immediately stop and not produce any output, ECJ does produce class files as output. However if you try to run those classes at all, you will get the message you saw:

java.lang.Error: Unresolved compilation problems: 
    The import xxx.xxxxxxxx cannot be resolved

The message about "import cannot be resolved" is from the Java compiler. If you look at the source code in your IDE you should see the red X markers there.

Basically, you must ensure your code is actually compiling without errors before trying to run it.

Upvotes: 1

Related Questions