--------------------------------------------------------------
ejb的远程接口 Hello.java
package firstejb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface Hello extends javax.ejb.EJBObject {
/**
*唯一的方法Hello,向客户端返回问候.
*/
public String hello() throws RemoteException;
}
--------------------------------------------------------------
ejb的HOME接口 HelloHome.java
package firstejb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface HelloHome extends javax.ejb.EJBHome {
/**
*这个方法生成EJB对象
*@return 新生成的EJB对象
*/
public Hello create() throws CreateException, RemoteException;
}
--------------------------------------------------------------------
ejb的bean部分 HelloBean.java
package firstejb;
import javax.ejb.*;
/**
*掩饰无状态会话Bean
*/
public class HelloBean implements javax.ejb.SessionBean{
//EJB必须的方法
private SessionContext ctx;
public void ejbCreate(){
System.out.println("ejbCreate");
}
public void ejbRemove(){
System.out.println("ejbCreate");
}
public void ejbActivate() {
System.out.println("edbActive");
}
public void ejbPassivate(){
System.out.println("ejbPassivate");
}
public void setSessionContext(javax.ejb.SessionContext cxt){
this.ctx = ctx;
}
//商务方法
public String hello(){
System.out.println("hello()");
return "Hello,World!";
}
}
--------------------------------------------------------------
下面是我编写的客户端调用ejb的程序
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.*;
import java.util.Properties;
import firstejb.*;
/**
*这个类文件举例说明客户端代码触发调用一个简单的无状态会话Bean方法
*/
public class HelloClient
{
public static void main(String[] args) throws Exception{
//为JNDI初始化获得系统属性,这些属性都是从命令读取的
System.out.println("1111111111111111111111111111111111");
Properties props = System.getProperties();
//System.out.println(props);
//获取初始化的Context,初始化的Context是连接JNDI的起始点,我们通过传递环境变量来选择JNDI驱动器和服务器的网络位置等.
System.out.println("22222222222222222222222222222222222");
Context ctx = new InitialContext(props);
Object obj = ctx.lookup("HelloHome");
System.out.println("333333333333333333333333333333333333");
//取得Home对象的一个引用
System.out.println("44444444444444444444444444444444444444444");
HelloHome Home = (HelloHome)javax.rmi.PortableRemoteObject.narrow(obj,HelloHome.class);
System.out.println("555555555555555555555555555555555555555555555");
//使用制作库(Home对象)来生成EJB对象
Hello hello = Home.create();
System.out.println("6666666666666666666666666666666666666666666666");
//调用hello方法返回字符串
System.out.println(hello.hello());
System.out.println("777777777777777777777777777777777777777777");
hello.remove();
}
};
在运行的时候输出22222222222222222222222222时出错,就编译不下去了,错误代码为
D:\bea\jdk141_05\bin\javaw -classpath "E:\java_ejb\project1\firstEjb\classes;D:\bea\weblogic81\server\lib\weblogic_sp.jar;D:\bea\weblogic81\server\lib\weblogic.jar;D:\bea\weblogic81\server\lib\webservices.jar;D:\JBuilder9\lib\jbcl.jar;D:\JBuilder9\lib\dx.jar;D:\JBuilder9\lib\beandt.jar;D:\bea\jdk141_05\jre\lib\charsets.jar;D:\bea\jdk141_05\jre\lib\ext\dnsns.jar;D:\bea\jdk141_05\jre\lib\ext\ldapsec.jar;D:\bea\jdk141_05\jre\lib\ext\localedata.jar;D:\bea\jdk141_05\jre\lib\ext\sunjce_provider.jar;D:\bea\jdk141_05\jre\lib\im\indicim.jar;D:\bea\jdk141_05\jre\lib\jaws.jar;D:\bea\jdk141_05\jre\lib\jce.jar;D:\bea\jdk141_05\jre\lib\jsse.jar;D:\bea\jdk141_05\jre\lib\rt.jar;D:\bea\jdk141_05\jre\lib\sunrsasign.jar;D:\bea\jdk141_05\lib\dt.jar;D:\bea\jdk141_05\lib\tools.jar;D:\bea\jdk141_05\lib\htmlconverter.jar" HelloClient
1111111111111111111111111111111111
22222222222222222222222222222222222
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at HelloClient.main(HelloClient.java:19)
Exception in thread "main"
那位知道怎么回事的给帮帮忙看看,是那里的原因啊,怎么改啊,谢谢了
8 个解决方案
#1
CLASS运行EJB要把EJB的一些JINI的信息放入到环境变量当中,我以前的代码如下:
public class HelloTestClient {
public static void main(String[]args)throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup(HelloHome.JNDI_NAME);
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.sayHello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
properties.put(Context.PROVIDER_URL,"localhost:1099");
return new InitialContext(properties);
}
}
public class HelloTestClient {
public static void main(String[]args)throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup(HelloHome.JNDI_NAME);
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.sayHello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
properties.put(Context.PROVIDER_URL,"localhost:1099");
return new InitialContext(properties);
}
}
#2
看不太懂啊,能讲清楚一些吗,我用的weblogic部署的ejb
#3
http://blog.csdn.net/wingtrace/archive/2005/03/05/310359.aspx上有WEBLOGIC部署的测试程序的参数问题
#4
环境参数错误,下面是例子,应用服务器是weblogic
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
#5
欢迎加入J2EE developer开发QQ群14673042
#6
我好晕啊,能不能帮我改改我的client代码,让它能运行了,我按你们的意思有很多类不认啊,是不是要导入其他的包那
#7
将客户端程序改为如下还是出错,又是那里错了
public class HelloClient
{
public static void main(String[] args) throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup("HelloHome");
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.hello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
return new InitialContext(properties);
}
};
出错显示javax.naming.ServiceUnavailableException. Root exception is java.net.UnknownHostException: ll4: ll4
public class HelloClient
{
public static void main(String[] args) throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup("HelloHome");
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.hello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
return new InitialContext(properties);
}
};
出错显示javax.naming.ServiceUnavailableException. Root exception is java.net.UnknownHostException: ll4: ll4
#8
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");//ll4是我机器的名称,当然会出错了,你把ll4换成你机器的名称,或者IP
#1
CLASS运行EJB要把EJB的一些JINI的信息放入到环境变量当中,我以前的代码如下:
public class HelloTestClient {
public static void main(String[]args)throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup(HelloHome.JNDI_NAME);
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.sayHello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
properties.put(Context.PROVIDER_URL,"localhost:1099");
return new InitialContext(properties);
}
}
public class HelloTestClient {
public static void main(String[]args)throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup(HelloHome.JNDI_NAME);
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.sayHello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
properties.put(Context.PROVIDER_URL,"localhost:1099");
return new InitialContext(properties);
}
}
#2
看不太懂啊,能讲清楚一些吗,我用的weblogic部署的ejb
#3
http://blog.csdn.net/wingtrace/archive/2005/03/05/310359.aspx上有WEBLOGIC部署的测试程序的参数问题
#4
环境参数错误,下面是例子,应用服务器是weblogic
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
#5
欢迎加入J2EE developer开发QQ群14673042
#6
我好晕啊,能不能帮我改改我的client代码,让它能运行了,我按你们的意思有很多类不认啊,是不是要导入其他的包那
#7
将客户端程序改为如下还是出错,又是那里错了
public class HelloClient
{
public static void main(String[] args) throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup("HelloHome");
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.hello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
return new InitialContext(properties);
}
};
出错显示javax.naming.ServiceUnavailableException. Root exception is java.net.UnknownHostException: ll4: ll4
public class HelloClient
{
public static void main(String[] args) throws Exception{
Context ctx=getInitialContext();
Object obj=ctx.lookup("HelloHome");
HelloHome helloHome=(HelloHome)PortableRemoteObject.narrow(obj,HelloHome.class);
Hello hello=helloHome.create();
System.out.println(hello.hello());
}
private static Context getInitialContext()throws Exception
{
Hashtable properties=null;
properties=new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");
return new InitialContext(properties);
}
};
出错显示javax.naming.ServiceUnavailableException. Root exception is java.net.UnknownHostException: ll4: ll4
#8
properties.put(Context.PROVIDER_URL,"t3://ll4:7001");//ll4是我机器的名称,当然会出错了,你把ll4换成你机器的名称,或者IP