Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classes of referenced EJB project not found in (the referencing) Web project

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:

  1. An EJB 3.0 compliant project, myEjbProject
  2. 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 myEjbProject explicitly to the Java Build Path -> Libraries list of myWebProject. 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 myEjbProject using 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 of myWebProject. 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 myBean

    This approach did not solve the problem either (same error message as before).

  • doing away with myEjbProject altogether and adding the session bean to myWebProject as myServletPackage.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 myEjbProject altogether and adding the session bean to myWebProject as mySessionBeanPackage.mySessionBean (a new package). In this case also, I can inject the session bean successfully into the servlet using

    @EJB mySessionBeanPackage.mySessionBean myBean

like image 828
ars-longa-vita-brevis Avatar asked Sep 29 '11 00:09

ars-longa-vita-brevis


1 Answers

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:

  1. EJB and WEB projects are modules of the EAR project. Right click the EAR project -> properties -> Deployment Assembly -> Add -> Project -> select the EJB and the WEB project. If you export your EAR project now you will notice that the EJB and the WEB project is packaged with the EAR.
  2. Add EJB project to MANIFEST of WEB project. Right click the WEB project -> properties -> Deployment Assembly -> Manifest entries -> Add -> select the EJB project. You can open the MANIFEST.MF file and see that it now includes the EJB project.

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.

like image 172
Ross Avatar answered Oct 08 '22 02:10

Ross