EJB Jndi look up in Glassfish server only in Java

2019-06-08 15:04发布

问题:

I am trying to connect to the ejb service in glassfish server through Java Web start. I am getting the following error while getting the initial context. I have also added the code snippet for getting the initial context.

One interesting is when I run the program as a simple java program outside of java web start in a netbeans IDE, it is working fine.

I added all the relevant jar files from the glassfish modules folder into the java web start.

java.lang.NullPointerException
    at com.sun.enterprise.naming.impl.SerialContext.<init>(SerialContext.java:275)
    at com.sun.enterprise.naming.impl.SerialContext.<init>(SerialContext.java:334)
    at com.sun.enterprise.naming.impl.SerialInitContextFactory.createInitialContext(SerialInitContextFactory.java:358)
    at com.sun.enterprise.naming.impl.SerialInitContextFactory.getInitialContext(SerialInitContextFactory.java:353)
    at com.sun.enterprise.naming.SerialInitContextFactory.getInitialContext(SerialInitContextFactory.java:69)
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.InitialContext.<init>(Unknown Source)
    at gov.ca.lc.util.ServiceLocator.getInitialContext(ServiceLocator.java:140)
    at gov.ca.lc.util.ServiceLocator.getVotesEJB(ServiceLocator.java:103)
    at gov.ca.lc.scenemanagement.AbstractVotingScene.getCommitteeList(AbstractVotingScene.java:143)
    at gov.ca.lc.scenemanagement.AbstractVotingScene.<init>(AbstractVotingScene.java:65)
    at gov.ca.lc.scenes.MenuScene.<init>(MenuScene.java:56)
    at gov.ca.lc.menu.StartVoting.startUp(StartVoting.java:47)
    at org.mt4j.MTApplication.setup(MTApplication.java:328)
    at processing.core.PApplet.handleDraw(PApplet.java:1580)
    at processing.core.PApplet.run(PApplet.java:1502)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "Animation Thread" java.lang.NullPointerException
    at gov.ca.lc.scenemanagement.AbstractVotingScene.getCommitteeList(AbstractVotingScene.java:143)
    at gov.ca.lc.scenemanagement.AbstractVotingScene.<init>(AbstractVotingScene.java:65)
    at gov.ca.lc.scenes.MenuScene.<init>(MenuScene.java:56)
    at gov.ca.lc.menu.StartVoting.startUp(StartVoting.java:47)
    at org.mt4j.MTApplication.setup(MTApplication.java:328)
    at processing.core.PApplet.handleDraw(PApplet.java:1580)
    at processing.core.PApplet.run(PApplet.java:1502)
    at java.lang.Thread.run(Unknown Source)


Following is my code to get the initial context

    private static InitialContext getInitialContext()
            throws NamingException {
            Properties props = null;
            try{  
    //            props=new Properties();   
    //            props.load(new FileInputStream(new File("jndi.properties")));
    //            System.out.println(props.get("java.naming.factory.initial"));
                 props=new Properties();   


    props.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");  
        props.setProperty("java.naming.factory.url.pkgs ", "com.sun.enterprise.naming");//ur server ip  
        props.setProperty("java.naming.factory.state ", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");//ur server ip  
        props.setProperty("org.omg.CORBA.ORBInitialHost", "165.107.33.181");//ur server ip  
        props.setProperty("org.omg.CORBA.ORBInitialPort","3700"); //default is 3700  


            }catch(Exception ex){
                ex.printStackTrace();
            }
            return new InitialContext(props);
        }

回答1:

I have the same problem. I use JMS and I need to add other jars. At first I tried using gf-client.jar, but it does not work through Java Web start. So I've added jars listed in Connecting a remote JMS client to GlassFish 3 . It works outside of Java Web start.



回答2:

I don't know what the relevant jar files are for you, but just in case we have a different view on that area: I use just appserv-rt.jar and java-ee.jar using the same properties you use for initial context and it works fine. Don't add anything else you don't need and try again.



回答3:

package-appclient in glassfish/bin creates appclient.jar that include jars for client. Then unpack it on a client and use gf-client.jar as usual.



回答4:

I would like to add that all these set up will work only if you have the glassfish installed in your machine. If you deploy the java webstart app in a machine which doesn't have the glassfish server then we will end up getting the same exception. The reason is that if you open the appserv-rt.jar it points to the gf-client.jar and if you look into the gf-client.jar it points to a whole bunch of jar files in modules folder. So I suppose, essentially we need the all the jar files pointed by the gf-client.jar.