java Guice 2.0 中的 ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1266587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:52:04  来源:igfitidea点击:

ClassNotFoundException with Guice 2.0

javaguiceclassnotfoundexception

提问by Benno Richters

The code below generates an error using Guice 2.0. With Guice 1.0 everything is fine. The JDK is Java 6 update 15.

下面的代码使用 Guice 2.0 生成错误。使用 Guice 1.0 一切正常。JDK 是 Java 6 更新 15。

public class App {
    public static void main(String[] args) {
        Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                // just testing 
            }
        });
    }
}

The error is:

错误是:

Exception in thread "main" java.lang.NoClassDefFoundError: [Lorg/aopalliance/intercept/MethodInterceptor;
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
    at java.lang.Class.getDeclaredMethods(Class.java:1791)
    at com.google.inject.internal.ProviderMethodsModule.getProviderMethods(ProviderMethodsModule.java:78)
    at com.google.inject.internal.ProviderMethodsModule.configure(ProviderMethodsModule.java:70)
    at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
    at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:232)
    at com.google.inject.spi.Elements.getElements(Elements.java:101)
    at com.google.inject.InjectorShell$Builder.build(InjectorShell.java:135)
    at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:102)
    at com.google.inject.Guice.createInjector(Guice.java:92)
    at com.google.inject.Guice.createInjector(Guice.java:69)
    at com.google.inject.Guice.createInjector(Guice.java:59)
    at App.main(App.java:6)
Caused by: java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor
    at java.net.URLClassLoader.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    ... 14 more

What can be the problem?

可能是什么问题?

回答by Boris Pavlovi?

You have missed to include a dependency jarwith the class org.aopalliance.intercept.MethodInterceptorin the classpath.

您错过了在类路径中包含具有该类的依赖项 jarorg.aopalliance.intercept.MethodInterceptor

回答by Brad Cupit

as Boris Pavlovi?mentions in his answer, you're missing a jar. Specifically the aopalliance.jar file, which comes in the guice zip file

鲍里斯·帕夫洛维?在他的回答中提到,你缺少一个罐子。特别是 guice zip 文件中的 aopalliance.jar文件

Alternatively, you might try using guice-2.0-no_aop.jar, but I'm not sure if that will work.

或者,您可以尝试使用guice-2.0-no_aop.jar,但我不确定这是否可行。

Adding this file to the classpath depends on which tool you use to run your java code.

将此文件添加到类路径取决于您用于运行 Java 代码的工具。

  • If you are running java from the command line:
  • 如果您从命令行运行 java:
windows: java -cp aopalliance.jar;guice-2.0.jar;other_jars.jar YourMainClass
*nix:    java -cp aopalliance.jar:guice-2.0.jar:other_jars.jar YourMainClass
windows: java -cp aopalliance.jar;guice-2.0.jar;other_jars.jar YourMainClass
*nix:    java -cp aopalliance.jar:guice-2.0.jar:other_jars.jar YourMainClass
  • If you are running java from Eclipse, typically you'll have some type of lib/ directory. Put your jar there, then right-click on the jar -> Build Path -> Add to Build Path
  • 如果您从 Eclipse 运行 java,通常您会有某种类型的 lib/ 目录。把你的 jar 放在那里,然后右键单击 jar -> Build Path -> Add to Build Path

回答by Isaiah

I run into this problem yesterday, the dependency is managed by Maven, so the aopalliance.jar is in classpath with no doubt.

我昨天遇到了这个问题,依赖是由 Maven 管理的,所以 aopalliance.jar 毫无疑问在类路径中。

Update jetty from version 6.1.10 to 6.1.26 fixed the problem.

将 jetty 从版本 6.1.10 更新到 6.1.26 修复了该问题。

The real problem here is the container does not support aopalliance.jar. I hope that will help someone there desperately looking for a solution.

这里真正的问题是容器不支持 aopalliance.jar。我希望这会帮助那里拼命寻找解决方案的人。

回答by Leo Zhao

I run into this problem today. Actually the tomcat can not find the class in its lib. So just copy the aopalliance.jar into the lib folder in tomcat ,that's enough.

我今天遇到了这个问题。实际上,tomcat 在其库中找不到该类。所以只需将 aopalliance.jar 复制到 tomcat 中的 lib 文件夹中,就足够了。

回答by Yergalem

Adding the below jars works fine for me.
javax.inject.jar
guice-3.0-no_aop.jar

添加以下罐子对我来说很好用。
javax.inject.jar
guice-3.0-no_aop.jar