我在NetBeans寫了一個程序與RMI客戶端有錯誤ClassNotFoundException的使用RMI
錯誤:
java.rmi.UnmarshalException:錯誤和解組返回;嵌套0例外是:拋出java.lang.ClassNotFoundException:rmiserver.Message (無安全管理器:RMI類裝載器禁用)在 sun.rmi.registry.RegistryImpl_Stub.lookup(來源不明)
但服務器不任何錯誤!
interfaace代碼:
package rmiclient;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Message extends Remote {
void sayHello(String name) throws RemoteException;
}
接口的實現是:
package rmiserver;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MessageImpl extends UnicastRemoteObject implements Message {
public MessageImpl() throws RemoteException {
}
@Override
public void sayHello(String name) throws RemoteException {
System.out.println("hello "+name);
}
}
服務器代碼:
package rmiserver;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Main {
private void startServer(){
try {
// create on port 1099
Registry registry = LocateRegistry.createRegistry(1099);
// create a new service named myMessage
registry.rebind("myMessage", new MessageImpl());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("system is ready");
}
public static void main(String[] args) {
Main main = new Main();
main.startServer();
}
}
客戶端代碼:
package rmiclient;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Main {
private void doTest(){
try {
// fire to localhost port 1099
Registry myRegistry = LocateRegistry.getRegistry("127.0.0.1", 1099);
// search for myMessage service
Message impl = (Message) myRegistry.lookup("myMessage");
// call server's method
impl.sayHello("edwin");
System.out.println("Message Sent");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Main main = new Main();
main.doTest();
}
}
謝謝:)。
而代碼不能編譯。 – EJP
我的代碼編譯完成!建立成功(總時間:0秒)。但不會運行 – sara
*這*代碼沒有。你在'rmiclient'包中有'Message'和在'rmiserver'包中實現它的服務器,並且沒有'import rmiclient。*'語句; – EJP