tags:

views:

85

answers:

2

My controller (abstract controller) is calling a service class to get a URL. In my service class I instantiate my dependency in the constructor:

the abstract controller:

public class MyPageController extends AbstractController{
private UserService userService;
private LearnerService learnerService;
private TwitterService twitterService;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
  HttpServletResponse response) throws Exception {
 userService = UserServiceFactory.getUserService();
 learnerService = new LearnerService();
 twitterService=new TwitterService();

the Service class:

public TwitterService(){
 //
 twitter = new Twitter();
}

but when I try to run my web app, spring throws the following error:

nested exception is java.lang.NoClassDefFoundError: twitter4j/Twitter:
    java.lang.NoClassDefFoundError: twitter4j/Twitter
    at com.hardwire.service.TwitterService.<init>(Twitter Service.java:21)
    at com.hardwire.controller.
    MyPageController.handleReq uestInternal(MyPageController.java:30)

Why does it throw this error? I don't understand. I imported the Twitter class in the service class that uses it.

+1  A: 

You get a NoClassDefFoundError if the ClassLoader instance tries to load in the definition of a class and no definition of the class could be found.

This basically means the class is not found on the classpath at runtime. Having an import declaration in your code is required but not sufficient to make it work, you also need to ensure the jar containing twitterj.Twitter is on the application classpath.

The simplest way to do this is to copy the jar into the WEB-INF/lib folder of the application. Or use a tool (like Maven, Ivy, Ant or even an IDE like Eclipse) to ensure the required jars are packaged into your war.

Rich Seller
Thanks for the reply. I copied the library already to the web-inf/lib folder. Now it works! This changes some assumptions I've had in the past. I thought it was just okay to have an import statement. Turns out that the dev and production environment have different classpaths! (Did I get that right?)
Jeune
yes the classpath of any given environment can be different for a number of reasons. A Java webapp will have access to the contents of WEB-INF/lib, any other classpath elements are subject to change
Rich Seller
A: 

Do you have the twitter4j jar in your WEB-INF/lib directory?

David Rabinowitz