Question

Recently, I've been playing a little bit with depency injections in 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.

Here comes the requested Person class, as requested:

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.

Was it helpful?

Solution

I think you are missing Google collections, now known as Guava.

See Google Guice Wiki

JSR 330

Guice 4.0 requires JSR 330 on your classpath. This is the javax.inject.jar included in the guice download. com.google.inject.internal

Many things inside com.google.inject.internal changed and/or moved. This is especially true for repackaged Guava (formerly Google Collections), cglib, and asm classes. All these classes are now hidden from IDE auto-import suggestions and are in new locations. You will have to update your code if you relied on any of these classes.

OTHER TIPS

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?

It's a great tool for handling dependencies, used extensively in the java world. 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).

I tried to prepare a maven pom.xml file it should look something like this. 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top