can someone tell me how to call EJB from a java client application?
I know to many people this sounds simple, but frankly the examples out there suck.
Luckily it's fairly simple.
First you must put the Home and Remote implementations as well as any utility classes in their own jar. The EJB 2.0 spec allows for automatic creations (WebLogic 6.1's ejbc tool does this for you) by specifying the ejb-client-jar tag like this:
<ejb-client-jar>client.jar</ejb-client-jar> in the ejb.xml file.
Next put this jar file in your client applications classpath.
Import all of the required classes into your application (J2EE stuff as well as your client side stuff):
Then to actually retrieve it:
Context ctx = new InitialContext();
// name is whatever JNDI name you gave it
Object o = ctx.lookup("home name");
EJBHome ejbHome = (EJBHome) PortableRemoteObject.narrow(o,EJBHome.class);
// This is userID should be the one passed.
EJB ejb = ejbHome.create(..);
Thank you very much.and...
[Next put this jar file in your client applications classpath] means not only deploy this jar in web application server but also copy this jar file to my client machine,doesn't it?
and would you like to show me the whole sample about it?
Yes , even the client program would need the jar.
Some containers like PowerTier also provides a
client application jar which is much thinner and
just carries all the required home and transportation
You need to set your context factory as well as provider
as follows and pass it to initial context.
Hashtable env = new Hashtable();
InitialContext ic = new InitialContext(env);System.out.println("Got Initial context");
EjbTestHome testhome = (EjbTestHome) PortableRemoteObject.narrow(ic.lookup("MyPackage.MyServerName"), EjbTestHome.class);
I also had a trouble when try to compile stand alone client application to access EJB, i already added the jars on the classpath j2ee.jar, HelloWorldJAR.jar(EJB Jar) and HelloWorldEARClient.jar(Stuff return when i deployed the EBJ). And i allways got like this :
HelloWorldClient.java:32: '.' expected
HelloWorldClient.java:33: '.' expected
anybody can tell me what i have to do .....
This is happening because probably your class HelloWorld is in some package(not in an unnamed package). To verify this, extract your client jar and see whether stub/ EJBObject classes generate for HelloWorld component interface are in some package.
Then modify your import statement. it should work.
can somebody tell me, i am invoking the remote object from the java application & it gives me error:
"java.lang.ClassCastException: Could not load a proxy stub. Possible reason is that the context classloader do not has reference to p4 service."
at $Proxy0.create(Unknown Source)
but if i package & the same java class in the same package with ejb . it works fine.
i have checked the ejb is in the class path of my java application