Why do I receive an error when connecting to a Java RMI Registry if it was created in a class which was called in MATLAB?

6 visualizaciones (últimos 30 días)
I have written a Java class that either uses an existing RMI registry, or creates one. See the following Java code snippet:
try {
reg = LocateRegistry.getRegistry(1099);
reg.list();
} catch (Exception e1) {
reg = LocateRegistry.createRegistry(1099);
}
See the files RMIExample.java and IRMIExample.java below for full source code.
When I instantiate the first object of this class in MATLAB:
>> a = tmw.dta.RMIExample
a =
RMIExample[UnicastServerRef [liveRef: [endpoint:[172.16.59.161:4086](local),objID:[-212c6059:11db4762d8e:-7fff, 4536923814734999922]]]]
the RMI registry is created, but then when I create a second object (which should thus connect to the registry created by the first instantiation) I receive a Java exception:
>> b = tmw.dta.RMIExample
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: tmw.dta.IRMIExample (no security manager: RMI class loader disabled)
b =
RMIExample[UnicastServerRef [liveRef: [endpoint:[172.16.59.161:4086](local),objID:[-212c6059:11db4762d8e:-7ffe, -8406764515259456100]]]]
When I create two instances of this class outside MATLAB, or when I first manually start the RMI registry, I do not receive this Java exception.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 27 de Jun. de 2009
You need to start a security manager before creating the RMI registry:
URL url = getClass().getResource("client.policy");
System.setProperty("java.security.policy", url.toString());
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
It is very important that the file client.policy can be found (otherwise MATLAB will crash), therefore in this example we assume that you include it in your JAR-file (or Java classpath). This ensures that it can be found using getClass().getResource. The contents of client.policy should resemble:
grant {
permission java.security.AllPermission;
};
You can apply stricter policies if your application requires it.
The attached file RMIExampleWithSecMan.java shows a fully working example class.

Más respuestas (0)

Productos


Versión

R2008b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by