求助:HELLOWORLD EJB入門部署 折騰了3天了,吐血啊.

fishrabbit發表於2007-06-22
小弟不才,最近在研究EJB.開頭的例子HELLOWORLD..看了網上無數例子,總是無法跑通,很多例子本有就有問題.經過3天,終於部署成功,但是無法執行,JNDI出錯,實在沒有辦法了,網上找不到資料,請高手指點下迷途的羔羊.
第一個類:package examples;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Hello extends EJBObject {
public String hello() throws RemoteException;
}
第二個類 package examples;
import javax.ejb.SessionContext;
public class HelloBean implements javax.ejb.SessionBean {
private SessionContext ctx;
public void ejbCreate() {
System.out.print("ejbCreate()");
}
public void ejbRemove() {
System.out.print("ejbRemove()");
}
public void ejbActivate() {
System.out.print("ejbActivate()");
}

public void ejbPassivate() {
System.out.print("ejbPassivate");
}
public void setSessionContext(javax.ejb.SessionContext ctx) {
this.ctx = ctx;
}
public String hello() {
System.out.print("hello()");
return "Hello World~!";
}
}
第三個類:package examples;
public interface HelloHome extends javax.ejb.EJBHome{
Hello create()throws java.rmi.RemoteException,javax.ejb.CreateException;
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<Session>
<ejb-name>Hello</ejb-name>
<Home>examples.HelloHome</Home>
<Remote>examples.Hello</Remote>
<ejb-class>examples.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</Session>
</enterprise-beans>
</ejb-jar>


打包成HelloWorld.jar 放在JBOSS Deploy 資料夾下...然後執行測試檔案:

package examples;

import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class test {
public static void main(String args[]) throws NamingException, RemoteException, CreateException, RemoveException {

Properties props = new Properties();
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url","localhost:1099");
props.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");

Context ctx = new InitialContext(props);

Object obj = ctx.lookup("HelloHome");

HelloHome Home = (HelloHome) javax.rmi.PortableRemoteObject.narrow(obj,
HelloHome.class);
Hello hello = Home.create();

System.out.print(hello.hello());
hello.remove();
}
}


部署成功了,但是測試一直出錯 Exception in thread "main" javax.naming.NameNotFoundException: HelloHome not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:296)




實在是不行了,請罈子的高手們抽幾分鐘看下....已經痛苦3天了.

相關文章