Java EE新手使用GlassFish 5.0遇JNDI查找NamingException问题求助
Hey there, let's work through this naming exception you're hitting with your stateless EJB lookup. The root error NameNotFoundException: ejb1 tells us the server can't find the module or app named ejb1 in its JNDI tree, so let's break down the fixes step by step:
1. First, Verify the Correct JNDI Name
GlassFish generates standardized JNDI names for EJBs when they're deployed, and guessing the name is a common pitfall. The easiest way to get the exact right name is to check GlassFish's server logs:
- Open your GlassFish
server.log(usually in<glassfish-install-dir>/glassfish/domains/domain1/logs) - Search for a line starting with
Portable JNDI names for EJB FlightServiceBean: - You'll see entries like this:
Portable JNDI names for EJB FlightServiceBean: [java:global/your-app-name/your-module-name/FlightServiceBean!com.airline.service.FlightServiceBean, java:global/your-app-name/your-module-name/FlightServiceBean]
Use one of these exact strings in your lookup() call—don't assume ejb1 is the correct app/module name (it might be your project name, but deployment can change it if you renamed the artifact).
2. Check EJB Deployment Status
Make sure your EJB module is actually deployed and running in GlassFish:
- Go to the GlassFish Admin Console (default URL:
http://localhost:4848) - Navigate to Applications and look for your EJB module (or the WAR/EAR containing it)
- Confirm its status is
Running—if it's failed, fix any deployment errors first (check logs for clues)
3. Ditch Manual Lookup for @EJB Injection (Simpler & Less Error-Prone)
Manual JNDI lookup is easy to mess up. Instead, let GlassFish handle injecting the EJB directly into your Servlet using the @EJB annotation. Here's how to rewrite your Servlet:
import javax.ejb.EJB; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/FlightDetailsServlet") // Match this to your URL pattern public class FlightDetailsServlet extends HttpServlet { // Let GlassFish inject the EJB automatically @EJB private FlightServiceBean fs; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); out.println("The flights details servlet has been called ..."); if (fs != null) { out.println("Successfully got the FlightServiceBean EJB!"); // Now you can call methods on fs directly } else { out.println("Failed to inject FlightServiceBean."); } } }
This eliminates the need for InitialContext and manual lookup entirely, avoiding naming mistakes.
4. Double-Check Your EJB & Servlet Setup
If you still want to use manual lookup, confirm these details:
- Your EJB's fully qualified class name (
com.airline.service.FlightServiceBean) matches exactly what's in the Bean's file (check thepackagestatement at the top ofFlightServiceBean.java) - If your EJB is packaged in an EAR, the JNDI name will include the EAR name first:
java:global/ear-name/ejb-module-name/FlightServiceBean!com.airline.service.FlightServiceBean - The
mappedNamein@Stateless(mappedName = "FlightServiceBean")is GlassFish-specific, but it doesn't affect the portable JNDI name—you can ignore it for lookup unless you specifically want to use that mapped name (portable names are safer for cross-server compatibility).
Quick Troubleshooting Tip
If you're using an IDE like Eclipse or IntelliJ, make sure your deployment artifact (WAR/EAR) includes the EJB class. Sometimes IDEs don't automatically include EJBs in the deployment package, so double-check your project's deployment assembly settings.
内容的提问来源于stack exchange,提问作者Mohammad Ismail

