当前位置: 技术问答>java相关
小弟最近在学习远程方法调用(rmi),从网上搞了个简单例子,但就是调试不通,请大家看看,谢谢!
来源: 互联网 发布时间:2017-05-03
本文导语: 测试环境win2000 Professional,jdk1.4 目前的情况是 在我机器上的D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclassesrmi目录下已经存在 AddServer.java,AddServerImpl.java,RmiClient.java,RmiServer.java 在D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclasses目录下我...
测试环境win2000 Professional,jdk1.4
目前的情况是
在我机器上的D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclassesrmi目录下已经存在
AddServer.java,AddServerImpl.java,RmiClient.java,RmiServer.java
在D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclasses目录下我运行
javac rmi*.java
rmic rmi.AddServerImpl
在D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclassesrmi下获得
AddServer.class,AddServerImpl.class,AddServerImpl_Skel.class,AddServerImpl_Stub.class,RmiClient.class,RmiServer.class
然后运行start rmiregistry弹出dos窗口显示
security properties not found. using defaults.
然后我运行
D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclasses>java rmi.RmiServer
结果如下:
Exception in thread "main" java.rmi.ServerException: Server RemoteException; nes
ted exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
java.lang.ClassNotFoundException: rmi.AddServerImpl_Stub
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
RemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:350)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:159)
at rmi.RmiServer.main(RmiServer.java:8)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
ception is:
java.lang.ClassNotFoundException: rmi.AddServerImpl_Stub
Caused by: java.lang.ClassNotFoundException: rmi.AddServerImpl_Stub
==========
package rmi;
import java.rmi.*;
public interface AddServer extends Remote {
public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException;
}
===========
package rmi;
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServer
{
public AddServerImpl() throws RemoteException
{
super();
}
public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException
{
return firstnumber + secondnumber;
}
}
==============
package rmi;
import java.rmi.*;
import java.net.*;
public class RmiClient {
public static void main(String args[]) throws RemoteException, MalformedURLException ,NotBoundException{
String url="rmi://127.0.0.1/addnumbers";
AddServer add;
add = (AddServer)Naming.lookup(url);
int result = add.AddNumbers(10,5);
System.out.println(result);
}
}
===================
package rmi;
import java.rmi.*;
import java.net.*;
public class RmiServer {
public static void main (String args[]) throws RemoteException, MalformedURLException
{
AddServerImpl add = new AddServerImpl();
Naming.rebind("addnumbers",add);
}
}
==============
目前的情况是
在我机器上的D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclassesrmi目录下已经存在
AddServer.java,AddServerImpl.java,RmiClient.java,RmiServer.java
在D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclasses目录下我运行
javac rmi*.java
rmic rmi.AddServerImpl
在D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclassesrmi下获得
AddServer.class,AddServerImpl.class,AddServerImpl_Skel.class,AddServerImpl_Stub.class,RmiClient.class,RmiServer.class
然后运行start rmiregistry弹出dos窗口显示
security properties not found. using defaults.
然后我运行
D:jakarta-tomcat-4.0.4webappsexamplesWEB-INFclasses>java rmi.RmiServer
结果如下:
Exception in thread "main" java.rmi.ServerException: Server RemoteException; nes
ted exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
java.lang.ClassNotFoundException: rmi.AddServerImpl_Stub
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
RemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:350)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:159)
at rmi.RmiServer.main(RmiServer.java:8)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
ception is:
java.lang.ClassNotFoundException: rmi.AddServerImpl_Stub
Caused by: java.lang.ClassNotFoundException: rmi.AddServerImpl_Stub
==========
package rmi;
import java.rmi.*;
public interface AddServer extends Remote {
public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException;
}
===========
package rmi;
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServer
{
public AddServerImpl() throws RemoteException
{
super();
}
public int AddNumbers(int firstnumber,int secondnumber) throws RemoteException
{
return firstnumber + secondnumber;
}
}
==============
package rmi;
import java.rmi.*;
import java.net.*;
public class RmiClient {
public static void main(String args[]) throws RemoteException, MalformedURLException ,NotBoundException{
String url="rmi://127.0.0.1/addnumbers";
AddServer add;
add = (AddServer)Naming.lookup(url);
int result = add.AddNumbers(10,5);
System.out.println(result);
}
}
===================
package rmi;
import java.rmi.*;
import java.net.*;
public class RmiServer {
public static void main (String args[]) throws RemoteException, MalformedURLException
{
AddServerImpl add = new AddServerImpl();
Naming.rebind("addnumbers",add);
}
}
==============
|
如果还有问题,,你分开执行
runreg.bat:启动rmiregistry,并且打开runreg-secu.bat(runreg-secu.bat不用另行打开
runreg-secu:在服务器端绑定engine.ComputeEngine(如果runreg.bat没有执行,则此命令无法执行成功)
test.bat:测试RMI是否布置成功,可以打开修改参数
runreg.bat:启动rmiregistry,并且打开runreg-secu.bat(runreg-secu.bat不用另行打开
runreg-secu:在服务器端绑定engine.ComputeEngine(如果runreg.bat没有执行,则此命令无法执行成功)
test.bat:测试RMI是否布置成功,可以打开修改参数
|
java.lang.ClassNotFoundException: rmi.AddServerImpl_Stub
这个是关键。检查生成的存根文件路径。
这个是关键。检查生成的存根文件路径。