最近在看JAVA核心技术 看到了RMI 那里 我照着书上打代码。 基本上没怎么改动 然后运行 就狂报异常!代码如下:
yuancheng.java
import java.rmi.Remote;
import java.rmi.RemoteException;public interface yuancheng extends Remote {
String returnstring() throws RemoteException;
}=======================================================
duixiang.javaimport 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.javaimport 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)
Caused by: 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 sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:385)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:240)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:343)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:116)
... 3 more
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: yuancheng哪位朋友帮忙看下是怎么回事啊? 谢谢!!!!!!!!
yuancheng.java
import java.rmi.Remote;
import java.rmi.RemoteException;public interface yuancheng extends Remote {
String returnstring() throws RemoteException;
}=======================================================
duixiang.javaimport 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.javaimport 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)
Caused by: 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 sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:385)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:240)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:343)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:116)
... 3 more
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: yuancheng哪位朋友帮忙看下是怎么回事啊? 谢谢!!!!!!!!
java.rmi.UnmarshalException: error unmarshalling arguments
这个应该是主要问题吧。似乎是命令行参数有问题。
这两个命令你用了吗?
rmic -v1.2
rmi registry &(启动服务器)
也运行了registry了 可 是还是报上面的错
java.rmi.ConnectException: Connection refused to host: 169.254.152.224; nested e
xception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at duixiang_Stub.returnstring(Unknown Source)
at client.main(client.java:16)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
... 6 more客户端代码是这些:import java.rmi.*;
import java.rmi.server.*;
import javax.naming.*;public class client {
public static void main(String[] args) {
System.setProperty("java.security.policy","client.policy");
System.setSecurityManager(new RMISecurityManager());
String url="rmi://localhost/"; try {
Context namingContext=new InitialContext();
yuancheng a=(yuancheng)namingContext.lookup(url+"toaster");
yuancheng b=(yuancheng)namingContext.lookup(url+"microwave");
System.out.println(a.returnstring());
System.out.println(b.returnstring());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}