客户端访问 ejb 时出错:javax.naming.NameNotFoundException: Hello not bound(90分)

  • 主题发起人 主题发起人 xbl
  • 开始时间 开始时间
X

xbl

Unregistered / Unconfirmed
GUEST, unregistred user!
我的ejb 名称为 Hello,
已经成功将 ejb 部署到 Jboss 中,
但是客户端访问 ejb 时出错:
javax.naming.NameNotFoundException: Hello not bound
//客户端代码:
public class HelloClient
{
public static void main(String[] args)
{
try
{
Properties p=new Properties();
p.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url", "localhost:1099");
InitialContext jndiContext = new InitialContext(p);
System.out.println("Got context");
// Get a reference to the Interest Bean
Object ref = jndiContext.lookup("Hello");
System.out.println("Got reference");
// Get a reference from this to the Bean's Home interface
HelloHome home = (HelloHome)
PortableRemoteObject.narrow(ref, HelloHome.class);
// Create an Hello object from the Home interface
Hello hello = home.create();
// call the hello() method
System.out.println(hello.hello());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
问题出在哪里呢?
 
你把代码改成这样看看 你写的也没错
properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put( javax.naming.Context.PROVIDER_URL, "localhost:1099");
还有 你的jndi 和你写的EJB里的是否一样 注意大小写
我没用过Jboss 我用weblogic 你看看Jboss 有没有jndi树 从里面可以查到你部署的Ejb的jndi名称

 
还有你把你的 ejb的jar文件 加在你的lib包里 否则有可能以下语句
HelloHome home = (HelloHome)
PortableRemoteObject.narrow(ref, HelloHome.class);
也可能出错
 
7syw,我没看见什么jndi树,
我是这样作的:
我的步骤时这样的:
1 建立目录:
../hello/ejb
Hello.java //定义 Remote 接口
HelloHome.java //定义 Home 接口
HelloBean.java //实现 ejb
../Meta_Inf
ejb-jar.xml
jboss.xml

其中每个文件的内容是这样:
//Hello.java
// Remote interface for Enterprise Bean: Hello
package hello.ejb;
public interface Hello extends javax.ejb.EJBObject
{
public String hello() throws java.rmi.RemoteException;
}
//HelloHome.java
//Home interface for Enterprise Bean: Hello
package hello.ejb;
public interface HelloHome extends javax.ejb.EJBHome
{
//Creates a default instance of Session Bean: Hello
public Hello create() throws
javax.ejb.CreateException,
java.rmi.RemoteException;
}
//HelloBean.java
//Bean implementation class for Enterprise Bean: Hello
package hello.ejb;
public class HelloBean implements javax.ejb.SessionBean
{
private javax.ejb.SessionContext mySessionCtx;
//getSessionContext
public javax.ejb.SessionContext getSessionContext()
{
return mySessionCtx;
}
//setSessionContext
public void setSessionContext(javax.ejb.SessionContext ctx)
{
mySessionCtx = ctx;
}
//ejbActivate
public void ejbActivate() { }
//ejbCreate
public void ejbCreate() throws javax.ejb.CreateException { }
//ejbPassivate
public void ejbPassivate() { }
//ejbRemove
public void ejbRemove() { }
// Remote interface
public String hello()
{
return "Hello, I am a ejb Bean ! Time " + new java.util.Date();
}
}
//ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar id="ejb-jar_ID">
<display-name>helloEJB</display-name>
<enterprise-beans>
<session id="Hello">
<ejb-name>Hello</ejb-name>
<home>hello.ejb.HelloHome</home>
<remote>hello.ejb.Hello</remote>
<ejb-class>hello.ejb.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
//jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">
<jboss>
<unauthen
ticated-principal>nobody</unauthen
ticated-principal>
<enterprise-beans>
<entity>
<ejb-name>Hello</ejb-name>
<jndi-name>HelloBean</jndi-name>
<local-jndi-name>HelloLocal</local-jndi-name>
</entity>
</enterprise-beans>
<resource-managers>
</resource-managers>
</jboss>
2 我将 3 个 .java 文件成功编译了。
3 将上面2个目录打包成 Hello.jar 放到 JBoss 的 Deploy 目录下面。
4 在客户端:
将 上面服务器端的 2个文件(Hello.java, HelloHome.java)复制过来
再建立文件 HelloClient.java
5 编译 HelloClient.java、Hello.java, HelloHome.java
编译成功
运行 HelloClient,
出错:javax.naming.NameNotFoundException: Hello not bound
不知道我是哪里少了什么步骤?
 
你把
Object ref = jndiContext.lookup("Hello");
改成
Object ref = jndiContext.lookup("HelloBean");

 
我试了,运行出错: javax.naming.NameNotFoundException: HelloBean not bound
是说 HelloBean 没有被绑定,唉,不知道怎么弄,
 
在你执行的
D:/JBuilder8/jdk1.4/bin/java.exe -classpath 你的ejb.jar;还有运行要的其他jar文件 HelloClient
总之
把你的jar包放在环境变量里 让你的HelloClient执行的时候可以找的到
 
我已经放进去了,一样的不行啊,
 
HelloClient.java里加
import hello.ejb.HelloHome;
import hello.ejb.Hello;
hello.ejb.HelloHome home = (hello.ejb.HelloHome)
PortableRemoteObject.narrow(ref, hello.ejb.HelloHome);
 
一样的不行,
 
/**
* Created by IntelliJ IDEA.
* User: administrator
* Date: Sep 25, 2003
* Time: 2:00:23 PM
* To change this template use Options | File Templates.
*/
package hello;
import hello.ejb.HelloHome;
import hello.ejb.Hello;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.Properties;
public class HelloClient
{
public static void main(String[] args)
{
try
{
Properties p=new Properties();
p.setProperty("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
p.setProperty("java.naming.provider.url", "t3://137.111.160.93:80");
InitialContext jndiContext = new InitialContext(p);
System.out.println("Got context");
// Get a reference to the Interest Bean
Object ref = jndiContext.lookup("HelloBean");
System.out.println("Got reference");
// Get a reference from this to the Bean's Home interface
HelloHome home = (HelloHome)
PortableRemoteObject.narrow(ref, HelloHome.class);
// Create an Hello object from the Home interface
Hello hello = home.create();
// call the hello() method
System.out.println(hello.hello());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
----运行结果-------------------------
D:/JBuilder8/jdk1.4/bin/javaw.exe -classpath C:/project_test/classes;D:/JBuilder8/jdk1.4/jre/lib/charsets.jar;D:/JBuilder8/jdk1.4/jre/lib/jaws.jar;D:/JBuilder8/jdk1.4/jre/lib/jce.jar;D:/JBuilder8/jdk1.4/jre/lib/jsse.jar;D:/JBuilder8/jdk1.4/jre/lib/rt.jar;D:/JBuilder8/jdk1.4/jre/lib/sunrsasign.jar;D:/JBuilder8/jdk1.4/jre/lib/ext/dnsns.jar;D:/JBuilder8/jdk1.4/jre/lib/ext/ldapsec.jar;D:/JBuilder8/jdk1.4/jre/lib/ext/localedata.jar;D:/JBuilder8/jdk1.4/jre/lib/ext/sunjce_provider.jar;F:/java-lib/j2ee.jar;F:/java-lib/log4j.jar;F:/java-lib/weblogic.jar;F:/java-lib/jdom.jar;C:/project_test/lib/TSAPI_special.jar;E:/oracle/ora81/jdbc/lib/classes12.zip;C:/project_test/lib/shecaapi.jar hello.HelloClient
Got context
Got reference
Hello, I am a ejb Bean ! Time Thu Sep 25 14:46:48 CST 2003
Process terminated with exit code 0
------------------------
这是我自己试的 是可以的 不过是在weblogic下的 你的代码是没问题的 主要还是部署的问题 你在仔细看看 你的代码有什么问题
 
多谢 7syw,
是部署的问题,我在 J2ee 下面部署一点问题也没有。
我对着 J2ee tutorial 上面说的,在 J2ee 里面用 deploytool 部署,一下通过。
在 Jboss 里面部署还是没找到原因,刚刚开始学习,很多东西不会。
谢谢你一直在关注我的这个问题,
 
后退
顶部