运行rmi测试程序的错误

问题描述:

我想运行以下两个类,但出现此错误。
有谁知道问题是什么?运行rmi测试程序的错误

java.rmi.ConnectException: Connection refused to host: 10.0.0.2; nested exception is: 
    java.net.ConnectException: Connection refused: connect 
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619) 
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) 
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) 
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340) 
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source) 
    at RegisterWithRMIServer.main(RegisterWithRMIServer.java:9) 
Caused by: java.net.ConnectException: Connection refused: connect 
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) 
    at java.net.Socket.connect(Socket.java:579) 
    at java.net.Socket.connect(Socket.java:528) 
    at java.net.Socket.<init>(Socket.java:425) 
    at java.net.Socket.<init>(Socket.java:208) 
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) 
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146) 
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613) 
    ... 5 more 




import java.rmi.registry.*; 

public class RegisterWithRMIServer { 
    /** Main method */ 
    public static void main(String[] args) { 
     try { 
     StudentServerInterface obj = new StudentServerInterfaceImpl(); 
     Registry registry = LocateRegistry.getRegistry(); 
     registry.rebind("StudentServerInterfaceImpl", obj); 
     System.out.println("Student server " + obj + " registered"); 
    } 
    catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    } 
} 

///////

import java.rmi.*; 
import java.rmi.server.*; 
import java.util.*; 

public class StudentServerInterfaceImpl extends UnicastRemoteObject 
     implements StudentServerInterface { 
    // Stores scores in a map indexed by name 
     private HashMap<String, Double> scores = 
     new HashMap<String, Double>(); 

    public StudentServerInterfaceImpl() throws RemoteException { 
    initializeStudent(); 
    } 

    /** Initialize student information */ 
    protected void initializeStudent() { 
    scores.put("John", new Double(90.5)); 
    scores.put("Michael", new Double(100)); 
    scores.put("Michelle", new Double(98.5)); 
    } 

    /** Implement the findScore method from the 
    * Student interface */ 
    public double findScore(String name) throws RemoteException { 
    Double d = (Double)scores.get(name); 

    if (d == null) { 
     System.out.println("Student " + name + " is not found "); 
     return -1; 
    } 
    else { 
    System.out.println("Student " + name + "\'s score is " + d.doubleValue()); 
     return d.doubleValue(); 
    } 
    } 
} 
+0

您是否启动了RMI服务器? – Crollster 2012-07-20 05:45:44

+0

如果您在Windows上工作,您是否从控制面板启动RMI服务? – aProgrammer 2012-07-20 05:47:13

什么时候开始注册表。这里是RMI

RMI Tutorial (Running RMI programs)

一个简短的链接,开始在Windows注册表(如果你的操作系统是Windows),你需要在

启动rmiregistry的

打开命令提示符,然后键入

否则,如果你的操作系统是Solaris或Linux

rmiregistry &