Calling EJB deplyed in Websphere from Standalone Java client

Discussions

EJB programming & troubleshooting: Calling EJB deplyed in Websphere from Standalone Java client

  1. Hi,
    I have deployed a EJB onto Websphere AppServer 5.0.
    here is my code :


    HOME :::::
    package com.cognizant.sample.ejb.interfaces;
    import javax.ejb.EJBHome;
    import java.rmi.RemoteException;
    import javax.ejb.CreateException;

    public interface BoxHome extends EJBHome {

    public Box create() throws CreateException, RemoteException;


    }


    Remote Interface ::::

    package com.cognizant.sample.ejb.interfaces;
    import javax.ejb.EJBObject;
    import java.rmi.RemoteException;
    public interface Box extends EJBObject{

    public int CalcArea (int length,int width) throws RemoteException;
    public void printArea(int length,int width) throws RemoteException;
    public int CalcVolume(int length ,int width,int depth) throws RemoteException;

    }

    BEAN ::::
    package com.cognizant.sample.ejb.impl;

    import java.rmi.RemoteException;

    import javax.ejb.EJBException;
    import javax.ejb.SessionBean;
    import javax.ejb.SessionContext;

    public class BoxBean implements SessionBean {

    public BoxBean() {
    super();

    }

    public void setSessionContext(SessionContext arg0)
    throws EJBException, RemoteException {


    }

    public void ejbRemove() throws EJBException, RemoteException {


    }

    public void ejbActivate() throws EJBException, RemoteException {


    }

    public void ejbPassivate() throws EJBException, RemoteException {


    }

    public int CalcArea (int length,int width) throws RemoteException{
    return length * width;
    }
    public void printArea(int length,int width) throws RemoteException{
    int area=length*width;
    System.out.println("The area of a box of length " +length + " width" + width + " is "+ area);
    }

    public int CalcVolume(int length ,int width,int depth) throws RemoteException{
    return length * width *depth;
    }

    }

    I am trying to access it from a standalone java client program specifying the initial context factory class


    STAND ALONE JAVA-CLIENT PROGRAM :

    package com.cognizant.sample.ejb.clients;


    import java.util.Properties;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.rmi.PortableRemoteObject;
    import com.cognizant.sample.ejb.interfaces.*;
    public class BoxClient {

    public BoxClient() {
    super();

    }

    public static void main(String[] args) {
    try{

    Properties props=new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
    props.put(Context.PROVIDER_URL,"iiop://localhost:900");
    Context ctx=new InitialContext(props);
    Object obj=ctx.lookup("SAMPLEBOX");
    BoxHome boxHome=(BoxHome)PortableRemoteObject.narrow(obj,BoxHome.class);
    Box box=boxHome.create();

    int area=box.CalcArea(1,2);
    System.out.println("Area from client" +area);
    box.printArea(2,3);
    int volume=box.CalcVolume(2,3,4);
    System.out.println("Volume from client" + volume);

    }
    catch(Exception ex){
    ex.printStackTrace();
    }


    }
    }


    I am getting the following exception.
    javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory. Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory

    I included the jar file naming.jar in the class path.Then another exception was thrown asking for jndiprovider.properties. i included namingclient.jar.again another exception was thrown.this process is cascading.

    Can anyone tell me how to write a standalone client program to access the beans in Websphere ?Is my program right ???
    What initialcontext factory and info should i provide ?
    AND MORE IMPORTANTLY,WHAT JARS SHOULD BE IN MY CLASSPATH and what properties files containg which info SHOULD BE IN MY CLASSPATH?????
    If anybody could explain the concept of the contextfactory,it would be very helpful.Pls reply asap.

    Threaded Messages (25)

  2. From all the app servers I have worked with, WebSphere is the one that makes more efforts to complicate even the simplest things. You would face no problems at all to do what you are trying to do in WebLogic or JBoss.

    The J2EE specification is quite vague when defining how J2EE application clients should be packaged or launched. In WebLogic you just need to include "weblogic.jar" (and the EJB interfaces, of course) in your classpath and there you go. JBoss is equally simple (there is a "client" dir with all you need).

    You must go to IBM Infocenter and look for "application clients" documentation. Don't be surprised if you find examples that suggest that you distribute the full EJB-jar to your clients (funny, heh?). You will end up using a "launchClient.bat" to run your client applications: maybe this will simplify the "what JARs do I need" question. And don't waste your time trying to use Sun JVM in your client - try to use IBM's first, until you make it work. Using Sun JVM is possible, but requires more few-documented tweaking, so leave it for now.

    It seems to me that all of IBM samples assume you have a WebSphere installation in your client machine (I may be wrong here). Picking what specific JARs you need may be tricky...
  3. From all the app servers I have worked with, WebSphere is the one that makes more efforts to complicate even the simplest things. You would face no problems at all to do what you are trying to do in WebLogic or JBoss.

    The J2EE specification is quite vague when defining how J2EE application clients should be packaged or launched. In WebLogic you just need to include "weblogic.jar" (and the EJB interfaces, of course) in your classpath and there you go. JBoss is equally simple (there is a "client" dir with all you need).

    You must go to IBM Infocenter and look for "application clients" documentation. Don't be surprised if you find examples that suggest that you distribute the full EJB-jar to your clients (funny, heh?). You will end up using a "launchClient.bat" to run your client applications: maybe this will simplify the "what JARs do I need" question. And don't waste your time trying to use Sun JVM in your client - try to use IBM's first, until you make it work. Using Sun JVM is possible, but requires more few-documented tweaking, so leave it for now.

    It seems to me that all of IBM samples assume you have a WebSphere installation in your client machine (I may be wrong here). Picking what specific JARs you need may be tricky...
    Hi Andre,
          I am facing similar problem when trying to run an application client for WAS 5. When trying to run this particular client code I am getting an error after the last line of the following code block

    -------------------------------------------
    Properties prop = new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
    prop.put(Context.PROVIDER_URL,"iiop:/localhost:900");

    Context ctx = new InitialContext(prop);

    -----------------------------------------------------------------------
    The error is

    Exception in thread "main" java.lang.UnsupportedClassVersionError: com/ibm/CORBA/iiop/ObjectURL (Unsupported major.minor version 48.0)
            at java.lang.ClassLoader.defineClass0(Native Method)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$100(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            at com.ibm.websphere.naming.WsnInitialContextFactory.init_implClassCtor(
    WsnInitialContextFactory.java:170)
            at com.ibm.websphere.naming.WsnInitialContextFactory.getInitialContext(W
    snInitialContextFactory.java:110)
            at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
            at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
            at javax.naming.InitialContext.init(Unknown Source)
            at javax.naming.InitialContext.<init>(Unknown Source)
            at fincalc.stateless.TestClient.main(TestClient.java:26)



    Do you know the reason of this error and how can this be eliminated.

    Thanks

    Regards
    Somya
  4. Hi,

    this error sounds like you are using an incompatible JDK for the IBM Orb Classes. JDK 1.4x uses major version 48 type classes. This looks as if you use an application client for jdk 1.3 with a JDK version 1.4 - or the other way around. For each Appserver Version exist two versions of the application client package. One for JDK 1.3 and the other for 1.4. You have to download the right version for your JDK to get the client working.

    Cheers,
    Philipp
  5. For a WebSphere 5.1 server there is no problem accessing EJb´s from a standalone Java client running Sun JRE 1.4.x:

    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
    env.put(Context.PROVIDER_URL, "iiop://myserver:myport/mypath/");
    InitialContext jndi = new InitialContext(env);
    jndi.lookup(...)
  6. Typically in WAS to WAS EJB JNDI the name is

    ctx.lookup("ejb/UserEJB");

    but for Sun JNDI it is

    ctx.lookup("java:comp/env/com/x/y/ejb/UserEJB");

    What did you use when talking Sun VM client to WAS VM Server?

    Martin
  7. You should add ibmorb.jar present at \Installationroot\IBM\WebSphere Studio\Application Developer\v5.1.2\runtimes\base_v51\java\jre\lib\ext , to your classpath
  8. If you have WSAD installed, create a Application Client module along with the EJB / WEB module and test it with WSAD Test environment and then package it. This will make all necessary things available in the Client jar file so the same can be run with IBM JDK.
  9. advance naming usage[ Go to top ]

    http://www.theserverside.com/discussions/thread.tss?thread_id=23632 kalpesh.soni@perficient.com
  10. Re: advance naming usage[ Go to top ]

    :) http://publib.boulder.ibm.com/infocenter/ieduasst/v1r1m0/index.jsp?topic=/com.ibm.iea.was_v6/was/6.0/Runtime/WASv6_NamingAdvanced/player.html
  11. ejb lookup[ Go to top ]

    try Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.cosnaming.CNCtxFactory"); env.put(Context.PROVIDER_URL,"iiop://localhost:2810"); InitialContext ic = new InitialContext(env); System.out.println("ic=>"+ic); //put server, port 2809, node name , server name, ejb home location correctly Object homeobj = ic.lookup("corbaname:iiop:localhost:2810#cell/nodes/widNode/servers/server1/ejb/test/cache/CacheEJBHome"); //System.out.println("homeobj "+homeobj); CacheEJBHome home = (CacheEJBHome) PortableRemoteObject.narrow(homeobj, CacheEJBHome.class); System.out.println("home created " + home); see http://publib.boulder.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/tnam_develop_naming.html
  12. More Details needed[ Go to top ]

    Andre, I am facing the same problem, and was not able to solve it by adding the library files mentioned in this post to the classpath. Searching in IMB infocenter did not get me the pages you mentioned.

    Could you be more specific which URL you got the information from? is there an IBM page from where i can download all the libraries necessary, and perhaps a sample 'launchclient.bat' file?

    Jake
  13. More Details needed[ Go to top ]

    And IBM Infocenter is
    http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp

    (For those like me not in the know - took quite some time to find it :-) )

    BTW it is, isnt it?

    Jake
  14. thanks, Amit
  15. This is not a 'solution' The ejb client lar file must NOT include websphere _STUB_ shit ! I got this working on Sun AS 8.2 - my client jar had only 1 dependency on Sun AS jar : appserv-rt.jar - that was it. The client jat file had only 2 clases inside: remote and home interfaces. No dependency whatsoever on _Stub_ deployment generated files at all. I am struggling to do the same clean stuff on WAS 6.0.2 - now way - ave to add all the websphere jars on the client path - then got into the class not found : looking for _stub_ . Yeah I know these get gen upon deployment and injected into the ejb-jar. I don't want my clients depende on WAS specific _stubs_. The thing it: WAS is broken when it comes to ejb clients. They do not implement Dynamic Proxy into their classloader-return-EJBInterfaceClass-to-client pattern!
  16. Naveen, if you need more help just post here for it, ok? I'll be watching.
  17. Hi Andre,

    you said that calling an EJB from a non IBM JDK is possible. Have you done this already? If so, could you please provide some more detailed information on that topic? I got my application client running with the IBM JDK but it doesn't seem to work with SUNs.

    Best regards,
    Peter
  18. Hi All
      I am trying to call a ejb which is deployed in the websphere from a standalone application. But i get the same errors as described by naveen. Even i am trying to access database connection which i am getting as null. My client application is in form of jar which has all the necessary classes and jar files. All works fine in wsad but when i create a jar and keep it in C: drive folder and try to execute the jar using batch file and get connection as null and look up fails any idea what's wrong?
  19. JAR Files required for CLasspath[ Go to top ]

    I've included the following JAR files in my classpath (from the WAS install, using WAS 5.1):

    \WebSphere\AppServer\lib\j2ee.jar
    \WebSphere\AppServer\properties
    \WebSphere\AppServer\lib\websphere.jar;
    \WebSphere\AppServer\lib\naming.jar;
    \WebSphere\AppServer\lib\namingclient.jar;
    \WebSphere\AppServer\java\jre\lib\server.jar

    Hope this helps.

    Gary
  20. Hi all,

    Have WSAD 5.1.1 installed on my machine. Can not find following jar files and property files in installation directory.

    \WebSphere\AppServer\lib\j2ee.jar
    \WebSphere\AppServer\properties
    \WebSphere\AppServer\lib\naming.jar;
    \WebSphere\AppServer\lib\namingclient.jar;

    Am assuming that it comes only with WebSphere server. Can anybody tell me from where can download these jar files and property files. Or can somebody send it to knnirmal at kanbay dot com.

    Thanks and Regards,
    Ketan
  21. The problem is with the version of java that is been used. Somebody here pointed that out. Set your path, classpath to these and it will work.
    1.Set the path to JAVA_HOME <your-ibm-installed-directory>/v5.1.2\runtimes\base_v51\java\bin.
    2.set the classpath
    <ibm-installed-dir>\runtimes\base_v51\java\jre\lib\rt.jar;
    <ibm-installed-dir>runtimes\base_v51\properties;
    <ibm-installed-dir>runtimes\base_v51\lib\bootstrap.jar;
    <ibm-installed-dir>\runtimes\base_v51\lib\j2ee.jar;
    <ibm-installed-dir>\runtimes\base_v51\lib\lmproxy.jar;
    <ibm-installed-dir>\runtimes\base_v51\lib\naming.jar;
    <ibm-installed-dir>\runtimes\base_v51\lib\namingclient.jar;
    <ibm-installed-dir>\runtimes\base_v51\lib\namingserver.jar;
    <ibm-installed-dir>\runtimes\base_v51\lib\websphere.jar;
    <ibm-installed-dir>\runtimes\base_v51\lib\server.jar;

    3.Remove any bootstrap specific host, ports from the -D


    Hope this helps.
  22. Set Classpath[ Go to top ]

    set classpath of
    \WebSphere\AppServer\lib\naming.jar
    \WebSphere\AppServer\lib\namingclient.jar
    \WebSphere\AppServer\properties
     from their directory. (Dont copy the jars to another Directory) and use IBM JDK. It works.
  23. The list of jars that worked[ Go to top ]

    I faced the same problem without any useful help from anywhere. I guess you can't really understand websphere problems until you face it yourself! Finally after a lot of trial and error..the list of jars that got it to work. com.ibm.mq.jar com.ibm.mqjms.jar ecutils.jar idl.jar j2ee.jar lmproxy.jar naming.jar namingclient.jar namingserver.jar ras.jar sas.jar wsexception.jar wssec.jar Please note: 1. There are a couple of JARs related to MQJMS that you may not need 2. I got it to work by using the jars from WSAD base_v5\lib. It did not work when I used the jars from the Application Server or the Deployment Manager!!
  24. Hi.. I want to access EJB deployed on Websphere from a standalone java application. Did any one get it working? I tried to use IBM specific jars but it did not help too. Finally I got it working using IBM JRE. But I am keen to know whether we can do it using Sun JRE. kindly reply at sumanthkumar_s at hotmail dot com. Thanks & Regards, Sumanth
  25. Did any one finally get it working![ Go to top ]

    That too using Context.INITIAL_CONTEXT_FACTORY = com.ibm.websphere.naming.WsnInitialContextFactory not com.sun.jndi.cosnaming.CNCtxFactory Johan, Can you please mention what is 'mypath' in 'iiop://myserver:myport/mypath/'? Best Regards, Sumanth
  26. I got it working finally[ Go to top ]

    This is an example of running a standalone java client that invokes a method in an EJB deployed in Websphere 6: This is the client code: ...public class App.java ... ...main... props.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); props.put(Context.PROVIDER_URL, "iiop://10.10.0.37:9100"); InitialContext ic = new InitialContext(props); Object objRef = ic.lookup("testapp/ejb/TestRmiEjb"); TestRmiEjbHome home = (TestRmiEjbHome)PortableRemoteObject.narrow(objRef, TestRmiEjbHome.class); TestRmiEjb testRmiEjb = home.create(); System.out.println(testRmiEjb.calcSum returned: " + testRmiEjb.calcSum(4, 3)); I installed Websphere 6 on my computer so that I have IBM JDK and a lot of required IBM JAR files. I put the following folder in the beginning of the path system environment variable: E:\Program Files\IBM\WebSphere\AppServer\java\bin. The following is the compilation script: "E:\Program Files\IBM\WebSphere\AppServer\java\bin\javac" -classpath testapp-ejb-client.jar;"E:\Program Files\IBM\WebSphere\AppServer\lib\naming.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\ras.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\wsexception.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\bootstrap.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\emf.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\j2ee.jar"; com/testrmi/App.java (App.java is the client mentioned above) The following is the running script: "E:\Program Files\IBM\WebSphere\AppServer\java\bin\java" -cp testapp-ejb-client.jar;"E:\Program Files\IBM\WebSphere\AppServer\lib\naming.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\ras.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\wsexception.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\bootstrap.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\emf.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\ffdc.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\namingclient.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\ecutils.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\iwsorb.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\idl.jar";"E:\Program Files\IBM\WebSphere\AppServer\lib\j2ee.jar"; com.testrmi.App testapp-ejb-client.jar (the first entry in the classpath) is tricky, though. I needed to add the _TestRmiEjb_Stub.class and _TestRmiEjbHome_Stub.class to the correct place in it (which is in the same package where the EJB respective interfaces reside). These class files only existed in the deployed JAR file, after deploying it in Websphere. AAT didn't create them, the deployment to Websphere did. HTH someone, and that I didn't forget any of the nitty gritty details I went through, Amit