osgimybatisguicejava-11apache-karaf

java.lang.ClassNotFoundException: com.google.inject.AbstractModule not found by org.mybatis.mybatis-guice


There is a problem that the bundle using mybatis-guice library using the felix framework fails to start. The running environment is java11 and karaf 4.2.14 versions.

This is the corresponding error message.


Error when instantiating bean daoService of class myApp.dao.inject.impl.ServiceImpl
org.osgi.service.blueprint.container.ComponentDefinitionException: Error when instantiating bean daoService of class myApp.dao.inject.impl.ServiceImpl
            at org.apache.aries.blueprint.container.BeanRecipe.wrapAsCompDefEx(BeanRecipe.java:362)
            at org.apache.aries.blueprint.container.BeanRecipe.getInstanceFromType(BeanRecipe.java:352)
            at org.apache.aries.blueprint.container.BeanRecipe.getInstance(BeanRecipe.java:283)
            at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:685)
            at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:666)
            at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:81)
            at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
            at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:90)
            at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:360)
            at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:190)
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:737)
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:433)
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:298)
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:335)
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:288)
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:284)
            at org.apache.aries.blueprint.container.BlueprintExtender.modifiedBundle(BlueprintExtender.java:274)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:500)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:433)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$AbstractTracked.track(BundleHookBundleTracker.java:725)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.bundleChanged(BundleHookBundleTracker.java:463)
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$BundleEventHook.event(BundleHookBundleTracker.java:422)
            at org.apache.felix.framework.util.SecureAction.invokeBundleEventHook(SecureAction.java:1179)
            at org.apache.felix.framework.EventDispatcher.createWhitelistFromHooks(EventDispatcher.java:730)
            at org.apache.felix.framework.EventDispatcher.fireBundleEvent(EventDispatcher.java:485)
            at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4579)
            at org.apache.felix.framework.Felix.startBundle(Felix.java:2174)
            at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
            at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)
            at org.apache.karaf.features.internal.service.BundleInstallSupportImpl.startBundle(BundleInstallSupportImpl.java:165)
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:1154)
            at org.apache.karaf.features.internal.service.Deployer.deploy(Deployer.java:1044)
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.doProvision(FeaturesServiceImpl.java:1063)
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.lambda$doProvisionInThread$13(FeaturesServiceImpl.java:998)
            at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
            at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: java.lang.NoClassDefFoundError: com/google/inject/AbstractModule
            at java.base/java.lang.ClassLoader.defineClass1(Native Method)
            at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2410)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2194)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1607)
            at org.apache.felix.framework.BundleWiringImpl.access$200(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at java.base/java.lang.ClassLoader.defineClass1(Native Method)
            at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2410)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2194)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1607)
            at org.apache.felix.framework.BundleWiringImpl.access$200(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(BundleWiringImpl.java:1414)
            at org.apache.felix.framework.BundleWiringImpl.searchImports(BundleWiringImpl.java:1660)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1590)
            at org.apache.felix.framework.BundleWiringImpl.access$200(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at java.base/java.lang.ClassLoader.defineClass1(Native Method)
            at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2410)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2194)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1607)
            at org.apache.felix.framework.BundleWiringImpl.access$200(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            at myApp.dao.inject.config.DaoModuleBindConfig$GameModule.configure(DaoModuleBindConfig.java:27)
            at com.google.inject.PrivateModule.configure(PrivateModule.java:102)
            at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:409)
            at com.google.inject.spi.Elements.getElements(Elements.java:108)
            at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:160)
            at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
            at com.google.inject.Guice.createInjector(Guice.java:87)
            at com.google.inject.Guice.createInjector(Guice.java:69)
            at com.google.inject.Guice.createInjector(Guice.java:59)
            at myApp.dao.inject.impl.ServiceImple.<init>(ServiceImple.java:17)
            at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
            at org.apache.aries.blueprint.utils.ReflectionUtils.newInstance(ReflectionUtils.java:369)
            at org.apache.aries.blueprint.container.BeanRecipe.newInstance(BeanRecipe.java:839)
            at org.apache.aries.blueprint.container.BeanRecipe.getInstanceFromType(BeanRecipe.java:350)
            \... 36 more
    Caused by: java.lang.ClassNotFoundException: com.google.inject.AbstractModule not found by org.mybatis.mybatis-guice [160]
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1639)
            at org.apache.felix.framework.BundleWiringImpl.access$200(BundleWiringImpl.java:80)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2053)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
            ... 83 more

The following is a list of the dependencies and import packages used by the bundle.

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-guice</artifactId>
    </dependency>
    
    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
            <instructions>
                <Import-Package>
                    !junit.framework*,
                    !org.junit*,
                    !org.mockito*,
                    !org.checkerframework.*,
                    org.apache.ibatis.builder.xml,
                    com.microsoft.sqlserver.jdbc,
                    *;
                </Import-Package>
                <Embed-Dependency/>
                <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                <DynamicImport>*</DynamicImport>
                <Export-Package>myApp.dao.*;version=${project.version};-noimport:=true
                </Export-Package>
                <!--suppress MavenModelInspection -->
                <_versionpolicy>[$(version;==;$(@)),$(version;+;$(@)))</_versionpolicy>
                <effective>effective:=active</effective>
            </instructions>
        </configuration>
    </plugin>

The version information for dependencies is defined in parent POM.

also added bootdelegation according to the guice osgi wiki

set JAVA_OPTS=-XX:+UnlockDiagnosticVMOptions --add-modules java.se --add-exports java.base/jdk.internal.ref=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.management/sun.management=ALL-UNNAMED --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED -Dhazelcast.diagnostics.enabled=true -Dorg.osgi.framework.bootdelegation=org.aopalliance.*,com.google.inject.*

Written in karaf.bat file.

Just in case, I specified the package in the bootdelegation in the karaf config.property accoding to the karaf container 4.x - 4.14.7. Security providers

org.osgi.framework.bootdelegation =
com.sun.*,
javax.transaction,
javax.transaction.xa,
javax.xml.crypto,
javax.xml.crypto.*,
jdk.nashorn.*,
sun.,
jdk.internal.reflect,
jdk.internal.reflect.
,
org.apache.karaf.jaas.boot.principal,
org.apache.karaf.jaas.boot,
org.aopalliance,
org.aopalliance.*,
com.google.inject,
com.google.inject.*,
com.google.common,
com.google.common.*,
org.mybatis.guice,
org.mybatis.guice.*

Nevertheless, the classloader still says the class cannot be found.

ID State Lvl Version Name
60 Active 80 5.1.0 guice
159 Active 80 3.5.9 mybatis
160 Active 80 3.17.0 mybatis-guice

It is confirmed that the guice, mybatis, and mybatis-guice are also active.

What else can i try?

Thank you in advance for your reply.


Solution

  • This part of the error message:

    com.google.inject.AbstractModule not found by org.mybatis.mybatis-guice
    

    Suggests that the org.mybatis.mybatis-guice bundle should import package com.google.inject but fails to do so. As such, all the mitigations you have added to your own bundle such as DynamicImport (which isn't a valid OSGi header anyway) will not do anything because it is the third-party bundle which is at fault.