0

Is there some particular approach to ensure that an EJB module gets deployed to a web app correctly in Netbeans?

I'm coming at the EJB cookbook example, to invoke the Session Bean from the servlet. I cleared the glassfish output in Netbeans, did a clean-compile and run:

INFO:   visiting unvisited references
INFO:   visiting unvisited references
SEVERE:   Class [ Lpakt/Salutation; ] not found. Error while loading [ class pakt.SalutationServlet ]
INFO:   visiting unvisited references
INFO:   Loading application [SalutationApplication-war] at [/SalutationApplication-war]
INFO:   SalutationApplication-war was successfully deployed in 382 milliseconds.
INFO:   processRequest..
WARNING:   StandardWrapperValve[SalutationServlet]: Servlet.service() for servlet SalutationServlet threw exception
java.lang.NoClassDefFoundError: pakt/Salutation
    at pakt.SalutationServlet.processRequest(SalutationServlet.java:30)
    at pakt.SalutationServlet.doGet(SalutationServlet.java:45)

Why isn't the servlet loading pakt.Salutation?

the clean structure (uncompiled):

SalutationApplication/
├── build.xml
├── nbproject
│   ├── ant-deploy.xml
│   ├── build-impl.xml
│   ├── genfiles.properties
│   ├── private
│   │   └── private.properties
│   ├── project.properties
│   └── project.xml
├── SalutationApplication-ejb
│   ├── build.xml
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   └── src
│       ├── conf
│       │   └── MANIFEST.MF
│       └── java
│           └── pakt
│               ├── LocalBean.java
│               ├── RemoteBean.java
│               └── Salutation.java
├── SalutationApplication-war
│   ├── build.xml
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   ├── src
│   │   ├── conf
│   │   │   └── MANIFEST.MF
│   │   └── java
│   │       └── pakt
│   │           └── SalutationServlet.java
│   └── web
│       ├── index.html
│       └── WEB-INF
│           └── web.xml
└── src
    └── conf
        └── MANIFEST.MF

20 directories, 30 files

servlet:

package pakt;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/SalutationServlet"})
public class SalutationServlet extends HttpServlet {

    @EJB
    private Salutation salutation;

    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("processRequest..");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SalutationServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>"
                    + salutation.getFormalSalutation("Sherlock Holmes")
                    + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.flush();
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

package pakt;

import javax.ejb.Stateless; import javax.ejb.LocalBean;

@LocalBean @Stateless(mappedName = "salutationBean") public class Salutation implements RemoteBean {

public String getFormalSalutation(String name) {
    return "Dear " + name;
}

public String getInformalSalutation(String name) {
    return "Hi " + name;
}

@Override
public void myRemote() {
    System.out.println("myRemote..");
}

}

the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>SalutationServlet</servlet-name>
        <servlet-class>pakt.SalutationServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SalutationServlet</servlet-name>
        <url-pattern>/SalutationServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

-------------------------------------------edit-----------------------------------

additional structure of the app:

thufir@dur:~$ 
thufir@dur:~$ tree NetBeansProjects/SalutationApplication
NetBeansProjects/SalutationApplication
├── build
│   ├── META-INF
│   │   └── MANIFEST.MF
│   ├── SalutationApplication-ejb.jar
│   └── SalutationApplication-war.war
├── build.xml
├── dist
│   └── SalutationApplication.ear
├── nbproject
│   ├── ant-deploy.xml
│   ├── build-impl.xml
│   ├── genfiles.properties
│   ├── private
│   │   └── private.properties
│   ├── project.properties
│   └── project.xml
├── SalutationApplication-ejb
│   ├── build
│   │   ├── classes
│   │   │   ├── META-INF
│   │   │   │   └── MANIFEST.MF
│   │   │   └── pakt
│   │   │       ├── Salutation.class
│   │   │       └── SalutationLocal.class
│   │   ├── empty
│   │   └── generated-sources
│   │       └── ap-source-output
│   ├── build.xml
│   ├── dist
│   │   └── SalutationApplication-ejb.jar
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   └── src
│       ├── conf
│       │   └── MANIFEST.MF
│       └── java
│           └── pakt
│               ├── Salutation.java
│               └── SalutationLocal.java
├── SalutationApplication-war
│   ├── build
│   │   ├── empty
│   │   ├── generated-sources
│   │   │   └── ap-source-output
│   │   └── web
│   │       ├── index.html
│   │       ├── META-INF
│   │       │   └── MANIFEST.MF
│   │       └── WEB-INF
│   │           ├── classes
│   │           │   ├── pakt
│   │           │   └── servlet
│   │           │       └── SalutationServlet.class
│   │           └── web.xml
│   ├── build.xml
│   ├── dist
│   │   └── SalutationApplication-war.war
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   ├── src
│   │   ├── conf
│   │   │   └── MANIFEST.MF
│   │   └── java
│   │       ├── pakt
│   │       └── servlet
│   │           └── SalutationServlet.java
│   └── web
│       ├── index.html
│       └── WEB-INF
│           └── web.xml
└── src
    └── conf
        └── MANIFEST.MF

43 directories, 42 files
thufir@dur:~$ 
thufir@dur:~$ jar -tf NetBeansProjects/SalutationApplication/SalutationApplication-war/dist/SalutationApplication-war.war 
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/pakt/
WEB-INF/classes/servlet/
WEB-INF/classes/servlet/SalutationServlet.class
WEB-INF/web.xml
index.html
thufir@dur:~$ 
thufir@dur:~$ jar -tf NetBeansProjects/SalutationApplication/SalutationApplication-ejb/dist/SalutationApplication-ejb.jar 
META-INF/
META-INF/MANIFEST.MF
pakt/
pakt/Salutation.class
pakt/SalutationLocal.class
thufir@dur:~$ 
thufir@dur:~$ 

this is how the application builds.

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 1
    Does SalutationApplication-war has SalutationApplication-ejb jar in it's WEB-INF/lib directory after building the WAR archive? – Arek Sep 08 '14 at 11:16
  • @Ajan I believe that the answer is "no" based on the above edit which includes the tree of the directory, and the jar structure. **However** `SalutationApplication-ejb` *does* show in the library (in netbeans) for `SalutationApplication-war`, so I'm not exactly sure. However, it doesn't appear in the actual resultant .war file. – Thufir Sep 08 '14 at 23:52
  • It's frustrating that Glassfish reports a successful deploy – basin Aug 08 '18 at 11:02

1 Answers1

0

I started from scrach as so:

NetBeansProjects/SalutationApp
├── build
│   ├── META-INF
│   │   └── MANIFEST.MF
│   ├── SalutationApp-ejb.jar
│   └── SalutationApp-war.war
├── build.xml
├── dist
│   └── gfdeploy
│       └── SalutationApp
│           ├── META-INF
│           │   └── MANIFEST.MF
│           ├── SalutationApp-ejb_jar
│           │   ├── ejb
│           │   │   └── Hello.class
│           │   └── META-INF
│           │       └── MANIFEST.MF
│           └── SalutationApp-war_war
│               ├── index.html
│               ├── META-INF
│               │   └── MANIFEST.MF
│               └── WEB-INF
│                   ├── classes
│                   │   └── srv
│                   │       └── HelloServlet.class
│                   └── web.xml
├── nbproject
│   ├── ant-deploy.xml
│   ├── build-impl.xml
│   ├── genfiles.properties
│   ├── private
│   │   └── private.properties
│   ├── project.properties
│   └── project.xml
├── SalutationApp-ejb
│   ├── build
│   │   ├── classes
│   │   │   ├── ejb
│   │   │   │   └── Hello.class
│   │   │   └── META-INF
│   │   │       └── MANIFEST.MF
│   │   ├── empty
│   │   └── generated-sources
│   │       └── ap-source-output
│   ├── build.xml
│   ├── dist
│   │   └── SalutationApp-ejb.jar
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   └── src
│       ├── conf
│       │   └── MANIFEST.MF
│       └── java
│           └── ejb
│               └── Hello.java
├── SalutationApp-war
│   ├── build
│   │   ├── empty
│   │   ├── generated-sources
│   │   │   └── ap-source-output
│   │   └── web
│   │       ├── index.html
│   │       ├── META-INF
│   │       │   └── MANIFEST.MF
│   │       └── WEB-INF
│   │           ├── classes
│   │           │   └── srv
│   │           │       └── HelloServlet.class
│   │           └── web.xml
│   ├── build.xml
│   ├── dist
│   │   └── SalutationApp-war.war
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   ├── src
│   │   ├── conf
│   │   │   └── MANIFEST.MF
│   │   └── java
│   │       └── srv
│   │           └── HelloServlet.java
│   └── web
│       ├── index.html
│       └── WEB-INF
│           └── web.xml
└── src
    └── conf
        └── MANIFEST.MF

52 directories, 46 files

I think that the only difference was in the sequence of clicking. I right clicked "clean" on the ejb and war projects, then the main application, and then hit "run" on the overall project.

I think that this then fixed the packaging...?

although the .war file seems quite similar, aside from some naming changes:

thufir@dur:~$ 
thufir@dur:~$ jar -tf NetBeansProjects/SalutationApp/SalutationApp-war/dist/SalutationApp-war.war 
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/srv/
WEB-INF/classes/srv/HelloServlet.class
WEB-INF/web.xml
index.html
thufir@dur:~$ 

I'm not quite sure why this example runs where the other didn't aside, from, perhaps, clicking "clean" and "run" on different sub-projects in error.

Thufir
  • 8,216
  • 28
  • 125
  • 273