Solved

java.lang.ClassCastException in Servlet on J2EE Sun Appserver

Posted on 2004-05-01
21
520 Views
Last Modified: 2013-11-24
I'm new to programming with EJB's and I'm having a problem that I need solved by Monday.

I've developed a basic stateful EJB to act as a simple shopping cart.  It works perfectly with a normal client, however when I attempt to use a servlet with the EJB I get the following error (from the server log):

java.lang.ClassCastException
        at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
        at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
        at CartServlet.doGet(Unknown Source)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:284)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAsPrivileged(Subject.java:500)
        at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
        at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:200)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
        at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:99)
        at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:563)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:256)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:210)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:563)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:196)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:175)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:577)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
        at com.sun.enterprise.webservice.EjbWebServiceValve.invoke(EjbWebServiceValve.java:134)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
        at com.sun.enterprise.security.web.SingleSignOn.invoke(SingleSignOn.java:254)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
        at com.sun.enterprise.web.VirtualServerValve.invoke(VirtualServerValve.java:209)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:563)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at com.sun.enterprise.web.VirtualServerMappingValve.invoke(VirtualServerMappingValve.java:166)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:563)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:974)
        at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:207)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:648)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:498)
        at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:575)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:649)
        at java.lang.Thread.run(Thread.java:534)

The section of the code in the servlet is pretty standard:

    Context initial = new InitialContext();
    Object objref = initial.lookup("java:comp/env/ejb/CartEJB");

    CartHome home = (CartHome)PortableRemoteObject.narrow(objref,CartHome.class);

    Cart shoppingCart = home.create();


The above code is in the initial doGet of the servlet.  The servlet functions properly otherwise.

-- web.xml --
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>ChrisCart</display-name>
  <servlet>
    <display-name>ChrisCart</display-name>
    <servlet-name>ChrisCart</servlet-name>
    <servlet-class>CartServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ChrisCart</servlet-name>
    <url-pattern>/ChrisCart</url-pattern>
  </servlet-mapping>
  <ejb-ref>
    <ejb-ref-name>ejb/CartEJB</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <home>CartHome</home>
    <remote>Cart</remote>
  </ejb-ref>
</web-app>


-- ejb-jar.xml --
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
  <display-name>ChrisCartJAR</display-name>
  <enterprise-beans>
    <session>
      <display-name>ChrisCartEJB</display-name>
      <ejb-name>CartEJB</ejb-name>
      <home>CartHome</home>
      <remote>Cart</remote>
      <ejb-class>CartBean</ejb-class>
      <session-type>Stateful</session-type>
      <transaction-type>Container</transaction-type>
      <security-identity>
        <use-caller-identity/>
      </security-identity>
    </session>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>CartEJB</ejb-name>
        <method-intf>Remote</method-intf>
        <method-name>getTitles</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
    <container-transaction>
      <method>
        <ejb-name>CartEJB</ejb-name>
        <method-intf>Remote</method-intf>
        <method-name>getPrices</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
    <container-transaction>
      <method>
        <ejb-name>CartEJB</ejb-name>
        <method-intf>Remote</method-intf>
        <method-name>getQuantity</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
    <container-transaction>
      <method>
        <ejb-name>CartEJB</ejb-name>
        <method-intf>Remote</method-intf>
        <method-name>getNumberofItems</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
    <container-transaction>
      <method>
        <ejb-name>CartEJB</ejb-name>
        <method-intf>Remote</method-intf>
        <method-name>addItem</method-name>
        <method-params>
          <method-param>java.lang.String</method-param>
          <method-param>java.lang.double</method-param>
        </method-params>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
    <container-transaction>
      <method>
        <ejb-name>CartEJB</ejb-name>
        <method-intf>Remote</method-intf>
        <method-name>removeItem</method-name>
        <method-params>
          <method-param>java.lang.String</method-param>
          <method-param>java.lang.double</method-param>
        </method-params>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>


-- application.xml --
<?xml version="1.0" encoding="UTF-8"?>
<application version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
  <description>Application description</description>
  <display-name>ChrisCart</display-name>
  <module>
    <web>
      <web-uri>ChrisCart.war</web-uri>
      <context-root>ChrisCart</context-root>
    </web>
  </module>
  <module>
    <ejb>ChrisCartEjb.jar</ejb>
  </module>
</application>

Any help would be greatly appreciated.  Also, if you need any additional code segments, etc. . .  I'll post them on request.
0
Comment
Question by:ckshort
  • 9
  • 8
  • 2
  • +1
21 Comments
 
LVL 9

Expert Comment

by:keteracel
ID: 10968782
Without the whole code it is difficult to debug this, however:

I think the problem may lie with this line:

CartHome home =(Cart Home)PortableRemoteObject.narrow(objref,CartHome.class);

try putting a try-catch block around this line like so:

CartHome home;

try {
    home = (Cart Home)PortableRemoteObject.narrow(objref,CartHome.class);
} catch (ClassCastException e) {
    System.out.println(e.getMessage());
    e.printStackTrace();
    System.exit(1);
}
0
 
LVL 9

Expert Comment

by:keteracel
ID: 10968783
oh yeah, if this catches the Exception you know that's the offending line.
0
 
LVL 7

Expert Comment

by:lhankins
ID: 10968799
In your code, does CartHome properly extend EJBHome...?   If this inheritance declaration is missing, it might cause the ClassCastException (since PortableRemoteObject.narrow is going to try and cast the stub to an appropriate ancestor of EJBHome).

If that's not your problem, can you tell me :

   1) What JDK are you using...?
   2) What AppServer are you using...?
   3) Are you deploying as an EAR which contains the WAR + EJB-Jars  or as standalone WAR  + standalone EJB-Jars

-Lance

0
 

Author Comment

by:ckshort
ID: 10969074
I'm using the following:

JDK: 1.4.2
App Server: J2EE(TM) Application Server 1.4 Developer Release (build b44-dr)


I'm deploying it as one whole item.  The EAR should contain the EJB-Jars and the WAR.  I might have done that wrong though.  I've included the build.xml file at the bottom. I had modified it from the build.xml file I used to test the EJB w/ a console based client.

Here's the full code of the servlet:

-- CartServlet.java --

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import tools.*;

public class CartServlet extends HttpServlet
{

  Cart shoppingCart;

  protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
  {
   try {
    res.setContentType("text/html");

    /*Create the CartBean Instance*/

    Context initial = new InitialContext();
    Object objref = initial.lookup("java:comp/env/ejb/CartEJB");


    PrintWriter out = res.getWriter();
    out.print("<HTML><HEAD><TITLE>Chris\'s Shopping Cart</TITLE>"+
              "</HEAD><BODY><DIV ALIGN=CENTER><TABLE WIDTH=600 BORDER=1>"+
              "<TR>\n <TD WIDTH=125 ALIGN=CENTER>\n   <BR>"+
              "   <FORM METHOD=POST>"+
              "    <INPUT TYPE=SUBMIT NAME=show "+
              "VALUE=\"Show Data\"><br><br>"+
              "    <INPUT TYPE=SUBMIT NAME=display "+
              "VALUE=\"Display Cart\">"+
              "   </FORM> </TD> <TD WIDTH=475>"+
              "  &nbsp;"+
              "</TD></TR></TABLE></BODY></HTML>");
    out.close();
    CartHome home = (CartHome)PortableRemoteObject.narrow(objref,CartHome.class);

    Cart shoppingCart = home.create();
   } catch (Exception ex) {
    System.err.println("Caught an unexpected exception!");
    ex.printStackTrace();
   }

  }
  protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
  {
   try {
    String msg = "";
    String tmp = "";
    double tmpp = 0.0;
    Connection con = null;
    Statement stmt = null;
    int i = 0;
    String[] name = new String[1000];
    double[] price = new double[1000];
    int[] quantity = new int[1000];
    int totalitems;

    String QueryString = "select * from dataitems";
    PrintWriter dataout = res.getWriter();

    if(req.getParameter("show") != null)
    {
      try
      {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        con = DriverManager.getConnection ("jdbc:mysql://localhost/hw2", "ckshor01", "startrek");
        GetDataStatement = con.prepareStatement(QueryString);
        ResultSet rs2 = GetDataStatement.executeQuery();
        dataout.print("<HTML><HEAD><TITLE>Chris\'s Shopping Cart</TITLE>"+
                      "</HEAD><BODY><DIV ALIGN=CENTER><TABLE WIDTH=600 BORDER=1>"+
                      "<TR>\n <TD WIDTH=125 ALIGN=CENTER>\n   <BR>"+
                      "   <FORM METHOD=POST>"+
                      "    <INPUT TYPE=SUBMIT NAME=show "+
                      "VALUE=\"Show Data\"><br><br>"+
                      "    <INPUT TYPE=SUBMIT NAME=display "+
                      "VALUE=\"Display Cart\">"+
                      "   </FORM> </TD> <TD WIDTH=475>");
        while(rs2.next())
        {
          dataout.print("<FORM METHOD=POST><TABLE BORDER=0><TR><TD WIDTH=150>");
          dataout.print(rs2.getString("name"));
          dataout.print("<INPUT TYPE=HIDDEN NAME=ItemName VALUE=\"");
          dataout.print(rs2.getString("name"));
          dataout.print("\"></TD><TD WIDTH=150>");
          dataout.print(rs2.getString("price"));
          dataout.print("<INPUT TYPE=HIDDEN NAME=ItemPrice VALUE=\"");
          dataout.print(rs2.getString("price"));
          dataout.print("\"></TD><TD><INPUT TYPE=SUBMIT NAME=Add VALUE=Add>");
          dataout.print("</TD></TR></TABLE></FORM>");
        }
        dataout.print("</TD></TR></TABLE>");
      } catch (Exception ex) {
          System.out.println("Failed to retrieve data!");
          System.out.println(ex);
      }
    }
    else if(req.getParameter("display") != null)
    {
      totalitems = shoppingCart.getNumberofItems();
      dataout.print("<HTML><HEAD><TITLE>Chris\'s Shopping Cart</TITLE>"+
                    "</HEAD><BODY><DIV ALIGN=CENTER><TABLE WIDTH=600 BORDER=1>"+
                    "<TR>\n <TD WIDTH=125 ALIGN=CENTER>\n   <BR>"+
                    "   <FORM METHOD=POST>"+
                    "    <INPUT TYPE=SUBMIT NAME=show "+
                    "VALUE=\"Show Data\"><br><br>"+
                    "    <INPUT TYPE=SUBMIT NAME=display "+
                    "VALUE=\"Display Cart\">"+
                    "   </FORM> </TD> <TD WIDTH=475>"+
                    "<TABLE><TR><TD WIDTH=150>Item Name</TD><TD WIDTH=150>"+
                    "Item Price</TD></TR></TABLE>");
      if(totalitems == 0)
      {
        dataout.print("No items in cart");
      }
      else if(totalitems > 0)
      {
        name = shoppingCart.getTitles();
        price = shoppingCart.getPrices();
        quantity = shoppingCart.getQuantity();
        i = 0;
        for(i = 0; i < totalitems; i++)
        {
          dataout.print("<FORM><TABLE BORDER=0><TR><TD WIDTH=40%>");
          dataout.print(name[i]);
          dataout.print("<INPUT TYPE=HIDDEN NAME=ItemName VALUE=\"");
          dataout.print(name[i]);
          dataout.print("\"></TD><TD WIDTH=20%>");
          dataout.print(price[i]);
          dataout.print("<INPUT TYPE=HIDDEN NAME=ItemPrice VALUE=\"");
          dataout.print(price[i]);
          dataout.print("\"></TD><TD WIDTH=20%>");
          dataout.print("<INPUT TYPE=SUBMIT NAME=Add VALUE=Add>");
          dataout.print("</TD><TD WIDTH=20%>");
          dataout.print("<INPUT TYPE=SUBMIT NAME=Remove VALUE=Remove>");
          dataout.print("</TR></TABLE></FORM>");
        }
      }
      dataout.print("</TD></TR></TABLE>");
    }
    else if(req.getParameter("Add") != null)
    {
      tmp = (String)req.getParameter("ItemName");
      tmpp = Double.parseDouble(req.getParameter("ItemPrice"));
      shoppingCart.addItem(tmp, tmpp);
      dataout.print("<HTML><HEAD><TITLE>Chris\'s Shopping Cart</TITLE>"+
                    "</HEAD><BODY><DIV ALIGN=CENTER><TABLE WIDTH=600 BORDER=1>"+
                    "<TR>\n <TD WIDTH=125 ALIGN=CENTER>\n   <BR>"+
                    "   <FORM METHOD=POST>"+
                    "    <INPUT TYPE=SUBMIT NAME=show "+
                    "VALUE=\"Show Data\"><br><br>"+
                    "    <INPUT TYPE=SUBMIT NAME=display "+
                    "VALUE=\"Display Cart\">"+
                    "   </FORM> </TD> <TD WIDTH=475>");
      dataout.print("The item "+tmp+" valued "+
                    "at "+tmpp+" has been added"+
                    " to your cart.");
      dataout.print("</TD></TR></TABLE>");
    }
    else if(req.getParameter("Remove") != null)
    {
      tmp = (String)req.getParameter("ItemName");
      tmpp = Double.parseDouble(req.getParameter("ItemPrice"));
      shoppingCart.removeItem(tmp, tmpp);
      dataout.print("<HTML><HEAD><TITLE>Chris\'s Shopping Cart</TITLE>"+
                    "</HEAD><BODY><DIV ALIGN=CENTER><TABLE WIDTH=600 BORDER=1>"+
                    "<TR>\n <TD WIDTH=125 ALIGN=CENTER>\n   <BR>"+
                    "   <FORM METHOD=POST>"+
                    "    <INPUT TYPE=SUBMIT NAME=show "+
                    "VALUE=\"Show Data\"><br><br>"+
                    "    <INPUT TYPE=SUBMIT NAME=display "+
                    "VALUE=\"Display Cart\">"+
                    "   </FORM> </TD> <TD WIDTH=475>");
      dataout.print("The item "+tmp+" valued "+
                    "at "+tmpp+" has been reduced in quantity"+
                    " in your cart.");
      dataout.print("</TD></TR></TABLE>");
    }

    res.setContentType("text/html");
    res.setHeader("pragma", "no-cache");
    PrintWriter out = res.getWriter();
    out.print("<HR><A HREF=\"");
    out.print(req.getRequestURI());
    out.print("\">Back to the shop</A></BODY></HTML>");
    out.close();
   } catch (ItemException ex) {
    System.err.println("Caught an ItemException: " + ex.getMessage());
   } catch (Exception ex) {
    System.err.println("Caught an unexpected exception!");
    ex.printStackTrace();
   }
  }

  private PreparedStatement GetDataStatement;
}

-- build.xml --
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [ <!ENTITY include SYSTEM "../../../../common.xml"> ]>

  <!-- ======================================================= -->
  <!--  SunOne Application Server Sample Application build xml -->
  <!-- ======================================================= -->

  <project name="cart" default="core" basedir=".">

  <!-- ======================================================= -->
  <!--                   App name-dependent settings.                              -->
  <!-- ======================================================= -->

  <property name="appname" value="ChrisCart"/>
  <property name="cart.home" value="../../../../"/>
  <property name="cart.name" value="CartServlet"/>

  <!-- ======================================================= -->
  <!--                   Package directory structures of interest.         -->
  <!-- ======================================================= -->

  <property name="servlet.pkg" value="."/>
  <property name="jar.pkg" value="."/>

  <!-- ======================================================= -->
  <!--                  Java Doc properties .                                          -->
  <!-- ======================================================= -->
  <property name="cart.name" value="cart"/>
  <property name="javadoc.pkgnames" value="*" />

  <!-- ======================================================= -->
  <!--                  Xml files properties .                                         -->
  <!-- ======================================================= -->

  <property name="warDD" value="web.xml,sun-web.xml"/>
  <property name="jarDD" value="ejb-jar.xml,sun-ejb-jar.xml"/>

  <!-- ======================================================= -->
  <!--                          Include common.xml                                             -->
  <!-- ======================================================= -->

  &include;

  <!-- ======================================================= -->
  <!--          Tagets to build and deploy sample application      -->
  <!-- ======================================================= -->

  <target name="clean" depends="clean_common"/>
  <target name="init" depends="init_common">
      <condition property="deploy.file" value="${assemble.ear}/${ear}">
        <available file="${assemble.ear}/${ear}"/>
    </condition>
    <property name="deploy.file" value="../${ear}"/>

    <condition property="verify.file" value="${assemble.ear}/${ear}">
        <available file="${assemble.ear}/${ear}"/>
    </condition>
    <property name="verify.file" value="../${ear}"/>
  </target>
  <target name="copy_ear" depends="init">
      <delete file="../${ear}"/>
      <copy file="${assemble.ear}/${ear}" todir=".." />
  </target>
  <target name="compile" depends="compile_common" />
  <target name="javadocs" depends="javadocs_common" />
  <target name="deploy" depends="init, deploy_common"/>
  <target name="undeploy" depends="undeploy_common"/>
  <target name="verify" depends="init, verify_common"/>

  <target name="jar" depends="init,create_ejbjar_common"/>
  <target name="war" depends="init,create_war_common"/>
  <target name="ear" depends="init,jar,war,create_ear_common"/>
  <target name="core" depends="compile,jar,ear,war" />
  <target name="all" depends="core,javadocs,verify,deploy"/>

</project>
0
 

Author Comment

by:ckshort
ID: 10969078
And CartHome does extend EJBHome.
0
 
LVL 7

Expert Comment

by:lhankins
ID: 10969114
can you post the CartHome interface...?
0
 

Author Comment

by:ckshort
ID: 10969240
-- CartHome.java --

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface CartHome extends EJBHome {

    Cart create() throws RemoteException, CreateException;

}

-- Cart.java --
import java.util.*;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import tools.ItemException;

public interface Cart extends EJBObject {

   public void addItem(String title, double price) throws RemoteException;

   public void removeItem(String title, double price) throws ItemException, RemoteException;

   public String[] getTitles() throws RemoteException;

   public double[] getPrices() throws RemoteException;

   public int[] getQuantity() throws RemoteException;

   public int getNumberofItems() throws RemoteException;

}
0
 
LVL 7

Expert Comment

by:lhankins
ID: 10969319

Does the build process produce a jar which contains the client side stubs for your the EJB's...?   (You would've had to include this jar in the classpath for your standalone java client as well)...

If that's the case, make sure your WAR has access to these same classes.   This is usually done by putting the jar in the War file's WEB-INF/lib directory (or the classes in its WEB-INF/classes directory).

0
 

Author Comment

by:ckshort
ID: 10969591
Ok, how do I double-check that?  

This is the current directory structure:
ChrisCart
.
|-- ChrisCartClient.jar
|-- ChrisCartEjb_jar
|   |-- Cart.class
|   |-- CartBean.class
|   |-- CartHome.class
|   |-- CartServlet.class
|   |-- META-INF
|   |   |-- MANIFEST.MF
|   |   |-- ejb-jar.xml
|   |   `-- sun-ejb-jar.xml
|   `-- tools
|       `-- ItemException.class
|-- ChrisCart_war
|   |-- Cart.class
|   |-- CartBean.class
|   |-- CartHome.class
|   |-- CartServlet.class
|   |-- META-INF
|   |   |-- MANIFEST.MF
|   |   |-- ejb-jar.xml
|   |   `-- sun-ejb-jar.xml
|   |-- WEB-INF
|   |   |-- lib
|   |   |   `-- ChrisCartEjb.jar
|   |   |-- sun-web.xml
|   |   `-- web.xml
|   `-- tools
|       `-- ItemException.class
`-- META-INF
    |-- MANIFEST.MF
    |-- application.xml
    `-- sun-application.xml
0
 
LVL 7

Expert Comment

by:lhankins
ID: 10970030
You need to put ChrisCartClient.jar in your war's WEB-INF/lib.

That should solve the problem.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Expert Comment

by:lhankins
ID: 10970347
Incidentally, you probably don't need ChrisCartEjb.jar in your WEB-INF/lib.

   - ChrisCartEjb will have the classes which implement your EJB tier.
   - ChrisCartClient will have the client side stubs for clients which wish to use your EJB tier.

The web-tieris a client of the EJB tier (and thus needs ChrisCartClient.jar, but not ChrisCartEjb.jar).

0
 
LVL 16

Expert Comment

by:gnoon
ID: 10970781
See its result of this code

    Context initial = new InitialContext();
    Object objref = initial.lookup("java:comp/env/ejb/CartEJB");

    System.out.println("The object: "+objref);
    System.out.println("Is it assignable to CartHome class: "+CartHome.class.isAssignableFrom(objref.getClass()));
    return;
   
0
 

Author Comment

by:ckshort
ID: 10973334
I've tried moving the ChrisCartClient.jar into WEB-INF/lib and it breaks the program outright.  
0
 
LVL 7

Expert Comment

by:lhankins
ID: 10973507
Did you remove ChrisCartEjb from WEB-INF/lib...?

What error are you seeing (can you provide the stacktrace)...?

When you ran your standalone java client which works, do you not have ChrisCartClient.jar in the classpath...?

0
 

Author Comment

by:ckshort
ID: 10974159
It's in the exact same location when I use the standalone and the servlet.
0
 
LVL 7

Expert Comment

by:lhankins
ID: 10974182
When you run your standalone client, what is the classpath...?
0
 

Author Comment

by:ckshort
ID: 10974220
CLASSPATH=/opt/glue/lib/GLUE-ALL.jar:/opt/glue/lib/GLUE.jar:/opt/glue/lib/GLUE-EXAMPLES.jar:/opt/glue/lib/dom.jar:/opt/glue/lib/EJB.jar:/opt/glue/lib/jaas.jar:/opt/glue/lib/jaxen-core.jar:/opt/glue/lib/jaxm-api.jar:/opt/glue/lib/jaxrpc-api.jar:/opt/glue/lib/jce1_2_2.jar:/opt/glue/lib/jcert.jar:/opt/glue/lib/jms.jar:/opt/glue/lib/jndi.jar:/opt/glue/lib/jnet.jar

The CLASSPATH from the AppServer basically includes /opt/SUNWappserver/*

None of the items in the system CLASSPATH are used by this program though.

JAVA_HOME = /opt/SUNWappserver/jdk
JAVA_BIN = /opt/SUNWappserver/jdk/bin

0
 
LVL 7

Accepted Solution

by:
lhankins earned 500 total points
ID: 10974258
So where does your standalone client pick up the definitions for your Cart classes from...?    In looking at the CLASSPATH above, I don't see anything that looks like something you've added.

General J2EE Deployment Stuff:
---------------------------------------
1. Generally, an EAR (enterprise archive) will contain 1 or more EJB-JARS and one or more WAR's.
2. Each EJB-JAR will contain all the classes for your EJB implementations (your EJB-Tier).
3. Each WAR will contain all the classes for your Webtier.
4. You also typically produce an EJB-Client jar, which contains the client side stubs and interface definitions which are neccessary for some client to utilize your EJB's (this jar only contains the interfaces needed to interact with your server-side EJB's).

Generally - the Webtier (defined in the WAR) is dependent on the EJB-Ter (defined in the EJB-Jar).  

Also, after looking at the ear contents you listed earlier, I think there may be a few issues:

1. The CartServlet class present in both the EJB-Jar and the WAR (should only be in the war)
2. The CartBean class is present in both the EJB-Jar and the WAR (should only be in the ejb-jar).
3. It looks like the ChrisCartClient.jar is empty - this should contain the classes neccessary for a client to use your EJB.


0
 
LVL 7

Assisted Solution

by:lhankins
lhankins earned 500 total points
ID: 10996727
Any luck resolving this Ckshort...?
0
 

Author Comment

by:ckshort
ID: 11314629
Didn't end up working, but didn't matter anyway.  Thanks for the help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now