JAVA RMI ClassNotFound
JAVA RMI ClassNotFound
过去几天我一直在尝试启动一个基本的 JAVA RMI 教程服务器。
我首先启动命令行并输入以下内容:
rmiregistry
java -classpath E:\Rmi\src\* -Djava.rmi.server.codebase=file:/E:/RMI/Src/Hello** Server
* I have all my *.java, and *.class files under "E:\RMI\SRC"
** I have attempted to use Hello.class, Hello.Java, made 'Hello' into a jar file and
** I have even-attempted to leave it blank
试图启动我的服务器。我编译在java 8.
我得到的异常是:
Server exception:
java.rmi.ServerException:
RemoteException occurred in server thread;
nested exception is:
java.rmi.UnmarshalException:
error unmarshalling arguments;
nested exception is:
java.lang.ClassNotFoundException:
Hello java.rmi.ServerException:
RemoteException occurred in server thread;
nested exception is:
java.rmi.UnmarshalException:
error unmarshalling arguments;
nested excep tion is:
java.lang.ClassNotFoundException:
Hello at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:391)
Class定义如下:
Hello.java:
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating Remote interface for our application
public interface Hello extends Remote {
void printMsg() throws RemoteException;
}
ImplExample.java
// Implementing the remote interface
public class ImplExample implements Hello {
// Implementing the interface method
public void printMsg() {
System.out.println("This is an example RMI program");
}
}
Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");
// Calling the remote method using the obtained object
stub.printMsg();
// System.out.println("Remote method invoked");
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample {
public Server() {}
public static void main(String args[]) {
try {
// Instantiating the implementation class
ImplExample obj = new ImplExample();
// Exporting the object of implementation class
// (here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
服务器在到达 'registry.bind("hello", stub)' 时崩溃。存根不为空。同样,'rmiregistry'此时已经启动。
如果有任何帮助,我将不胜感激。
来自文档
The URL of a directory in which the classes are organized in
package-named sub-directories
所以URL应该指向类根目录,大致是这样的:
-Djava.rmi.server.codebase=file://E:/Rmi/src
试试吧。
过去几天我一直在尝试启动一个基本的 JAVA RMI 教程服务器。
我首先启动命令行并输入以下内容:
rmiregistry
java -classpath E:\Rmi\src\* -Djava.rmi.server.codebase=file:/E:/RMI/Src/Hello** Server
* I have all my *.java, and *.class files under "E:\RMI\SRC"
** I have attempted to use Hello.class, Hello.Java, made 'Hello' into a jar file and
** I have even-attempted to leave it blank
试图启动我的服务器。我编译在java 8.
我得到的异常是:
Server exception:
java.rmi.ServerException:
RemoteException occurred in server thread;
nested exception is:
java.rmi.UnmarshalException:
error unmarshalling arguments;
nested exception is:
java.lang.ClassNotFoundException:
Hello java.rmi.ServerException:
RemoteException occurred in server thread;
nested exception is:
java.rmi.UnmarshalException:
error unmarshalling arguments;
nested excep tion is:
java.lang.ClassNotFoundException:
Hello at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:391)
Class定义如下:
Hello.java:
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating Remote interface for our application
public interface Hello extends Remote {
void printMsg() throws RemoteException;
}
ImplExample.java
// Implementing the remote interface
public class ImplExample implements Hello {
// Implementing the interface method
public void printMsg() {
System.out.println("This is an example RMI program");
}
}
Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");
// Calling the remote method using the obtained object
stub.printMsg();
// System.out.println("Remote method invoked");
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample {
public Server() {}
public static void main(String args[]) {
try {
// Instantiating the implementation class
ImplExample obj = new ImplExample();
// Exporting the object of implementation class
// (here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
服务器在到达 'registry.bind("hello", stub)' 时崩溃。存根不为空。同样,'rmiregistry'此时已经启动。
如果有任何帮助,我将不胜感激。
来自文档
The URL of a directory in which the classes are organized in package-named sub-directories
所以URL应该指向类根目录,大致是这样的:
-Djava.rmi.server.codebase=file://E:/Rmi/src
试试吧。