【问题标题】:lookup fail "Root exception is javax.naming.NameNotFoundException"查找失败“根异常是 javax.naming.NameNotFoundException”
【发布时间】:2013-12-25 16:45:30
【问题描述】:

我从数据库创建了实体类,然后我从实体类创建了会话 bean 并且当我尝试测试我的数据是否到达实体类时,我在会话 bean 类上创建了一个 junit 测试,但我得到了查找失败“根异常是 javax.naming.NameNotFoundException: EmpTableFacade!Beans.EmpTableFacade 即使我使用过EJB 注入 然后我尝试通过创建使用会话 bean 的类来手动测试我的代码,但这里有相同的异常。

我认为是 Deploying staff 的错误,但我不知道我哪里做错了 这是会话 bean 接口。

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

    package Beans;

    import java.util.List;
    import javax.ejb.Local;

    /**
     *
     * @author HADDAD
     */
    @Local
    public interface EmpTableFacadeLocal {

        void create(EmpTable empTable);

        void edit(EmpTable empTable);

        void remove(EmpTable empTable);

        EmpTable find(Object id);

        List<EmpTable> findAll();

        List<EmpTable> findRange(int[] range);

        int count();

    }



 and this is the test class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

    package Controller;

    import Beans.EmpTable;
    import Beans.EmpTableFacade;
    import java.util.List;
    import java.util.Properties;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;

    /**
     *
     * @author HADDAD
     */
    public class Functionality {
        List<EmpTable> l;
        Context c=null;
        {
            try {
                Properties props = new Properties();
                props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");

                props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
                // glassfish default port value will be 3700,
                props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
                //props.load(new  FileInputStream("jndi.properties"));
                c = new InitialContext(props);
            } catch (NamingException ex) {
                Logger.getLogger(Functionality.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
        public void retrive() throws NamingException
        {


            EmpTableFacade empTableFacade=(EmpTableFacade)
                    c.lookup("java:global/employee/employee-ejb/EmpTableFacade!Beans.EmpTableFacade");
           l= empTableFacade.findAll();
           System.out.println(l.get(0).getName().toString());
        }
        public static void main(String[] args) throws NamingException
        {
            Functionality f=new Functionality();
            f.retrive();

        }
    }

这是个例外

Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/employee/employee-ejb/EmpTableFacade!Beans.EmpTableFacade' in SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: EmpTableFacade!Beans.EmpTableFacade not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at Controller.Functionality.retrive(Functionality.java:45)
    at Controller.Functionality.main(Functionality.java:52)

【问题讨论】:

    标签: java ejb jndi naming


    【解决方案1】:

    您需要使用Remote 接口公开您的bean。 Local 接口仅可用于在同一 JVM 中调用,这不是您的情况。

    JNDI 名称看起来也有问题。 您没有指定您正在使用哪个 ejb 或服务器版本,这对于解决此类问题很重要,但我认为 link 可以帮助您定义正确的 JNDI 名称条目。

    还请记住,根据 glassfish doc。

    在部署期间,每个可移植的全局 JNDI 名称都会打印到 server.log 中。

    【讨论】:

    • 但是我在同一个 jvm 上工作,我需要远程接口吗?
    • 是的,您的服务器在一个 JVM 中运行,而您的测试类在另一个中运行。当您执行测试类的 main() 方法时,new JVM starts up. -
    • 感谢 Gabriel 的帮助,我将尝试修复 jndi 命名
    猜你喜欢
    • 2013-02-09
    • 2021-01-29
    • 2016-08-19
    • 1970-01-01
    • 2011-10-02
    • 2018-06-15
    • 2021-09-11
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多