最近在看JAVA核心技术 看到了RMI 那里 我照着书上打代码。 基本上没怎么改动 然后运行 就狂报异常!
代码如下:
yuancheng.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface yuancheng extends Remote {
String returnstring() throws RemoteException;
}
=======================================================
duixiang.java
import java.rmi.*;
import java.rmi.server.*;
public class duixiang extends UnicastRemoteObject implements yuancheng{
private String str=null;
public duixiang(String str) throws RemoteException
{
this.str=str;
}
public String returnstring() throws RemoteException
{
return this.str;
}
}
===================================================================
Server.java
import java.rmi.*;
import java.rmi.server.*;
import javax.naming.*;
public class Server {
public static void main(String[] args) {
try {
duixiang first=new duixiang( "wufangdong ");
duixiang second=new duixiang( "wangdi ");
Context namingContext=new InitialContext();
namingContext.bind( "rmi:toaster ",first);
namingContext.bind( "rmi:microwave ",second);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
贴出一些异常信息:
javax.naming.CommunicationException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: yuancheng]
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:122)
at com.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:208)
at javax.naming.InitialContext.bind(InitialContext.java:359)
at duixiang.main(duixiang.java:27)