2

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.

5
  • 4
    even though I'm sure the necessary .jar file is included in the classpath? while compiling and running your app? Take a look at stackoverflow.com/questions/1457863/…. Commented Apr 29, 2014 at 19:31
  • 2
    If you are absolutely sure, it's clear that the compiler is wrong. Try to convince it. Commented Apr 29, 2014 at 19:31
  • It appears as a referenced library, and it's on the top of Order and Export in project properties. Not really sure how else could I check it ;) Commented Apr 29, 2014 at 19:33
  • 1
    which version of Guice you are using ?? Commented Apr 29, 2014 at 19:52
  • I'm trying to get out of the question ban. This question has 2 downvotes, which were never lifted. What else can I do to improve this question? Commented Dec 13, 2014 at 10:44

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Nope, added guava to the path as well, and the compiler still wont
You should try to provide more info than "I did it and it did not work".
@ Michał Szydłowski Post Your Person class with import statements . this com.google.common.collect.ImmutableList is available in guava
@Michał Szydłowski after adding guava, what exception you are getting
oh you are using guice with AOP
|
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.