EJB Stateless Session Bean - could not create error

Insectatorious picture Insectatorious · Apr 30, 2011 · Viewed 12.3k times · Source

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.

Answer

Jon picture Jon · May 1, 2011

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.";
    }     
}