larry-fuy
1/7/2015 - 5:45 PM

Run dagger example

Run dagger example

Run the dagger example

Maven

To run simple example, first build the project by Maven

mvn compile

then try to run the example by

java -cp target/classes/ coffee.CoffeeApp

The errors are

Exception in thread "main" java.lang.NoClassDefFoundError: dagger/Factory

What is the error?

The NoClassDefFoundError is a common error in Java. The main reason is the JVM class loader cannot find the correct class. See how to for this error. dagger/Factory seems a method/class in Dagger library. Let us first see where is the library. Actually the dagger library in the directory of Maven default directory for third-party package. But when running the application, the class path does not include the path.

Fix

There are two solutions. First, we can add the necessary path in the command line options. However there is another way if you use Maven. See run java main in Maven. Using this method, we can run the application like this:

mvn exec:java -Dexec.mainClass="coffee.CoffeeApp"

Now it works.

Eclipse (Luna)

Import the project

File->import->Maven->Existing Maven Project

Update compiler

If the java compiler used in Eclipse is 1.5 or lower, there should be annoying error which shows "must override the superclass". The solution is to update java compiler >= 1.6. Project->Java Compiler. Mark off Use compliance from execution environment 1.5... and choose newer version.

Install m2e connector to apt

  1. Windows->Preference->Maven->Discovery and then Open Catalog to choose m2e-apt.
  2. Project->Java Compiler->Annotation Processing to enable project specific setting
  3. Restart Eclipse and update project (right click the project and choose Maven->Update project