+1  A: 

Yes you can. Some specifics are here (references EJB2 but it the same for EJB3 when it comes to remote clients): http://www.theserverside.com/discussions/thread.tss?thread%5Fid=9197

Paraphrased:

Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url", "jnp://localhost:1099");
env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
Context ctx = new InitialContext(env); 
// name is whatever JNDI name you gave it 
Object o = ctx.lookup("home name"); 
EJBHome ejbHome = (EJBHome) PortableRemoteObject.narrow(o,EJBHome.class); 
// This is userID should be the one passed. 
EJB ejb = ejbHome.create(..);
Courtney Palit
I am working on EJB3 ,,,
Moro
@Coutney This is EJB >= 2.1 version example
cetnar
I'm not sure remote client access has changed in EJB3. The server-side code has been cleaned up a lot in EJB3, with the awesome annotation-driven configuration. However, the basics of RMI invocation are the same as I showed in the paraphrased block of code as far as I know.
Courtney Palit
Just for sanity, I found an EJB3 tutorial that backs up the fact that remote client access is pretty much the same in EJB2 and EJB3: http://www.laliluna.de/ejb-3-tutorial-jboss.html
Courtney Palit
Thank u very much ... I am running it now.
Moro
@Courney. Code you pasted is EJB 2.1 client that will work with EJB 3.0 server, but this code is stale. Look at http://www.oracle.com/technology/tech/java/oc4j/1013/how_to/how-to-ejb30-compatibility-ejb2x/doc/how-to-ejb30-compatibility-ejb2x.html Home classes are needless in EJB 3.0.
cetnar
Good to know, thanks cetnar!
Courtney Palit
A: 

Yes, you can do that.

Nick Veys
+1  A: 

Yes.

public static void main(String args[]) throws Exception {

   InitialContext ctx = new InitialContext();
   YourService yourService = (YourService) ctx.lookup("com.example.session.YourService");
   String time = yourService.getTime();
   System.out.println("Time is: " + time);
}

For client configuration you must provide jndi.properties file with contents

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

If you are looking for working examples on JBoss try download source code of Enterprise JavaBeans 3.0, Fifth Edition

cetnar
Are you sure this is the right JNDI name to lookup when using JBoss?
Pascal Thivent
@Pascal. Yes, it from examples from book I mentioned in my answer.
cetnar
+1  A: 

Let's assume you have the following remote interface:

@Remote
public interface HelloBeanRemote {
    public String sayHello();
}

And a session bean implementing it:

@Stateless
public class HelloBean implements HelloBeanRemote {
    ...
}

And that this EJB is correctly packaged and deployed on JBoss.

On the client side, create a jndi.properties with the following content and put it on the classpath:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

Then use the following code to call your EJB:

Context context;
try {
    context = new InitialContext();
    HelloBeanRemote beanRemote = (HelloBeanRemote)context.lookup("HelloBean/remote"); 
    beanRemote.test(); 
} catch (NamingException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
}

Alternatively, if you don't want to provide a jndi.properties file, you can explicitly setup the JNDI environment in the code and create the context like this:

Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url","localhost:1099");
Context context = new InitialContext(properties);

But I'd recommend using the jndi.properties for the sake of portability.

Pascal Thivent
A: 

You can also expose the bean as a web service. I believe this is available as of EJB 3. It is quite nice considering you can do it with annotations. You may wish to consider using this option to decrease coupling. Here is a link to a tutorial.

Matthew Sowders