Can't put EJB in separate jar

I have an EJB with some JPA logic that I am using in my Java EE projects. Instead of copying the class in each project, I try to put it in a separate jar, so I have this structure:

  • Java EE project with EJB and WAR projects
  • JPALogic: JAR project with JPALogic class in it
  • RemoteServices: JAR project with beans interfaces
  • Services: EJB project with beans including JPALogic and RemoteServices as libraries
  • Frontend: WAR project with frontend including RemoteServices as library.

JPALogic is only used in the EJB project, and there is no reference to it in other parts of the Java EE application. In the JPALogic library, I have a JPALogic bean:

public class JPALogic {
  private EJBContext ejbContext;
  @Inject @ShardedPersistenceContext
  private EntityManager em;
  public JPALogic() {
  [...lots of methods...]


It works fine if the JPALogic code is directly in the EJB project, but when I put it in an external library, the deployment becomes very unstable (netbeans 8 + glassfish 4) and almost every deployment fails with this error:

Exception while deploying the app [app] : Cannot resolve reference [Remote ejb-ref,Remote 3.x interface,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session] because there are [2] ejbs in the application with interface 


I have a search, but it seems that such an error appears when the interface has multiple implementations, and it doesn't: there is only one bean named "JPALogic" and, checking ear, JPALogic.jar appears only once.

Am I doing something wrong?


source to share

1 answer

If the jar contains EJB annotations (@Singleton, @Stateless, @Stateful annotations defining components), it is not a plain jar but an ejb-jar and should be packaged as a module.


  • You should reference JPALogic.jar in application.xml as an EJB module.
  • You can also add "Class-Path: JPALogic.jar" to the META-INF / MANIFEST.MF of the referenced modules to ensure visibility. It may be visible, depending on the container.

Like this:

        (must have a reference to the JPALogic.jar EJB module)
    JPALogic.jar (EJB-JAR)
        META-INF/MANITESF.MF Class-Path: JPALogic.jar


For more details see JAVA EE 6 spec, chapter EE.8:

  • EE.8.3.2 Requirements for Loading an EJB Container Class
  • EE.8.5.2 Deploying a Java EE Application


All Articles