在lenskit和jdk 10中找不到javax.annotation.PreDestroy类

时间:2018-08-29 15:03:13

标签: java gradle intellij-idea annotations classnotfoundexception

我正在使用lenskit,但收到此错误

Exception in thread "main" java.lang.NoClassDefFoundError: javax/annotation/PreDestroy
    at org.grouplens.grapht.LifecycleManager.registerComponent(LifecycleManager.java:52)
    at org.grouplens.grapht.reflect.internal.ClassInstantiator.instantiate(ClassInstantiator.java:140)
    at org.grouplens.grapht.Instantiators$ProviderInstantiator.instantiate(Instantiators.java:148)
    at org.grouplens.grapht.Instantiators$MemoizingInstantiator.instantiate(Instantiators.java:184)
    at org.lenskit.inject.NodeInstantiator$DefaultImpl.instantiate(NodeInstantiator.java:91)
    at org.lenskit.inject.NodeInstantiator.apply(NodeInstantiator.java:70)
    at org.lenskit.inject.NodeInstantiator$DefaultImpl.apply(NodeInstantiator.java:82)
    at org.lenskit.inject.InstantiatingNodeProcessor.processNode(InstantiatingNodeProcessor.java:54)
    at org.lenskit.inject.NodeProcessors.processNodes(NodeProcessors.java:92)
    at org.lenskit.inject.RecommenderInstantiator.replaceShareableNodes(RecommenderInstantiator.java:113)
    at org.lenskit.inject.RecommenderInstantiator.instantiate(RecommenderInstantiator.java:82)
    at org.lenskit.LenskitRecommenderEngineBuilder.build(LenskitRecommenderEngineBuilder.java:144)
    at org.lenskit.LenskitRecommenderEngineBuilder.build(LenskitRecommenderEngineBuilder.java:112)
    at org.lenskit.cli.util.RecommenderLoader.loadEngine(RecommenderLoader.java:87)
    at org.lenskit.cli.commands.GlobalRecommend.execute(GlobalRecommend.java:72)
    at org.lenskit.cli.Main.main(Main.java:73)
Caused by: java.lang.ClassNotFoundException: javax.annotation.PreDestroy
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
    ... 16 more

它表示未找到annotation.PreDestroy类。但是在我的外部库中,有一个java.xml.ws.annotation库,其中包含javax.annotation文件。

被称为的地方在这里。

    /**
 * Register a component with the lifecycle manager.  The component will be torn down when the lifecycle manager
 * is closed, using whatever teardown the lifecycle manager institutes.
 *
 * @param instance The component to register.
 */
public void registerComponent(Object instance) {
    if (instance == null) {
        return;
    }

    if (instance instanceof AutoCloseable) {
        actions.add(new CloseAction((AutoCloseable) instance));
    }
    for (Method m: MethodUtils.getMethodsListWithAnnotation(instance.getClass(), PreDestroy.class)) {
        actions.add(new PreDestroyAction(instance, m));
    }
}

我当前正在使用intellij,并且我尝试导入javax.annotation jar file,但是没有用。我也尝试过放置这段代码

dependencies {
compileOnly group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.0'
compile group: 'org.jetbrains', name: 'annotations', version: '15.0'

}

但是它仍然没有用。

1 个答案:

答案 0 :(得分:-1)

查看GitHub上的lenskit源,它在build,gradle中声明了sourceCompatibility = '1.7'。 将JDK降级为8或7应该可以解决问题。因为javax.annotations.PreDestroy似乎已在JDK 9及更高版本中删除/更新。

此外,由于上述项目的源兼容性,与更高版本的JDK可能还会存在其他兼容性问题。