java.lang.ClassCastException - 将WAR部署到GlassFish 3.1.2。 - 可能是无效的JNDI查找?

时间:2013-02-13 15:48:14

标签: java eclipse java-ee ejb-3.0 jndi

我正试图通过* NIX上的Eclipse EE将WAR文件的标准部署过程解密为Oracle GlassFish 3.1.2。我得到一个java.lang.ClassCastException的JNDI查找字符串(据我所知)。有什么建议吗?

JNDI查找字符串:Object objref = ctx.lookup("java:comp/env/");

SE上的一些帖子主张使用:ctx.lookup("java:comp/env/ejb/<mapped name>") 所以:Object objref = ctx.lookup(“java:comp / env / Exchange”);`

  • 'ejb'表示什么?它是内部的,特定于Java的标识符,还是用户目录?

导航到http://localhost:8080/Exchange/投掷错误:

java.lang.ClassCastException: com.sun.enterprise.naming.impl.JavaURLContext cannot be cast to org.omg.CORBA.Object

堆栈跟踪

[#|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)
|#]

ejb-jar.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
  <enterprise-beans>
    <session>
      <ejb-name>Exchange</ejb-name>
      <home>ejb.ExchangeHome</home>
      <remote>ejb.ExchangeObject</remote>
      <ejb-class>ejb.ExchangeBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
    </session>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>Exchange</ejb-name>
        <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>Exchange</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>ExchangeClient.jsp</welcome-file>
  </welcome-file-list>  
</web-app>

ExchangeClient.jsp

<%@page import="ejb.*" %>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.rmi.PortableRemoteObject" %>

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Currency Converter</title>
        <link rel="stylesheet" href="master.css" />
    </head>
    <body>
        <h1>WAR</h1>

        <section id="user_input">
            <form action="ExchangeClient.jsp" method="POST">
                <label>Amount</label>   <input type="text" name="initial_amount" size="16" placeholder="0.00" pattern="^([.0-9])" required/><BR>
                <label>From</label> <input type="text" name="base_trigram" size="16" placeholder="CAD" pattern="^([A-Z])" required/>
                <label>To</label>   <input type="text" name="destination_trigram" size="16" placeholder="USD" pattern="^([A-Z])" required/>

                <button id="submit" type="submit">Submit</button>   
                <button id="reset" type="reset">Clear</button>   
            </form>
        </section>

        <section id="result">
            <%!ExchangeObject exchange; %>
            <%
            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());
            %>
        </section>
    </body>
</html>

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

1 个答案:

答案 0 :(得分:1)

您好在EJB服务器中拥有我们称之为JNDI Server的东西。

JNDI服务器是一个难治的。这意味着当您部署和EJB时,部署者在JNDI树中创建一个条目来引用您的新EJB(比如ejb / MyEjb)。

因此,要使用MyEJB,您需要使用其名称“ejb / MyEjb”查找实现:

ctx.lookup("java:comp/env/ejb/ejb/MyEjb")

尝试使用

ctx.lookup("java:comp/env/ejb/Exchange");

如果它不起作用,请尝试浏览您的服务器jndi树并替换为正确的Exchange路径

有关详细信息,请参阅http://docs.oracle.com/javase/tutorial/jndi/concepts/index.html

此致