wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Problem with jndi lookup for remote bean in EJB 3.x Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Problem with jndi lookup for remote bean in EJB 3.x" Watch "Problem with jndi lookup for remote bean in EJB 3.x" New topic
Author

Problem with jndi lookup for remote bean in EJB 3.x

Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
Hello,

I am trying to access Remote Stateless bean through a different JAVA application.

My RemoteStateless.java file

package com.ejb;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;

@Stateless
@EJB(name="RemoteStatelesss", mappedName="RemoteStateless", beanInterface=RemoteStatelessRemote.class)


public class RemoteStateless implements RemoteStatelessRemote {


private String name;

public String getName(){
return name;
}

public void setName(String name) {
this.name = name;
}
}


interface RemoteStatelessRemote.java

package com.ejb;

import javax.ejb.Remote;


@Remote
public interface RemoteStatelessRemote {
void setName(String a);
String getName();
}

Client - a JAVA Application


package clientejbtypes;

import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class Main {

public static void main(String[] args) {

String str ="Hello";
try {
System.out.println("HELLO");
Context ic = new InitialContext();
Object o = ic.lookup("java:global/EJBTypes/EJBTypes-ejb/RemoteStateless!com.ejb.RemoteStatelessRemote");
com.ejb.RemoteStateless Rsl = (com.ejb.RemoteStateless) PortableRemoteObject.narrow(o, com.ejb.RemoteStatelessRemote.class);
Rsl.setName("Sonal Popat");
System.out.println("Hello " +Rsl.getName());
}
catch(Exception ex)
{
ex.printStackTrace();
}

}
}

Here is the output... it gives me runtime error.

HELLO
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
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at clientejbtypes.Main.main(Main.java:30)

Any pointers on this will be of great help.

Thanks,
- Sonal


SCJP 6.0 93%
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
Use your server specification to set environment variable or write a method to for InitialContext(hashtable) implementation.
Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
Here I am using Glassfish V3 and

loosely following this, there is no need to specify properties.

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB

I have changed nothing except adding gf-client.jar and appserv-rt.jar in classpath

I am getting new error...

Output is as follows:

HELLO
May 10, 2010 5:58:57 PM com.sun.enterprise.transaction.JavaEETransactionManagerSimplified initDelegates
INFO: Using com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate as the delegate
java.lang.ClassCastException
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:258)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at com.sun.enterprise.naming.impl.SerialContext.narrowProvider(SerialContext.java:359)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:327)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at clientejbtypes.Main.main(Main.java:39)
Caused by: java.lang.ClassCastException: Object is not of remote type com.sun.enterprise.naming.impl.SerialContextProvider
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:250)
... 7 more
javax.naming.NamingException: Lookup failed for 'ejb/com.ejb.RemoteStateless' in SerialContext ,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext ,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is java.lang.ClassCastException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at clientejbtypes.Main.main(Main.java:39)
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext ,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is java.lang.ClassCastException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 2 more
Caused by: java.lang.ClassCastException
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:258)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at com.sun.enterprise.naming.impl.SerialContext.narrowProvider(SerialContext.java:359)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:327)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
... 3 more
Caused by: java.lang.ClassCastException: Object is not of remote type com.sun.enterprise.naming.impl.SerialContextProvider
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:250)
... 7 more

Sachin Chandra
Greenhorn

Joined: Jun 19, 2009
Posts: 7
Hi,

The name to the look up object is depend on the server. So please change the look up code as below.


com.ejb.RemoteStateless Rsl = (RemoteStateless)ctx.lookup("java:comp/env/RemoteStateless");

Also try with the package name of the lookup object.

com.ejb.RemoteStateless Rsl = (RemoteStateless)ctx.lookup("java:comp/env/com.ejb.RemoteStateless");

or

com.ejb.RemoteStateless Rsl = (RemoteStateless)ctx.lookup("com.ejb.RemoteStateless");

Thanks

Sachin CR


Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
Hello Sachin,

Thanks for your help. But its not working even after trying all possible combinations with ctx.lookup , infact its just the same run time error.

Even, I tried to set Property for that,

Properties jndiProps = new Properties();
jndiProps.put("java.naming.factory.initial", "com.sun.enterprise.naming.impl.SerialInitContextFactory");
jndiProps.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
jndiProps.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
jndiProps.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
jndiProps.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

Context ctx = new InitialContext(jndiProps);

then I am getting this error.

javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at clientejbtypes.Main.main(Main.java:37)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
... 4 more

Any help on this would be of great help.

Thanks,
- Sonal
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
It clearly says

Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory..

follow this http://forums.java.net/jive/thread.jspa?threadID=53411
Sachin Chandra
Greenhorn

Joined: Jun 19, 2009
Posts: 7
Hi,
Add all jars from glassfish modules directory into the lib of your stand alone client.

Thanks

Sachin CR

Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
Hello,

Thanks Sachin and Nitin,

So far I've added gf-client.jar, appserv-rt.jar files and EJBTpes-ejb.jar (jar file having RemoteStateless EJB which I'm trying to lookup) file in to the library of my JAVA client application.

this is the sun-ejb-jar.xml file
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>RemoteStateless</ejb-name>
<jndi-name>RemoteStateless</jndi-name>
</ejb>
</enterprise-beans>
</sun-ejb-jar>

entry in to ejb-jar.xml file is as follows
<enterprise-beans>
<session>
<description> Remote EJB </description>
<display-name> Remote EJB </display-name>
<ejb-name>RemoteStateless</ejb-name>
<remote>com.ejb.RemoteStateless</remote>
<ejb-class>com.ejb.RemoteStateless</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>

I've tried to lookup with all the different combinations

this is the entry of RemoteStateless EJB in server.log file

[#|2010-05-12T11:46:29.175-0700|INFO|glassfishv3.0|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=19;_ThreadName=Thread-1;|Portable JNDI names for EJB RemoteStateless : [java:global/EJBTypes/EJBTypes-ejb/RemoteStateless, java:global/EJBTypes/EJBTypes-ejb/RemoteStateless!com.ejb.RemoteStatelessRemote]|#]

[#|2010-05-12T11:46:30.127-0700|INFO|glassfishv3.0|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=19;_ThreadName=Thread-1;|Glassfish-specific (Non-portable) JNDI names for EJB RemoteStateless : [RemoteStateless#com.ejb.RemoteStatelessRemote, RemoteStateless]|#]

and then also I'm getting this error

May 12, 2010 11:57:16 AM com.sun.enterprise.transaction.JavaEETransactionManagerSimplified initDelegates
INFO: Using com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate as the delegate
java.lang.ClassCastException
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:258)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at com.sun.enterprise.naming.impl.SerialContext.narrowProvider(SerialContext.java:359)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:327)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at clientejbtypes.Main.main(Main.java:38)
Caused by: java.lang.ClassCastException: Object is not of remote type com.sun.enterprise.naming.impl.SerialContextProvider
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:250)
... 7 more
javax.naming.NamingException: Lookup failed for 'RemoteStateless' in SerialContext ,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext ,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is java.lang.ClassCastException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at clientejbtypes.Main.main(Main.java:38)
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext ,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is java.lang.ClassCastException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 2 more
Caused by: java.lang.ClassCastException
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:258)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at com.sun.enterprise.naming.impl.SerialContext.narrowProvider(SerialContext.java:359)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:327)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
... 3 more
Caused by: java.lang.ClassCastException: Object is not of remote type com.sun.enterprise.naming.impl.SerialContextProvider
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:250)
... 7 more

Any idea what is wrong with my code.

Thanks,
- Sonal
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
I think your client should

com.ejb.RemoteStatelessRemote Rsl = (com.ejb.RemoteStatelessRemote) PortableRemoteObject.narrow(o, com.ejb.RemoteStatelessRemote.class);
Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
Hello,


I tried com.ejb.RemoteStatelessRemote Rsl = (com.ejb.RemoteStatelessRemote) PortableRemoteObject.narrow(o, com.ejb.RemoteStatelessRemote.class); doesnt work that way too

Here is one more example, which is of Asynch Session bean but the same thing for JNDI lookup

http://paddyweblog.blogspot.com/2010/04/ejb-31-asynchronous-session-beans.html

but this is also does not work and it gives the same error
May 12, 2010 1:55:55 PM com.sun.enterprise.transaction.JavaEETransactionManagerSimplified initDelegates
INFO: Using com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate as the delegate
java.lang.ClassCastException
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:258)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at com.sun.enterprise.naming.impl.SerialContext.narrowProvider(SerialContext.java:359)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:327)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at ClientMain.main(ClientMain.java:27)
Caused by: java.lang.ClassCastException: Object is not of remote type com.sun.enterprise.naming.impl.SerialContextProvider
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:250)
... 7 more
javax.naming.NamingException: Lookup failed for 'java:global/AsynchEJB/HelloEjbAsynchronous' in SerialContext targetHost=localhost,targetPort=3700,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext targetHost=localhost,targetPort=3700,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is java.lang.ClassCastException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at ClientMain.main(ClientMain.java:27)
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext targetHost=localhost,targetPort=3700,orb'sInitialHost=localhost,orb'sInitialPort=3700 [Root exception is java.lang.ClassCastException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 2 more
Caused by: java.lang.ClassCastException
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:258)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at com.sun.enterprise.naming.impl.SerialContext.narrowProvider(SerialContext.java:359)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:327)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
... 3 more
Caused by: java.lang.ClassCastException: Object is not of remote type com.sun.enterprise.naming.impl.SerialContextProvider
at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:250)
... 7 more

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
How about if you just run with lookup. is it working till that point? I have never used glassfish but in jboss we can get the lookup name, there should be a way to figure out what is the name you have to look up in JNDI. I doubt that lookup is not right.
Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
Hello Nitin,

No it doesnt execute any line after the lookup. As, problem occurs at the line of lookup only
whatever parameter I am passing in the lookup, its end up with the same java.lang.ClassCastException problem.

I've tried MDB, it works but even for that I had to make connectionFactory under JMS resources, on the Glassfish server.
So, here also I guess, I've to make entry of this Remote EJB somewhere on Glassfish server. But where should I?

Or this ClassCastException problem is because of some other reason???

Thanks,
- Sonal
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
How did you deployed your project in glassfish? do you have any .ear file or any ant script to do that.
Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
Yeah, my deployed Remote EJB project is ear file and it is on the glassfish.
Infact, when I click on the deploy it automatically makes an entry in the application section of Glassfish server.

In my clientApp, I am including jar file of EJB module. So I am able to instantiate and then access the methods of the Remote EJB from Client App but not able to access through JNDI lookup.

Direct instantiation works ...

com.ejb.RemoteStateless Rsl = new com.ejb.RemoteStateless();
Rsl.setName("Hello"+str);
System.out.println("Value is = +Rsl.getName());


But,

Context ctx = new InitialContext(); //even I've tried InitialContext ctx = new InitialContext();
com.ejb.RemoteStateless Rsl = (com.ejb.RemoteStateless) ctx.lookup("com.ejb.RemoteStateless");

Doesnt work

I've tried almost all possible lookup

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

Sonal Popat wrote:
com.ejb.RemoteStateless Rsl = (com.ejb.RemoteStateless) ctx.lookup("com.ejb.RemoteStateless");




That's doesn't look like the correct JNDI name. Use the tool provided by your application server to view the JNDI tree and see what JNDI name you should be using. Looking at the logs, it probably is:



[My Blog] [JavaRanch Journal]
Sonal Popat
Greenhorn

Joined: May 10, 2010
Posts: 9
It didnt work that way too
Christian Herzog
Greenhorn

Joined: Jan 25, 2011
Posts: 1
Sonal Popat wrote:It didnt work that way too


Did you find any solution to this problem? We have exactly the same problem, when trying to call an EJB on Glassfish v2.1 from Glassfish v3.0.1. There are differences between the classes needed for an EJB client between v2.1 and v3.0.1, and the client classes for v3.0.1 is already loaded on the v3.0.1 server when looking up the EJB on v2.1.

Specifically, it seems that on lookup the 2.1 server returns an object of the class com.sun.enterprise.naming.SerialContextProvider, while the 3.0.1 client classes expects
com.sun.enterprise.naming.impl.SerialContextProvider and this causes a ClassCastException.

I find it hard to accept that it is impossible to call EJBs on Glassfishv2.1 from Glassfish v3.0.1, so I really hope that someone has a solution for this problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with jndi lookup for remote bean in EJB 3.x
 
Similar Threads
problem with stateless session bean
Problem with code
javax.naming.NoInitialContextException while Running the client
JBoss5.1 EJB3 JSF1.2 integration problem
JBOSS - need help with my first ejb deployment and testing