您的位置:首页 > 其它

Rmi Hello World (实践rmi)

2017-01-06 12:18 323 查看
package com.xiuye.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IHello extends Remote {

/**
* java.rmi.server.ExportException: remote object implements illegal remote
* interface; nested exception is: java.lang.IllegalArgumentException:
* illegal remote method encountered: public abstract java.lang.String
* com.xiuye.rmi.IHello.helloworld() at
* sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:203)
* at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.
* java:383) at
* java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject
* .java:320) at
* java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:198)
* at
* java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:180)
* at com.xiuye.rmi.HelloWorldRmi.<init>(HelloWorldRmi.java:13) at
* com.xiuye.rmi.IHelloServer.main(IHelloServer.java:13) Caused by:
* java.lang.IllegalArgumentException: illegal remote method encountered:
* public abstract java.lang.String com.xiuye.rmi.IHello.helloworld() at
* sun.rmi.server.Util.checkMethod(Util.java:267) at
* sun.rmi.server.Util.getRemoteInterfaces(Util.java:246) at
* sun.rmi.server.Util.getRemoteInterfaces(Util.java:216) at
* sun.rmi.server.Util.createProxy(Util.java:146) at
* sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:201)
* ... 6 more
*
*
*
* @return
* @throws RemoteException
*/
public String helloworld() throws RemoteException;// 必须抛出remote异常,否则报错:

}


package com.xiuye.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class HelloWorldRmi extends UnicastRemoteObject implements IHello {

/**
*
*/
private static final long serialVersionUID = -5537185508353408293L;

protected HelloWorldRmi() throws RemoteException {
}

@Override
public String helloworld() {
return "HelloWorld!";
}

}

package com.xiuye.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class IHelloServer {

public static void main(String[] args) {

try {
IHello hello = new HelloWorldRmi();
LocateRegistry.createRegistry(9999);
// Naming.rebind("rmi://localhost:9999/hello", hello);
Naming.rebind("//localhost:9999/hello", hello);
System.out.println("wait for client coming!");
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}

}

}

wait for client coming!


package com.xiuye.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class IHelloClient {

public static void main(String[] args) {

try {
IHello hello = (IHello) Naming.lookup("//localhost:9999/hello");
System.out.println(hello.helloworld());

} catch (MalformedURLException | RemoteException | NotBoundException e) {
e.printStackTrace();
}

}

}

HelloWorld!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: