aspose file tools*
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
Author

Starting problem

Chinababu Illa
Greenhorn

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;

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

Bean Class:

package com.calculator;

import javax.ejb.Stateless;

@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) {
ne.printStackTrace();
}
}
}


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.

Output:

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 com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at CalculatorEJB3Test.main(CalculatorEJB3Test.java:28)
Caused by: java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to org.omg.CORBA.Object
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
... 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,
Chinna

Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
Chinna,

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

Hope it helps,
Reza


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: 9243
    
    1

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


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Chinababu Illa
Greenhorn

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.
Chinna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Starting problem
 
Similar Threads
EJB3 Client Not Running
whats wrong with this client ?
Mikalai Zaikin SCBCD5.0 example problem
Exception in thread Exception in thread "main" java.lang.ClassCastException
javax.naming.Reference cannot be cast to org.omg.CORBA.Object