How can I bind lookup with a String

2019-09-18 06:27发布

问题:

My Program is package client;

import homeif.HelloWorldHome;
import remoteif.HelloWorld;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.awt.image.LookupOp;
import java.util.Properties;

public class HelloClient {
    public static void main(String args[]) {
        try {


            Context initialContext = new InitialContext();

            Object object = initialContext.lookup("myHelloWorld");
            HelloWorldHome home =
                    (HelloWorldHome) PortableRemoteObject.narrow(object,
                            HelloWorldHome.class);
            HelloWorld myHelloWorld = home.create();
            String message = myHelloWorld.sayHello();
            System.out.println(message);
        } catch (Exception e) {
            System.err.println(" Error : " + e);
            System.exit(2);
        }

    }
}

I am getting a javax.naming.NameNotFoundException: myHelloWorld not bound.I have tried to bind it using initialContext.bind("myHelloWorld",null); But getting a NullPointerException. My jndi.properties includes

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
I have specified lookup-name myHelloWorld in ejb-jar.xml. what should I have to do to bind the lookup

In jonas-ejb-jar.xml   
  ejb-name->HelloWorld 
  jndi-name->myHelloWorld

回答1:

It seems like the JNDI name ('myHelloWorld') that you are trying to connect is incorrect. Your JNDI name should bound either in Java scope or in Global scope. If it is in Java naming scope then you should access it using java:myHelloWorld, otherwise directly. You can verify the naming scope from the Jboss Admin console. Java name sapce means, it is accessible only from the same server. Global name space means, it is accessible from any other server. You can control this using the configuration in ejb-jar.xml.

Other possibility is your bean not get deployed properly. It will show a message in server log on the startup. This message also contains the JNDI name that the bean bound. Try a server restart can see you can get this details