1
votes

I am a beginner in EJB 3.1 and trying to run my first Hello World program and getting the NameNotFoundException.

Exception in thread "main" javax.naming.NameNotFoundException: WFNAM00004: Name "global/HelloWorld/HelloWorldBean!api.HelloWorldRemote" is not found
at org.wildfly.naming.client.util.NamingUtils$1.lookupNative(NamingUtils.java:95)
at org.wildfly.naming.client.AbstractContext.lookup(AbstractContext.java:84)
at org.wildfly.naming.client.WildFlyRootContext.lookup(WildFlyRootContext.java:150)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at driver.HelloWorld.main(HelloWorld.java:27)

Here are my classes

Local Interface:

@Local
 public interface HelloWorldLocal {
public void getHello();
 }

Remote Interface:

@Remote
public interface HelloWorldRemote extends HelloWorldLocal {

 }

Bean Class:

@Stateless(name="HelloWroldBean")
public class HelloWorldBean implements HelloWorldLocal, HelloWorldRemote{

@Override
public void getHello() {
    System.out.println("Hello Cheppura");
}

  }

Client:

public class HelloWorld {

public static void main(String[] args) throws NamingException {
    final Hashtable<String, String> jndiProperties = new Hashtable<>();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY,         
            "org.wildfly.naming.client.WildFlyInitialContextFactory");
    Context con = new InitialContext(jndiProperties);

    final String appName = "";
    final String moduleName = "HelloWorld";
    final String beanName = HelloWorldBean.class.getSimpleName();
    final String viewClassName = HelloWorldRemote.class.getName();

    Object ob = con.lookup("java:global" + appName + "/" + moduleName  + "/" + beanName + "!" + viewClassName);

    HelloWorldRemote hw = (HelloWorldRemote) PortableRemoteObject.narrow(ob, HelloWorldRemote.class);

    hw.getHello();
}

}

Can anyone please advice on this? Thanks in advance

1
I've never used WildFly, but it looks like you're not actually registering the bean anywhere. con.lookup looks for registered beans, but there's nothing telling WildFly to create an instance of HelloWorldBean.Yserbius
What is the reason to extend the Remote interface from the Local one?Alex Sergeenko
You have a typo on your implementation in the Stateless anotation you wrote: HelloWroldBean. Please correct and try again. 😁Level_Up
@Pulszar I guess it makes no sense in that case.Alex Sergeenko
What is your deployment unit - JAR or EAR?Alex Sergeenko

1 Answers

0
votes

You've violated the JEE specification, since there is no inheritance in EJB interfaces.

Your Remote interface extends the Local one but such approach goes against the specification. I think such EAR will never deployed correctly, regardless a runtime environment.

If you want to write a less code this way, then create a third interface with the public void getHello(); method and extend it in both Local and Remote interfaces.

public interface Secondary {
    public void getHello();
}

@Remote
public interface HelloWorldRemote extends Secondary {
 }

@Local
 public interface HelloWorldLocal extends Secondary{
 }