客户端程序为什么要和ejb打包在一起?客户端程序应该是调用 ejb而已, 放在哪里都可以的。比如已经部署了一个叫做 helloword的ejb 这样我只要在jndi上拿到这个ejb的home接口 再create一个本地接口 就可以调用ejb session bean的方法了。 示例程序大概是这样。public class TestEjb{
public static void main(String args[]) {
try {
InitialContext context = new InitialContext();
hellowordHome home = (hellowordHome ) context.lookup("helloword");
hellowordRemote remote = home.create();
remote.callBusinessMethod();// 调用ejb的方法
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
public static void main(String args[]) {
try {
InitialContext context = new InitialContext();
hellowordHome home = (hellowordHome ) context.lookup("helloword");
hellowordRemote remote = home.create();
remote.callBusinessMethod();// 调用ejb的方法
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
解决方案 »
- hibernate 查询出的list不能转换成持久化类
- JAVA 连接数据库
- 各位帮忙看一下array[i] = Integer.parseInt(ch.toString());
- doubleselect 中doubleHeaderValue请教
- hibernate查询出错
- hibernate 连接 mysql求救!
- 万分紧急!!!!hibernate中的string转化成datetime问题
- 请问web.xml修改的时候eclipse要验证很长时间,咋办啊??
- JAVA FTP上传语音文件后有杂音
- windows下Tomcat做成服务后启动控制台的问题
- 求解答SQL查询语句!!内祥!!谢谢!!!
- 百分求杭州恒生面试题目,希望去过的恒生的捧场
我的客户程序如下:
package sample;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;public class HelloWorldClient{
public static void main( String [] args ){
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{
Context ctx = new InitialContext(env);
Object obj = ctx.lookup( "HelloWord" );
HelloWorldHome home =(HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloWorldHome.class );
HelloWorld helloWorld = home.create();
System.out.println( helloWorld.hello());
helloWorld.remove();
}
catch ( Exception e )
{
e.printStackTrace();
System.out.println( "Exception: " + e.getMessage() );
}
}
} 出错信息如下:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/logging/Logger
at org.jnp.interfaces.NamingContext.<clinit>(NamingContext.java:158)
at org.jnp.interfaces.NamingContextFactory.getInitialContext(NamingContextFactory.java:56)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at sample.HelloWorldClient.main(HelloWorldClient.java:13)这是什么原因呢?
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)
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:585)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(Unknown Source)
at sample.HelloWorldClient.main(HelloWorldClient.java:14)
Exception: HelloWord not bound这个错误是什么造成的呢?
package sample;
import java.rmi.*;
import javax.ejb.*;public interface HelloWorldHome extends EJBHome{
HelloWorld create() throws RemoteException,CreateException;}remote接口:
package sample;
import java.rmi.*;
import javax.ejb.*;public interface HelloWorld extends EJBObject{
public String hello() throws RemoteException;}bean实现类:
package sample;import java.rmi.RemoteException;import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;public class HelloWorldBean implements SessionBean{
private SessionContext ctx;
public void setSessionContext(SessionContext ctx){
this.ctx = ctx;
}
public void ejbCreate(){
System.out.println("ejbCreate()");
}
public void ejbRemove(){
System.out.println("ejbRemove()");
}
public void ejbActivate(){
System.out.println("ejbActivate()");
}
public void ejbPassivate(){
System.out.println("ejbPassivate()");
}
public String hello(){
System.out.println("hello()");
return "hello,world";
}
} 客户程序:
package sample;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;public class HelloWorldClient{
public static void main( String [] args ){
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{
Context ctx = new InitialContext(env);
Object obj = ctx.lookup( "ejb/HelloWord" );
HelloWorldHome home =(HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloWorldHome.class );
HelloWorld helloWorld = home.create();
System.out.println( helloWorld.hello());
helloWorld.remove();
}
catch ( Exception e )
{
e.printStackTrace();
System.out.println( "Exception: " + e.getMessage() );
}
}
} ejb-jar.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>
<description>JBoss Hello World Application</description>
<display-name>Hello World EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>HelloWorld</ejb-name>
<home>sample.HelloWorldHome</home>
<remote>sample.HelloWorld</remote>
<ejb-class>sample.HelloWorldBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>jboss.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>HelloWorld</ejb-name>
<jidi-name>ejb/HelloWord</jidi-name>
</session>
</enterprise-beans>
</jboss>能帮忙看看吗?我哪里写的不对,多谢了!!
javax.naming.NameNotFoundException: ejb 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:267)
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:585)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(Unknown Source)
at sample.HelloWorldClient.main(HelloWorldClient.java:14)
22:09:05,529 INFO [ProxyFactory] Bound EJB Home 'HelloWorld' to jndi 'HelloWorl
d'
22:09:05,539 INFO [EJBDeployer] Deployed: file:/D:/jboss-4.0.4.GA/jboss-4.0.4.G
A/server/default/deploy/HelloWord.jar
javac -cp **/**/j2ee.jar;**/**/jbossall-client.jar HelloWorldClient.java
java -cp **/**/j2ee.jar;**/**/jbossall-client.jar HelloWorldClient
**是你上面两个文件所在的目录
能告诉我运行这个例子都需要些什么jar包吗?
好象写错了吧,是不是写成("HelloWorld");