0

I am trying to access an ejb method sayHello() from my client code.
What I have did: 1:created a HelloWorld interface

    package remoteif;
    import java.rmi.*;
    import javax.ejb.*;
    public interface HelloWorld extends EJBObject{
    public String sayHello() throws RemoteException;
     }

2:HelloWorldHome

package homeif;
import remoteif.HelloWorld;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
public interface HelloWorldHome extends EJBHome {
    public HelloWorld create() throws
            CreateException, RemoteException;
}

3: a Bean Implementation HelloWorldBean

package beanimpl;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.rmi.RemoteException;
public class HelloWorldBean implements SessionBean {
    // Methods of Remote interface
    public String sayHello() {
        return "Hello, world !";
    }
    // Methods of Home interface
    public void ejbCreate() {}
    protected SessionContext ctx;
    public void setSessionContext(SessionContext ctx) {
        this.ctx = ctx;
    }
    @Override
    public void ejbRemove() throws EJBException, RemoteException {
        //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public void ejbActivate() throws EJBException, RemoteException {
        //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public void ejbPassivate() throws EJBException, RemoteException {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

4:Deployment descriptors : jonas-ejb-jar.xml & ejb-jar.xml
5:created jar files by including these classes ,interfaces and the xml files
6:created a client application & added the created jar as library file
7.created HelloClient

package client;
import homeif.HelloWorldHome;
import remoteif.HelloWorld;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class HelloClient {
    public static void main(String args[]) {
        try {
            Context initialContext = new InitialContext();
            Object objref = initialContext.lookup("myHelloWorld");
            System.out.println("3");
            HelloWorldHome home =
                    (HelloWorldHome) PortableRemoteObject.narrow(objref,
                            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);
        }
    }
}

But while running it, I am getting

javax.naming.NoInitialContextException: 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
    
How can I solve this?
anirban
  • 674
  • 1
  • 7
  • 21
Sreevidya Aravind
  • 433
  • 1
  • 6
  • 18

2 Answers2

0

Create a file named jndi.properties in your classpath and add the following lines:

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

This will solve your problem.

Sreevidya Aravind
  • 433
  • 1
  • 6
  • 18
Jens
  • 67,715
  • 15
  • 98
  • 113
  • How can I get the classpath of my project? I am using Intellij. – Sreevidya Aravind Jun 06 '14 at 06:25
  • I don`t know. Because i am not using Intellij. But see [here](http://stackoverflow.com/questions/854264/how-to-add-directory-to-classpath-in-an-application-run-profile-in-intellij-idea) may it helps you. – Jens Jun 06 '14 at 06:27
  • I am getting javax.naming.NameNotFoundException: myHelloWorld not bound – Sreevidya Aravind Jun 06 '14 at 08:16
  • Taka a look [here](http://stackoverflow.com/questions/3911856/javax-naming-namenotfoundexception) mybe it helps. – Jens Jun 06 '14 at 08:20
0

You marked this topic as answered, but I would like to say, that in my opinion, you have to annotate your bean as @LocalBean or your interface as @Remote / @Local to have a remote / local bean.

jcomouth
  • 324
  • 3
  • 16
  • Thank you. jndi.properties was the answer to clear that error message. can you please help me in this http://stackoverflow.com/questions/24080004/how-can-i-bind-lookup-with-a-string?noredirect=1#comment37139013_24080004 – Sreevidya Aravind Jun 09 '14 at 06:28