Z
Zheng Da
The program consists of 4 files.
-----------------------------------------------------------------------------------------------------------------------------
Server.java:
package test;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class Server
{
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "BackendProtocol";
try {
System.out.println("create backend");
Backend backend = new Backend();
System.out.println("start rebind");
Naming.rebind(name, backend);
System.out.println("Backend bound");
} catch (Exception e) {
System.err.println("Backend exception: " + e.getMessage());
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
BackendProtocol.java:
package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
import net.jini.admin.Administrable;
public interface BackendProtocol extends Remote , Administrable {
public float doConversion(float amount, String country)
throws RemoteException;
}
-----------------------------------------------------------------------------------------------------------------------------
Backend.java:
package test;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;
public class Backend extends UnicastRemoteObject implements
BackendProtocol {
Hashtable exchangerate = new Hashtable();
Backend() throws RemoteException {
}
// This initializes the converter to allow conversions
// for known countries.
public void initRates() {
exchangerate.put("UK", new Float(0.72));
exchangerate.put("Sweden", new Float(200));
}
public float doConversion(float amount, String country)
throws RemoteException {
System.out.println("Back-end: doing the conversion");
Float xchrate = (Float) exchangerate.get(country);
if (xchrate != null)
return amount * (xchrate.floatValue());
else
return amount;
}
public Object getAdmin() throws RemoteException {
return "Admin";
}
}
-----------------------------------------------------------------------------------------------------------------------------
Client.java:
package test;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class Client {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "//" + args[0] + "/BackendProtocol";
BackendProtocol comp = (BackendProtocol) Naming.lookup(name);
comp.doConversion(500 , "UK");
System.out.println(comp.getAdmin());
} catch (Exception e) {
System.err.println("Backend: " + e.getMessage());
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
I compile and run it.
E:\Projects\eclipse\workspace\TestRMI>javac -classpath
d:\jini2_1\lib\jini-core.jar;d:\jini2_1\lib\jini-ext.jar;d:\jini2_1\lib\sun-util.jar
test\*.java
E:\Projects\eclipse\workspace\TestRMI>rmic -classpath
d:\jini2_1\lib\jini-core.jar;d:\jini2_1\lib\jini-ext.jar;d:\jini2_1\lib\sun-util.jar;.\
test.Backend
E:\Projects\eclipse\workspace\TestRMI>java -cp
d:\jini2_1\lib\jini-core.jar;d:\jini2_1\lib\jini-ext.jar;d:\jini2_1\lib\sun-util.jar;.\
-Djava.rmi.server.codebase=file:/e:\Projects\eclipse\workspace\TestRMI/
-Djava.security.policy=java.policy test.Server
Because the interface of BackendProtocol extends
net/jini/admin/Administrable interface, there is NoClassDefFoundError
when I run the program.
Backend exception: Error occurred in server thread; nested exception
is:
java.lang.NoClassDefFoundError: net/jini/admin/Administrable
java.rmi.ServerError: Error occurred in server thread; nested exception
is:
java.lang.NoClassDefFoundError: net/jini/admin/Administrable
at
sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:349
)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:207)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
..java:701)
at java.lang.Thread.run(Thread.java:534)
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:160)
at test.Server.main(Server.java:19)
Caused by: java.lang.NoClassDefFoundError: net/jini/admin/Administrable
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:219)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:430)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:165)
at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:631)
at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:257)
at
sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.jav
a:200)
at
java.ibjectInputStream.readNonProxyDesc(ObjectInputStream.java:151
3)
at java.ibjectInputStream.readClassDesc(ObjectInputStream.java:1435)
at
java.ibjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
626)
at java.ibjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.ibjectInputStream.readObject(ObjectInputStream.java:324)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at
sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:342
)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:207)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
..java:701)
at java.lang.Thread.run(Thread.java:534)
Does anybody have ideas? Is there anything wrong in my steps compiling
and running the program?
-----------------------------------------------------------------------------------------------------------------------------
Server.java:
package test;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class Server
{
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "BackendProtocol";
try {
System.out.println("create backend");
Backend backend = new Backend();
System.out.println("start rebind");
Naming.rebind(name, backend);
System.out.println("Backend bound");
} catch (Exception e) {
System.err.println("Backend exception: " + e.getMessage());
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
BackendProtocol.java:
package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
import net.jini.admin.Administrable;
public interface BackendProtocol extends Remote , Administrable {
public float doConversion(float amount, String country)
throws RemoteException;
}
-----------------------------------------------------------------------------------------------------------------------------
Backend.java:
package test;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;
public class Backend extends UnicastRemoteObject implements
BackendProtocol {
Hashtable exchangerate = new Hashtable();
Backend() throws RemoteException {
}
// This initializes the converter to allow conversions
// for known countries.
public void initRates() {
exchangerate.put("UK", new Float(0.72));
exchangerate.put("Sweden", new Float(200));
}
public float doConversion(float amount, String country)
throws RemoteException {
System.out.println("Back-end: doing the conversion");
Float xchrate = (Float) exchangerate.get(country);
if (xchrate != null)
return amount * (xchrate.floatValue());
else
return amount;
}
public Object getAdmin() throws RemoteException {
return "Admin";
}
}
-----------------------------------------------------------------------------------------------------------------------------
Client.java:
package test;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class Client {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "//" + args[0] + "/BackendProtocol";
BackendProtocol comp = (BackendProtocol) Naming.lookup(name);
comp.doConversion(500 , "UK");
System.out.println(comp.getAdmin());
} catch (Exception e) {
System.err.println("Backend: " + e.getMessage());
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
I compile and run it.
E:\Projects\eclipse\workspace\TestRMI>javac -classpath
d:\jini2_1\lib\jini-core.jar;d:\jini2_1\lib\jini-ext.jar;d:\jini2_1\lib\sun-util.jar
test\*.java
E:\Projects\eclipse\workspace\TestRMI>rmic -classpath
d:\jini2_1\lib\jini-core.jar;d:\jini2_1\lib\jini-ext.jar;d:\jini2_1\lib\sun-util.jar;.\
test.Backend
E:\Projects\eclipse\workspace\TestRMI>java -cp
d:\jini2_1\lib\jini-core.jar;d:\jini2_1\lib\jini-ext.jar;d:\jini2_1\lib\sun-util.jar;.\
-Djava.rmi.server.codebase=file:/e:\Projects\eclipse\workspace\TestRMI/
-Djava.security.policy=java.policy test.Server
Because the interface of BackendProtocol extends
net/jini/admin/Administrable interface, there is NoClassDefFoundError
when I run the program.
Backend exception: Error occurred in server thread; nested exception
is:
java.lang.NoClassDefFoundError: net/jini/admin/Administrable
java.rmi.ServerError: Error occurred in server thread; nested exception
is:
java.lang.NoClassDefFoundError: net/jini/admin/Administrable
at
sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:349
)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:207)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
..java:701)
at java.lang.Thread.run(Thread.java:534)
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:160)
at test.Server.main(Server.java:19)
Caused by: java.lang.NoClassDefFoundError: net/jini/admin/Administrable
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:219)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:430)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:165)
at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:631)
at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:257)
at
sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.jav
a:200)
at
java.ibjectInputStream.readNonProxyDesc(ObjectInputStream.java:151
3)
at java.ibjectInputStream.readClassDesc(ObjectInputStream.java:1435)
at
java.ibjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
626)
at java.ibjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.ibjectInputStream.readObject(ObjectInputStream.java:324)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at
sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:342
)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:207)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
..java:701)
at java.lang.Thread.run(Thread.java:534)
Does anybody have ideas? Is there anything wrong in my steps compiling
and running the program?