使用RMI时抛出 error during JRMP connection establishment; nested exception is:java.io.EOFException]
刚学了RMI,编译教材上的代码运行很正常,教材上是使用rmiregistry注册远程对象的。然后我想用weblogic来代替行不行,但修改代码后就报错了。代码如下:
---------HelloService.java------------------------------------------------------------------------------------------
package chapter11;
import java.util.Date;
import java.rmi.*;
public interface HelloService extends Remote{
public String echo(String msg)throws RemoteException;
public Date getTime()throws RemoteException;
}
-------------HelloServiceImpl.java----------------------------------------------------------------------
package chapter11;
import java.rmi.*;
import java.util.Date;
import java.rmi.server.UnicastRemoteObject;
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService{
private String name;
public HelloServiceImpl(String name)throws RemoteException{
this.name = name;
}
public String echo(String msg)throws RemoteException{
System.out.println(name+":use the echo() method");
return "echo:"+msg+"from"+name;
}
public Date getTime()throws RemoteException{
System.out.println(name+":use the getTime() method");
return new Date();
}
}
----------SimpleServer.java----------------------------------------------------------------------------
package chapter11;
import java.rmi.*;
import javax.naming.*;
import java.util.*;
public class SimpleServer {
static Context namingContext = null;
public static void bind(String name,HelloService object){
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); try{
namingContext = new InitialContext(ht);
namingContext.rebind(name,object);
}catch(NamingException e){
e.printStackTrace();
}finally{
try{
namingContext.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]){
try{
HelloService service1 = new HelloServiceImpl("service1");
HelloService service2 = new HelloServiceImpl("service2");
bind("rmi://localhost:7001/HelloService1",service1);
bind("rmi://localhost:7001/HelloService2",service2);
System.out.println("the server has registered two HelloService Object");
}catch(Exception e){
e.printStackTrace();
}
}
}
--------------------------------------
在项目里引入weblogic.jar后,运行weblogic server,再运行服务器类即SimpleServer 就报错
javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.io.EOFException]
at com.sun.jndi.rmi.registry.RegistryContext.rebind(RegistryContext.java:142)
at com.sun.jndi.toolkit.url.GenericURLContext.rebind(GenericURLContext.java:231)
at javax.naming.InitialContext.rebind(InitialContext.java:408)
at chapter11.SimpleServer.bind(SimpleServer.java:15)
at chapter11.SimpleServer.main(SimpleServer.java:31)。。。