OS环境: windows xp sp2
server: jboss3.2.2
ejb version: 2.0
看了一些书本和网上的例子,运行总会碰到一些问题,经过总结,发现了一些问题没有说明:
1、客户端的factory的jdni设置,在Client中代码设置,或者设置jdni.properties
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url","localhost:1099");
env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
try{
InitialContext ctx=new InitialContext(env);
......
2、环境变量的设置
参见下文第四步
一、结构
EJB(文件夹)
|
|--*.class
|
|--META-INF
|
|-ejb-jar.xml
|-jboss.xml
二、打包相关的文件
jar -cf till.jar ./*.class ./META-INF(till是我的ejb的名字)
生成till.jar文件,这个文件将要放到jboss的deploy中去。
三、客户端
---------------------------------------------------------------------------------------------
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
public class Client{
private TillHome home;
public static void main(String[] args){
Client cl=new Client();
cl.init();
cl.pay();
}
public void init(){
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url","localhost:1099");
env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
try{
InitialContext ctx=new InitialContext(env);
Object obj=ctx.lookup("ejb/till");
home=(TillHome)PortableRemoteObject.narrow(obj,TillHome.class);
}
catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
public void pay(){
Book[] books={new Book("Java","John Hunt",29.50,"1111"),
new Book("XML","Chris Loftus",45.00,"1112"),
new Book("UML","Denise Cooke",19.90,"1113")};
try{
System.out.println("You must pay:");
Till till=home.create();
System.out.println(till.getTotal(books));
}
catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
}
-------------------------------------------------------------------------------
四、需要的环境变量
4.1 j2ee.jar需要加入classpath中,否则会:java.lang.NoClassDefFoundError: javax/ejb/EJBObject
4.2 jboss-3.2.3\client\jbossall-client.jar需要加入classpath中,否则会:
javax.naming.NoInitialContextException: Cannot instantiate class:
org.jnp.interfaces.NamingContextFactory
说明:或者把j2ee.jar和jbossall-client.jar复制到你的JRE安装目录下C:\Program Files\Java\j2re1.4.2\lib\ext\
五、启动服务
把第二步生成的till.jar放置到 jboss-3.2.3\server\default\deploy\目录下,然后启动jboss服务器。
六、运行客户端
java Client
---------------------
结果。。。
=====================================================================
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://java.sun.com/xml/ns/j2ee"
xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
http://java.sun.com/xml/ns/j2ee
and by indicating the version of the schema by
using the version element as shown below:
<ejb-jar 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"
version="2.1">
...
</ejb-jar>