title: java.lang.ClassCastException - Deploying WAR to GlassFish 3.1.2. - Invalid JNDI lookup, maybe?
body: <p>I'm trying to decipher the standard deployment procedure for WAR files to Oracle GlassFish 3.1.2 via Eclipse EE on *NIX. I'm getting a <code>java.lang.ClassCastException</code> for the JNDI lookup String (as far as I know). Any suggestions?</p>
  
  <p>JNDI lookup String: <code>Object objref = ctx.lookup("java:comp/env/");</code></p>
  
  <p>Some posts on SE have advocated using: <code>ctx.lookup("java:comp/env/ejb/&lt;mapped name&gt;")</code>
  So: Object objref = ctx.lookup("java:comp/env/Exchange");` </p>
  
  <ul>
  <li>What does 'ejb' denote? Is it an internal, Java-specific identifier, or is it a userland directory?</li>
  </ul>
  
  <p>Navigating to <code>http://localhost:8080/Exchange/</code> throws error:</p>
  
  <pre><code>java.lang.ClassCastException: com.sun.enterprise.naming.impl.JavaURLContext cannot be cast to org.omg.CORBA.Object
  </code></pre>
  
  <p><strong>Stack trace</strong></p>
  
  <pre><code>[#|2013-02-13T10:26:01.507-0500|WARNING|oracle-glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=73;_ThreadName=Thread-2;|StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
  java.lang.ClassCastException: com.sun.enterprise.naming.impl.JavaURLContext cannot be cast to org.omg.CORBA.Object
      at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:245)
      at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:153)
      at org.apache.jsp.ExchangeClient_jsp._jspService(ExchangeClient_jsp.java:87)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
      at java.lang.Thread.run(Thread.java:722)
  |#]
  </code></pre>
  
  <p>ejb-jar.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ejb-jar&gt;
    &lt;enterprise-beans&gt;
      &lt;session&gt;
        &lt;ejb-name&gt;Exchange&lt;/ejb-name&gt;
        &lt;home&gt;ejb.ExchangeHome&lt;/home&gt;
        &lt;remote&gt;ejb.ExchangeObject&lt;/remote&gt;
        &lt;ejb-class&gt;ejb.ExchangeBean&lt;/ejb-class&gt;
        &lt;session-type&gt;Stateless&lt;/session-type&gt;
        &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
      &lt;/session&gt;
    &lt;/enterprise-beans&gt;
    &lt;assembly-descriptor&gt;
      &lt;container-transaction&gt;
        &lt;method&gt;
          &lt;ejb-name&gt;Exchange&lt;/ejb-name&gt;
          &lt;method-name&gt;*&lt;/method-name&gt;
        &lt;/method&gt;
        &lt;trans-attribute&gt;Required&lt;/trans-attribute&gt;
      &lt;/container-transaction&gt;
    &lt;/assembly-descriptor&gt;
  &lt;/ejb-jar&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
    &lt;display-name&gt;Exchange&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;ExchangeClient.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>ExchangeClient.jsp</strong></p>
  
  <pre><code>&lt;%@page import="ejb.*" %&gt;
  &lt;%@page import="javax.naming.InitialContext"%&gt;
  &lt;%@page import="javax.naming.Context"%&gt;
  &lt;%@page import="javax.rmi.PortableRemoteObject" %&gt;
  
  &lt;!DOCTYPE HTML&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8" /&gt;
          &lt;title&gt;Currency Converter&lt;/title&gt;
          &lt;link rel="stylesheet" href="master.css" /&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;WAR&lt;/h1&gt;
  
          &lt;section id="user_input"&gt;
              &lt;form action="ExchangeClient.jsp" method="POST"&gt;
                  &lt;label&gt;Amount&lt;/label&gt;   &lt;input type="text" name="initial_amount" size="16" placeholder="0.00" pattern="^([.0-9])" required/&gt;&lt;BR&gt;
                  &lt;label&gt;From&lt;/label&gt; &lt;input type="text" name="base_trigram" size="16" placeholder="CAD" pattern="^([A-Z])" required/&gt;
                  &lt;label&gt;To&lt;/label&gt;   &lt;input type="text" name="destination_trigram" size="16" placeholder="USD" pattern="^([A-Z])" required/&gt;
  
                  &lt;button id="submit" type="submit"&gt;Submit&lt;/button&gt;   
                  &lt;button id="reset" type="reset"&gt;Clear&lt;/button&gt;   
              &lt;/form&gt;
          &lt;/section&gt;
  
          &lt;section id="result"&gt;
              &lt;%!ExchangeObject exchange; %&gt;
              &lt;%
              String amount = request.getParameter("initial_amount");
              String from = request.getParameter("base_trigram");
              String to = request.getParameter("destination_trigram");
  
              Context ctx = new InitialContext(); 
              Object objref = ctx.lookup("java:comp/env/");
              ExchangeHome home = (ExchangeHome)PortableRemoteObject.narrow(objref, ExchangeHome.class);
  
              ExchangeObject exchange = home.create();
              out.println(exchange.convert(amount, from, to));
              out.println(exchange.getExchangeRate());
              out.println(exchange.getExchangeRateTimestamp());
              %&gt;
          &lt;/section&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  
  foo@Machine ~/workspace $ tree Exchange
  Exchange
  ├── build
  │   └── classes
  │       └── ejb
  │           ├── ExchangeBean.class
  │           ├── ExchangeHome.class
  │           └── ExchangeObject.class
  ├── src
  │   └── ejb
  │       ├── ExchangeBean.java
  │       ├── ExchangeHome.java
  │       └── ExchangeObject.java
  └── WebContent
      ├── ExchangeClient.jsp
      ├── META-INF
      │   └── MANIFEST.MF
      └── WEB-INF
          ├── lib
          │   └── ejb-jar.xml
          └── web.xml
  </code></pre>