I’m learning J2EE and EJB and I’ve run into a strange problem. This is my project setup:
I’m using Glassfish 3.1 and Eclipse 3.7. I have two projects in Eclipse:
- An EJB 3.0 compliant project,
myEjbProject - A Dynamic Web Project,
myWebProject
I’m injecting an EJB session bean mySessionBeanPackage.mySessionBean from myEjbProject into a servlet myServletPackage.myServlet in myWebProject and then (in the servlet) calling a simple method defined in the session bean.
I’ve provided myWebProject with a reference to myEjbProject (using the Properties -> Project References dialog). However, for some reason, the server is unable to find the session bean. The exact error I get is:
SEVERE: Class [ LmySessionBeanPackage/mySessionBean ] not found. Error while loading [ class myServletPackage.myServlet ]
Note that both the EJB project and the web project have been published to the server and I’ve double-checked that the class file generated for the bean is, in fact, present on the server (so I can’t think of a reason why the server isn’t able to find them).
Can anyone tell me what I’m doing wrong?
I’ve also tried
-
adding the classes folder of
myEjbProjectexplicitly to the Java Build Path -> Libraries list ofmyWebProject. This is in addition to the project reference (although I think adding a project reference should have sufficed in the first place). Anyway, it makes no difference (same error message as before). -
creating a JAR file from
myEjbProjectusing the Export -> EJB Jar File option (I don’t want to get into writing an Ant script at the moment). I then put this JAR in the Java Build Path -> Libraries section ofmyWebProject. This approach does not solve the problem either (same error message as before). -
modifying the session bean to have
- no interface
- a local interface
- a remote interface
and modifying the injection accordingly. E.g., for a bean with only a local interface, I used:
@EJB mySessionBeanPackage.mySessionBeanLocal myBeanThis approach did not solve the problem either (same error message as before).
-
doing away with
myEjbProjectaltogether and adding the session bean tomyWebProjectasmyServletPackage.mySessionBean(same package as the servlet). In this case, I can inject the session bean successfully into the servlet using@EJB mySessionBeanPackage.mySessionBean myBean -
doing away with
myEjbProjectaltogether and adding the session bean tomyWebProjectasmySessionBeanPackage.mySessionBean(a new package). In this case also, I can inject the session bean successfully into the servlet using@EJB mySessionBeanPackage.mySessionBean myBean
I would recommend taking a look at the actual EAR that Eclipse packages and deploys to the server. You can do this by exporting the EAR project (right click on EAR project -> export -> Java EE -> EAR file)
I think the step you are missing is to add the EJB project to the WEB project’s MANIFEST file. But just in case you are missing a few more steps I would check the following:
Also I like adding the EJB project to the WEB project build path just so that it is included in the WEB’s classpath: Right click WEB project -> properties -> Java Build Path -> Projects -> Add -> Select the EJB.