简体   繁体   English

Eclipse上的Java构建路径错误

[英]Java build path errors on Eclipse

Recently, I've been playing a little bit with depency injections in Java. 最近,我一直在使用Java进行依赖注入。 I'm a complete newbie in this field, and I don't really get, why in this simple example I keep receiving an error. 我是该领域的一个新手,但我真的不明白,为什么在这个简单的示例中我总是收到错误消息。

package michal.dependency;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Injector injector = Guice.createInjector(new ProjectModule());
        Person person = injector.getInstance(Person.class);
        person.greetFriend();
    }

}

The error message I receive is as follows: 我收到的错误消息如下:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList
at com.google.inject.internal.Errors.<clinit>(Errors.java:656)
at com.google.inject.internal.InternalInjectorCreator.<init>(InternalInjectorCreator.java:62)
at com.google.inject.Guice.createInjector(Guice.java:96)
at com.google.inject.Guice.createInjector(Guice.java:73)
at com.google.inject.Guice.createInjector(Guice.java:62)
at michal.dependency.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableList
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more

I'm pretty sure by the way the necessary .jar file is included in the classpath. 我很确定在类路径中包含了必需的.jar文件。

Here comes the requested Person class, as requested: 以下是请求的Person类,如下所示:

package michal.dependency;

import com.google.inject.Inject;

public class Person {

    private MessageService messageService;

    @Inject
    public Person (MessageService messageService)
    {
        this.messageService = messageService;
    }


    public void greetFriend ()
    {
        messageService.sendMessage("Hey!", "How are you?");
    }

}

Thanks in advance. 提前致谢。

I think you are missing Google collections , now known as Guava . 我认为您正在丢失现在称为Guava的 Google收藏

See Google Guice Wiki 参见Google Guice Wiki

JSR 330 JSR 330

Guice 4.0 requires JSR 330 on your classpath. Guice 4.0在您的类路径上需要JSR 330。 This is the javax.inject.jar included in the guice download. 这是guice下载中包含的javax.inject.jar com.google.inject.internal com.google.inject.internal

Many things inside c om.google.inject.internal changed and/or moved. c om.google.inject.internal内部的许多内容om.google.inject.internal更改和/或移动。 This is especially true for repackaged Guava (formerly Google Collections), cglib, and asm classes. 对于重新打包的Guava(以前称为Google Collections),cglib和asm类,尤其如此。 All these classes are now hidden from IDE auto-import suggestions and are in new locations. 现在,所有这些类都已从IDE自动导入建议中隐藏,并位于新位置。 You will have to update your code if you relied on any of these classes. 如果您依赖这些类中的任何一个,则必须更新代码。

As other have suggested, it seems that something is missing from your classpath. 正如其他人所建议的那样,您的类路径似乎缺少了一些东西。 Maybe you could try using some sort of dependency management tool, for example Apache Maven ? 也许您可以尝试使用某种依赖性管理工具,例如Apache Maven

It's a great tool for handling dependencies, used extensively in the java world. 它是处理依赖关系的好工具,在Java世界中广泛使用。 Depending on your IDE you will have lots of supportfor using it (my personal favourite is Intellij Idea with really great maven support, though Netbeans also does it pretty well). 根据您的IDE,您将获得使用它的大量支持(我个人最喜欢的是Intellij Idea,它具有非常强大的maven支持,尽管Netbeans也做得很好)。

I tried to prepare a maven pom.xml file it should look something like this. 我试图准备一个Maven pom.xml文件,它看起来应该像这样。 I tested the project with this and there are no compilation errors: 我对此进行了测试,并且没有编译错误:

  <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>test-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>3.0</version>
    </dependency>
</dependencies>


</project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM