EJB‎ > ‎

EJB Hello World Example

This example Uses JBoss as its server side EJB3 Container

Server Side:

HelloEJB.java
package info.compute.example;

import javax.ejb.Stateless;

@Stateless
public class HelloEJB implements HelloEJBRemote {
    public String hello() {
        return "Hello";
    }
}

HelloEJBRemote.java
package info.compute.example;

import javax.ejb.Remote;

@Remote
public interface HelloEJBRemote {
    public String hello();
}

Client Side:

HelloClient.java
package info.compute.example;

import javax.naming.Context;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;


public class HelloClient
{
   public static void main(String[] args) throws Exception
   {      
       try {
      Properties p = new Properties( );
      p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
           Context jndiContext = new javax.naming.InitialContext(p);
           Object ref = jndiContext.lookup("HelloEJB/remote");
           HelloEJBRemote dao = (HelloEJBRemote)PortableRemoteObject.narrow(ref,HelloEJBRemote.class);
           System.out.println(dao.hello());
       } catch (javax.naming.NamingException ne){ne.printStackTrace( );}
   }
}

To compile the client, you would need jbossall-client.jar (copy from the client folder of your jboss server) in your build path.

Comments