import java.rmi.*; public interface HelloIn extends java.rmi.Remote{
String sayHello() throws RemoteException;
}import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
import java.rmi.server.*;public class Hello extends java.rmi.server.UnicastRemoteObject implements HelloIn{
public Hello() throws RemoteException{
super();
}
public String sayHello() throws RemoteException{
return "Hello,World!";
}
public static void main(String[] args){
//System.setSecurityManager(new java.rmi.RMISecurityManager());
try{
Hello h=new Hello();
java.rmi.Naming.rebind("hello",h);
System.out.print("Ready......");
}
catch(Exception e){
e.printStackTrace();
}
}
}import java.rmi.Naming;
import java.rmi.registry.*;
public class Helloworld{
public static void main(String[] args){
//System.setProperty( "java.security.policy", "client.policy" );
//System.setSecurityManager(new java.rmi.RMISecurityManager());
try{
HelloIn hi=(HelloIn)Naming.lookup("//hello");
for(int i=0;i<10;i++){
System.out.println(hi.sayHello());
}
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}javac Hello.java
rmic Hello 生成Stub 和 Skeleton
start rmiregistry 执行服务端程序前在命令行方式下启动rmi的注册程序
java Hello 启动服务器
javac Helloworld.java
以上命令成功执行。
java Helloworld 运行此命令输出如下错误:
sun.rmi.registry.RegistryImpl_Stub cannot be cast to HelloIn
java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub cannot be cast
to HelloIn
at Helloworld.main(Helloworld.java:8)
String sayHello() throws RemoteException;
}import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
import java.rmi.server.*;public class Hello extends java.rmi.server.UnicastRemoteObject implements HelloIn{
public Hello() throws RemoteException{
super();
}
public String sayHello() throws RemoteException{
return "Hello,World!";
}
public static void main(String[] args){
//System.setSecurityManager(new java.rmi.RMISecurityManager());
try{
Hello h=new Hello();
java.rmi.Naming.rebind("hello",h);
System.out.print("Ready......");
}
catch(Exception e){
e.printStackTrace();
}
}
}import java.rmi.Naming;
import java.rmi.registry.*;
public class Helloworld{
public static void main(String[] args){
//System.setProperty( "java.security.policy", "client.policy" );
//System.setSecurityManager(new java.rmi.RMISecurityManager());
try{
HelloIn hi=(HelloIn)Naming.lookup("//hello");
for(int i=0;i<10;i++){
System.out.println(hi.sayHello());
}
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}javac Hello.java
rmic Hello 生成Stub 和 Skeleton
start rmiregistry 执行服务端程序前在命令行方式下启动rmi的注册程序
java Hello 启动服务器
javac Helloworld.java
以上命令成功执行。
java Helloworld 运行此命令输出如下错误:
sun.rmi.registry.RegistryImpl_Stub cannot be cast to HelloIn
java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub cannot be cast
to HelloIn
at Helloworld.main(Helloworld.java:8)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货