[ACCEPTED]-ejb lookup failing with NamingException-jndi

Accepted answer
Score: 37

I think you want to access an EJB application 85 (known as EJB module) from a web application 84 in Sun Application Server, right ?

ok, let's 83 go.

When you deploy an EJB into an application 82 server, the application server gives it 81 an address - known as global JNDI address 80 - as a way you can access it (something 79 like your address). It changes from an application 78 server to another.

In JBoss Application 77 Server, you can see global JNDI address 76 (after starting it up) in the following 75 address

http://127.0.0.1:8080/jmx-console/HtmlAdaptor

In Sun Application Server, if you 74 want to see global JNDI address (after starting 73 it up), do the following

Access the admin 72 console in the following address

http://127.0.0.1:4848/asadmin

And click 71 JNDI browsing

If your EJB IS NOT registered right there, there is something wrong

EJB comes in two flavors: EJB 2.1 and EJB 70 3.0. So what is the difference ?

Well, well, well...

Let's 69 start with EJB 2.1

  1. Create a Home interface

It defines methods for 68 CREATING, destroying, and finding local 67 or remote EJB objects. It acts as life cycle 66 interfaces for the EJB objects. All home 65 interfaces have to extend standard interface 64 javax.ejb.EJBHome - if you a using a remote 63 ejb object - or javax.ejb.EJBLocalHome - if 62 you are using a local EJB object.

// a remote EJB object - extends javax.ejb.EJBHome
// a local EJB object - extends javax.ejb.EJBLocalHome
public interface MyBeanRemoteHome extends javax.ejb.EJBHome {

    MyBeanRemote create() throws javax.ejb.CreateException, java.rmi.RemoteException;

}

Application 61 Server will create Home objects as a way 60 you can obtain an EJB object, nothing else.

Take 59 care of the following

A session bean’s remote 58 home interface MUST DEFINE ONE OR MORE create<METHOD> methods. A 57 stateless session bean MUST DEFINE exactly 56 one <METHOD> method with no arguments.

...

throws 55 clause MUST INCLUDE javax.ejb.CreateException

...

If 54 your Home interface extends javax.ejb.EJBHome, throws 53 clauses MUST INCLUDE the java.rmi.RemoteException. If 52 it extends javax.ejb.EJBLocalHome, MUST 51 NOT INCLUDE the java.rmi.RemoteException.

...

Each 50 create method of a stateful session bean 49 MUST BE NAMED create<METHOD>, and 48 it must match one of the Init methods 47 or ejbCreate<METHOD> methods defined 46 in the session bean class. The matching 45 ejbCreate<METHOD> method MUST HAVE 44 THE SAME NUMBER AND TYPES OF ARGUMENTS. The 43 create method for a stateless session bean 42 MUST BE NAMED create but need not have a matching 41 “ejbCreate” method.


Now create an business 40 interface in order to define business logic 39 in our EJB object

// a remote EJB object - extends javax.ejb.EJBObject
// a local EJB object - extends javax.ejb.EJBLocalObject
public interface MyBeanRemote extends javax.ejb.EJBObject {

    void doSomething() throws java.rmi.RemoteException;

}

Now take care of the following

If 38 you are using a remote EJB object, remote 37 interface methods MUST NOT EXPOSE local 36 interface types or local home interface 35 types.

...

If your Home interface extends 34 javax.ejb.EJBObject, throws clauses MUST 33 INCLUDE the java.rmi.RemoteException. If 32 it extends javax.ejb.EJBLocalObject, MUST 31 NOT INCLUDE the java.rmi.RemoteException.


Now 30 our EJB

public class MyBean implements javax.ejb.SessionBean {

    // why create method ? Take a special look at EJB Home details (above)
    public void create() {
        System.out.println("create");
    }

    public void doSomething() throws java.rmi.RemoteException {
        // some code
    };

}

Now take care of the following

It 29 MUST IMPLEMENTS javax.ejb.SessionBean. It 28 defines four methods - not shown above: setSessionContext, ejbRemove, ejbPassivate, and 27 ejbActivate.

Notice our bean DOES NOT IMPLEMENT our business 26 interface because of EJB specification says:

For 25 each method defined in the interface, there 24 must be a matching method in the session bean’s class. The matching 23 method must have:

  • The same name
  • The same number and types of arguments, and the same return type.
  • All the exceptions defined in the throws clause of the matching method of the session bean class must be defined in the throws clause of the method of the local interface.

And YOU HAVE TO DECLARE 22 a ejb-jar.xml file according to

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" version="2.1">
    <enterprise-beans>
        <session>
            <ejb-name>HelloWorldEJB</ejb-name>
            <home>br.com.MyBeanRemoteHome</home>
            <remote>br.com.MyBeanRemote</remote>
            <local-home>br.com.MyBeanLocalHome</local-home>
            <local>br.com.MyBeanLocal</local>
            <ejb-class>br.com.MyBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>
        </session>
    </enterprise-beans>
</ejb-jar>

If you do 21 not have a local EJB object remove from 20 the deployment descriptor above

<local-home>br.com.MyBeanLocalHome</local-home>
<local>br.com.MyBeanLocal</local>

If you do 19 not have a remote EJB object remove from 18 the deployment descriptor above

<home>br.com.MyBeanRemoteHome</home>
<remote>br.com.MyBeanRemote</remote>

And put in 17 META-INF directory

Our jar file will contain 16 the following

/META-INF/ejb-jar.xml
br.com.MyBean.class
br.com.MyBeanRemote.class
br.com.MyBeanRemoteHome.class

Now our EJB 3.0

// or @Local
// You can not put @Remote and @Local at the same time
@Remote
public interface MyBean {

    void doSomething();

}

@Stateless
public class MyBeanStateless implements MyBean {

    public void doSomething() {

    }

}

Nothing else,

In 15 JBoss put jar file in

<JBOSS_HOME>/server/default/deploy

In Sun Application 14 Server access (after starting it up) admin 13 console

http://127.0.0.1:4848/asadmin

And access EJB Modules in order to 12 deploy your ejb-jar file

As you have some 11 problems when deploying your application 10 in NetBeans, i suggest the following

  1. Create a simple Java library PROJECT (a simple jar without a main method)
  2. Add /server/default/lib (contains jar files in order you retrieve your EJB's) jar files to your Java application whether you are using JBoss (I do not know which directory in Sun Application Server)
  3. Implement code above

Now 9 create another war PROJECT

  1. Add our project created just above and add <JBOSS_HOME>/client (contains jar files in order to access our EJB's). Again i do not know which directory in Sun Application Server. Ckeck out its documentation.
  2. See its global mapping address as shown in the top of the answer

And implement 8 the following code in your Servlet or something 7 else whether you are using JBoss

public static Context getInitialContext() throws javax.naming.NamingException {

    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://127.0.0.1:1099");

    return new javax.naming.InitialContext(p);
}

Or the following 6 whether you are using Sun Application Server 5 - put the file appserv-rt.jar (I do not 4 know which past contain appserv-rt.jar in 3 Sun Application Server) in your classpath

public static Context getInitialContext() throws javax.naming.NamingException {

    return new javax.naming.InitialContext();

}

In 2 order to access your EJB in our Servlet 1 or something else

MyBeanRemote myBean = (MyBeanRemote) getInitialContext().lookup(<PUT_EJB_GLOBAL_ADDRESS_RIGHT_HERE>);

myBean.doSomething();

regards,

Score: 1

First, fix your web.xml and add the Remote 15 Interface in it:

<ejb-ref>
  <description>Sample EJB</description>
  <ejb-ref-name>SampleBean</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <home>com.SampleHome</home>
  <remote>com.Sample</remote> <!-- the remote interface goes here -->
</ejb-ref>

Then, regarding the java.lang.NoSuchMethodError, Sean 14 is right, you have a mismatch between the 13 version of the app server "client library" you 12 are using inside NetBeans and the app server 11 version (server-side). I can't tell you 10 exactly which JARs you need to align though, refer 9 to the Sun Application Server documentation.

PS: This 8 is not a direct answer to the problem but 7 I don't think you're currently passing any 6 useful properties when creating your initial 5 context with the results of the call to 4 System.getProperties(), there is nothing helpful in these properties 3 to define the environment of a context (e.g. the 2 initial context factory). Refer to the InitialContext javadocs 1 for more details.

Score: 1

Last two answers are both correct in that 14 they are things you need to change/fix. But 13 the NoSuchMethodError you see is not from 12 your code, nor from things trying to find 11 your code (would produce some kind of NoClassDefFoundException, I 10 think, were this the case). This looks more 9 like incompatible versions of the JNDI provider 8 provided by the container, and what the 7 JNDI implementation in the Java library 6 wants. That's a pretty vague answer, but, would 5 imagine it is solvable by perhaps upgrading 4 your application server, and, ensuring you 3 aren't deploying possibly-stale copies of 2 infrastructure classes related to JNDI with 1 your app, that might interfere.

More Related questions