| Author |
exception while runing EJB3.0 basic application
|
rachna jain
Ranch Hand
Joined: Jul 14, 2009
Posts: 74
|
|
Hi All
I have created a simple EJB3 calculator bean and remote interface
i have created jar deployed it using WEBSPHERE enterprise edition server and eclipse IDE
while runing caculator client
/**
*
*/
package ejb3inaction.example.client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import ejb3inaction.example.StatelessCalculator;
/**
* @author rachna
*
*/
public class CalculatorClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Context jndiContext = new InitialContext();
Object ref = jndiContext.lookup("StatelessCalculator/remote");
StatelessCalculator calc = (StatelessCalculator) PortableRemoteObject
.narrow(ref, StatelessCalculator.class);
System.out.println("4 + 3 = " + calc.add(4,3));
System.out.println("4 - 3 = " + calc.subtract(4,3));
System.out.println("4 * 3 = " + calc.multiply(4,3));
System.out.println("4 / 3 = " + calc.divide(4,3));
} catch (NamingException ne) {
ne.printStackTrace();
}
}
}
Exception on console------
javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable {} [Root exception is java.lang.NullPointerException]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:243)
at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:327)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:357)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:447)
at javax.naming.InitialContext.lookup(InitialContext.java:455)
at ejb3inaction.example.client.CalculatorClient.main(CalculatorClient.java:31)
Caused by: java.lang.NullPointerException
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:235)
... 5 more
Please help
THnaks
|
Rachna Jain
SCWCD 1.5
|
 |
Ivan Krizsan
Bartender
Joined: Oct 04, 2006
Posts: 2193
|
|
Hi!
In WebSphere, you cannot create an InitialContext without supplying some parameters, like described in this article:
http://publib.boulder.ibm.com/infocenter/wasinfo/v5r0//topic/com.ibm.websphere.base.doc/info/aes/ae/rnam_example_prop2.html
Best wishes!
|
My free books and tutorials: http://www.slideshare.net/krizsan
|
 |
amit punekar
Ranch Hand
Joined: May 14, 2004
Posts: 488
|
|
Hi,
I would like to add the following to Ivan's reply.
Because you are using the Standalone Java Client to execute your EJB, you need to obtain the JNDI context for that particular environment where your EJB is deployed.
Instead if you put the same code in a JSP deployed as part of the webapplication that belongs to the EAR (EJB + WEB APP) , then getting a default InitialContext works fine.
Hope this helps,
Amit
|
 |
rachna jain
Ranch Hand
Joined: Jul 14, 2009
Posts: 74
|
|
Thanks all for replying but i need to clarify one more doubt
i have not edited any deployment descriptor and there i cant see remote interface ref aur name so
are they autogenerated in EJB3 or i need to edit ejb-jar.xml and open-ejbjar.xml for defining jndi context
Please clarify as i have worked over ejb2 and there we do it.
Thanks
|
 |
Ivan Krizsan
Bartender
Joined: Oct 04, 2006
Posts: 2193
|
|
Hi!
JNDI names are automatically generated in EJB 3.x, so you do not need to use the deployment descriptor if you do not want to. Prior to EJB 3.1, there is no standard so you have to investigate what names are created in the particular container you use.
To specify a JNDI name of an EJB you can use the mappedName element in the annotation annotating the bean implementation class (@Stateful, @Stateless etc).
With EJB 3.1, JNDI names have been standardized and using such portable JNDI names, your program retains portability across containers. For more information, see section 4.4 of the EJB 3.1 specification.
Best wishes!
|
 |
rachna jain
Ranch Hand
Joined: Jul 14, 2009
Posts: 74
|
|
Hi all
Thanks for giving a nice explanation to my problem.
|
 |
 |
|
|
subject: exception while runing EJB3.0 basic application
|
|
|