Search code examples
javaxmleclipseejb

Name [HelloBeanName] is not bound in this Context. Unable to find [HelloBeanName]


I am new in ejb,and i'm trying to run simple ejb application..my application consist of classes Hello(remote interface),HelloBean(class that implements Hello interface) and HelloClient(which i run).Program should write to console "Hello world",but i always get error"Name [HelloBeanName] is not bound in this Context. Unable to find [HelloBeanName]"...Here are those 3 classes
Hello Interface:

package vezba2;

public interface Hello {
public String hello();
}

HelloBean:

    package vezba2;

import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(name="HelloBeanName")
@Remote(Hello.class)
public class HelloBean implements Hello{

    @Override
    public String hello() {
        // TODO Auto-generated method stub
        System.out.println("hello()");
        return "Hello,World!"; 

    }


}

HelloClient:

package vezba2;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class HelloClient {

    public static void main(String[] args) throws NamingException {
        // TODO Auto-generated method stub

        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.naming.java.javaURLContextFactory");

        //i first didn't have system.setProperty and i was getting error:Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial//

        Context ctx = new InitialContext();
        Hello hello=(Hello) ctx.lookup("HelloBeanName");
        System.out.println(hello.hello());
    }

}

I am using java8,also ApacheTomee plus 1.7.0 as server,in server folder i have files server.xml,context.xml files,tomcat-users.xml,web.xml files,and in my project i have ejb-jar.xml,and none of those i didn't edited and changed..I also run this application with "Run HelloClient" on green arrow in eclipse.(when i start the server,it doesn't offer to run this on server...)
Is there maybe someone know this problem and how can i make my program working?
Should i perhaps edit xml files?Should i try to run on server my application?(somehow)...
Is there also some good detailed literature on how to run,and work with ejb?Thanks in advance.`

(P.S. When i put this in HelloClient:

System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.RemoteInitialContextFactory");        System.setProperty(Context.PROVIDER_URL,"http://localhost:8080/tomee/ejb");
    Context ctx = new InitialContext();
    Hello hello=(Hello) ctx.lookup("HelloBeanName");
    System.out.println(hello.hello());


i get following error:

RequestFailed{server=http://localhost:8080/tomee/ejb} JNDI_LOOKUPnull:/HelloBeanName {error=Cannot open input stream to server: }
Exception in thread "main" org.apache.openejb.client.ClientRuntimeException: Invalid response from server: -1
    at org.apache.openejb.client.JNDIContext.lookup(JNDIContext.java:297)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at vezba2.HelloClient.main(HelloClient.java:18)

now i think i connected apache server and the program which runs HelloClient ..)


Solution

  • If your main is not in the container you need to specify how to connect to the server. For tomee the remote context factory is explained there http://tomee.apache.org/advanced/client/jndi.html

    Side note: since version 1.7.4 and 7.x the remoting needs some more configuration to avoid the 0-day vulnerability. See http://tomee.apache.org/security/tomee.html