File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Starting problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Starting problem" Watch "Starting problem" New topic

Starting problem

Chinababu Illa

Joined: Jun 04, 2008
Posts: 23
Hi All,

I started preparing for the SCBCD 5 recently and I started of with writing simple stateless bean, deploying and running. But I could not cross the initial step. Below are the details...

Remote Interface:

package com.calculator;

import javax.ejb.Remote;

public interface StatelessCalculator {
public void add(int a, int b);

Bean Class:

package com.calculator;

import javax.ejb.Stateless;

public class StatelessCalculatorBean implements StatelessCalculator {
public void add(int a, int b){
System.out.println("Addition = " + (a+b));

Client Code:

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.calculator.StatelessCalculator;

public class CalculatorEJB3Test {

* @param args
public static void main(String[] args) {

try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory" );
props.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.namingrg.jnp.interfaces" );
Context ctx = new InitialContext( props );
System.out.println("context : " + ctx.getEnvironment());
Object ref = ctx.lookup("StatelessCalculatorBean");
System.out.println("ref = " + ref);
StatelessCalculator calc = (StatelessCalculator) PortableRemoteObject
.narrow(ref, StatelessCalculator.class);

calc.add(5, 2);

} catch (NamingException ne) {

I created jar file out of remote interface and bean class and deployed in jboss 4.2.3 (ie., placed jar file inside /server/default/deploy folder). When I run the client file I am getting the below error.


context : {jnp.parsedName=, java.naming.provider.url=localhost:1099, java.naming.factory.initial=org.jboss.naming.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.namingrg.jboss.naming}
ref = org.jnp.interfaces.NamingContext@1729854
Exception in thread "main" java.lang.ClassCastException
at javax.rmi.PortableRemoteObject.narrow(
at CalculatorEJB3Test.main(
Caused by: java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to org.omg.CORBA.Object
... 2 more

I am getting the same error even if I created InitialContext object without setting any properties. I am not able to find out what is the problem. I am very glad if somebody help me in crossing my initial step. I would request someone to take a minute and provide me the solution.

Thanks in advance,

Reza Rahman
Ranch Hand

Joined: Feb 01, 2005
Posts: 580

Remove the request to narrow the remote object. It is no longer needed in EJB 3.0.

Hope it helps,

Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10033

No more look up to the Remote Home interfaces. Direct on the Remote Interface.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Chinababu Illa

Joined: Jun 04, 2008
Posts: 23
Hi Reza and Jyothi,

Thanks for your reply. The reasons for failure are two things.
1. jndi name was wrong, it expected "StatelessCalculatorBean/remote" instead of "StatelessCalculatorBean".
2. I had to add jar files from "jboss-4.2.3.GA\client" folder from jboss directory.

After I set these two it started working fine. One observation is if I give wrong jndi name for lookup, it's returning "org.jnp.interfaces.NamingContext" object instead of remote object.

Thanks both of you.
I agree. Here's the link:
subject: Starting problem
It's not a secret anymore!