2

Let me start by pointing out that while I've been using Java SE for a while now this is my first foray into Java EE territory. I'm using Netbeans 6.9 and the Netbeans code generator to do most of the heavy lifting (more on this further down). The version of GlassFish is 3 - the bog standard one that ships when you download Netbeans.

I have created a stateless Session Bean to return a simple String as follows:

@Stateless
public class SDBSStatelessSessionBean implements SDBSStatelessSessionBeanRemote {

    @Override
    public String sayHello() {
        return "This seems to be working just fine.";
    }     
}

with the interface definition as:

@Remote
public interface SDBSStatelessSessionBeanRemote {

   String sayHello();
}

The class and the interface were both created by using the 'Insert Code' feature that Netbeans provides. I figure this way I avoid making any stupid newbie errors (oh the irony).

My problem is that when I try to call the Enterprise bean from a servlet (the call was added using the 'Call Enterprise Bean' option from the Netbeans code generator) I get the following error:

javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB

the exception was caused by: NoClassDefFoundError

This is how the servlet makes the call:

@EJB
private SDBSStatelessSessionBeanRemote sDBSStatelessSessionBean;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Test Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<p>" + sDBSStatelessSessionBean.sayHello()  +"</p>");
        out.println("</body>");
        out.println("</html>");
    } catch (Exception e) {
        out.println("<p>" + e.getMessage() + "</p>");
        out.println("</body>");
        out.println("</html>");
    } finally {
        out.close();
    }
}

I'm afraid that I have not been able to find a solution to this problem after extensive Googling (mostly because the few forum posts that seem to come near this problem contain too much jargon for me to follow the solution).

I'd greatly appreciate any advice/help pointing me in the right direction.

Insectatorious
  • 1,305
  • 3
  • 14
  • 29
  • There should be a "Caused by" for that exception that shows the reason that the EJB could not be created. Please include that. – Brett Kail Apr 30 '11 at 05:01
  • Thanks for pointing that out. I've made the edit above. – Insectatorious May 02 '11 at 06:30
  • For future questions, it's probably best to include the full stack trace (or at least the full "Caused by" line); the message of a NoClassDefFoundError is significant. – Brett Kail May 02 '11 at 15:16

1 Answers1

3

If this is a local EJB (in the same JVM / EE container as your servlet) you could try declaring your EJB as a @LocalBean (under the @Stateless annotation). You could also remove the @Remote interface (and make your EJB no loger implement it).

So your EJB would become

@Stateless
@LocalBean
public class SDBSStatelessSessionBean
{
    public String sayHello() 
    {
        return "This seems to be working just fine.";
    }     
}
Jon
  • 365
  • 2
  • 13
  • Thanks! I made the changes and it worked. I had to restart the project from scratch in netbeans and for some reason it worked this time round. – Insectatorious May 02 '11 at 06:29
  • 1
    No problem, using EJB's was simplified with EE6 (thankfully!) – Jon May 02 '11 at 12:08