0

I'm still new to Java EE 7 / Spring and trying to solve the following issue. I use Glassfish 4.1 and JDK 1.8.

I have created a test project in Eclipse (New Project -> Spring Project -> Simple Java) using the project facets "Dynamic Web Module", "Java", "JavaScript", "JavaServer Faces" as follows:

index.xhtml

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">

<h:form>
    <h:commandButton value="Submit Query" action="#{bean.login()}"></h:commandButton>
</h:form>
</html>

Bean.java

package springWeb;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


@RequestScoped
@Named
public class Bean {

    //@Autowired
    private UserService userService;

    public String login(){
        AnnotationConfigApplicationContext context = 
                new AnnotationConfigApplicationContext(springConfiguration.class);
//      userService = (UserService)context.getBean("UserService");
//      userService.service();
        return "go.jsf";
    }
}

UserServiceImpl

package springWeb;


import org.springframework.stereotype.Component;

//@Component
public class UserServiceImpl implements UserService {

    @Override
    public void service() {
        System.out.println ("Service running...");
    }

}

When pressing the Command-Button, I receive the following error:

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/springframework/context/annotation/AnnotationConfigApplicationContext

root cause javax.faces.el.EvaluationException: java.lang.NoClassDefFoundError: org/springframework/context/annotation/AnnotationConfigApplicationContext

root cause java.lang.NoClassDefFoundError: org/springframework/context/annotation/AnnotationConfigApplicationContext

root cause java.lang.ClassNotFoundException: org.springframework.context.annotation.AnnotationConfigApplicationContext

What is the reason for this and how to solve it? (For reasons of clarity, I have commented out the dependency injection.)

Thank you very much for your help!

mike128
  • 609
  • 1
  • 5
  • 8
  • Are you sure you're deploying the .war file produced as the result of `mvn package`? It's not at all clear what steps you've performed, some statements suggest overall confusion (*"(...) Glassfish server does not have access to the libraries that are in the maven repository"*) – kryger Aug 04 '15 at 15:17
  • For reasons of clarity, I have re-structured my question above – mike128 Aug 10 '15 at 08:08

1 Answers1

0

Finally I was able to solve the problem by myself. The reason was that class files which my code depended on and that had been present at compile time have not been found at runtime. Therefore, I had to copy seven missing libraries (such as spring-context-4.2.0.RELEASE.jar, spring-expression-4.2.0.RELEASE.jar, spring-core-4.2.0.RELEASE.jar, spring-context-support-4.2.0.RELEASE.jar etc.) to the following Glassfish libary-folder

C:\glassfish-4.1\glassfish4\glassfish\domains\domainXXXX\lib

The missing libaries you can easily find online e.g. in the Maven repository: http://repo1.maven.org/maven2/

Related posts that helped me understanding this were: Why am I getting a NoClassDefFoundError in Java? and Maven vs. Eclipse Project Facets for Java EE 6

Community
  • 1
  • 1
mike128
  • 609
  • 1
  • 5
  • 8
  • You should bundle your application's dependencies directly into the webapp artifact (i.e. the .war file), not require the container to provide them for you. Using Maven properly would make it easier for you, you should spend some time researching - this investment will pay off in the future. – kryger Aug 12 '15 at 13:14