Java RMI Implementation - java

I have a typical client & server situation where I want the client be able to call methods from objects running on the server. I use the Java RMI to solve this situation.
The serverside code is below and it compiles and runs fine:
ServerInt.java
package gpio.control;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ServerInt extends Remote{
public void setHeizung(boolean x) throws RemoteException;
public void quitError() throws RemoteException;
public void startPWM(int periode,int pulsbreite,int pulsanzahl) throws RemoteException;
}
ServerImpl.java
package gpio.control;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RemoteServer;
import java.rmi.server.UnicastRemoteObject;
public class ServerImpl implements ServerInt{
private Model myModel;
public ServerImpl(Model m) throws RemoteException {
myModel = m;
LocateRegistry.createRegistry(1099);
ServerInt stub = (ServerInt) UnicastRemoteObject.exportObject(this, 1099);
RemoteServer.setLog(System.out);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("Server1", stub);
}
#Override
public void setHeizung(boolean x) throws RemoteException {
myModel.setHeizung(x);
}
#Override
public void quitError() throws RemoteException {
myModel.quitError();
}
#Override
public void startPWM(int periode, int pulsbreite, int pulsanzahl) throws RemoteException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
For testing purposes, I also created a small client application which runs on the same server as the server application:
Client.java
package client;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry();
ServerInt serverint = (ServerInt) registry.lookup("rmi://127.0.0.1:1099/Server1");
serverint.setHeizung(true);
}
}
When I run the Client program now, I get the following errors:
Error from Client:
Exception in thread "main" java.rmi.NotBoundException: rmi://localhost:1099/Server1
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$241(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at client.Client.main(Client.java:12)
Error from server:
Sep 07, 2016 9:47:04 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(1)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: void rebind(java.lang.String, java.rmi.Remote)]
Sep 07, 2016 9:47:04 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(2)-127.0.0.1: [127.0.0.1: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
Sep 07, 2016 9:47:05 AM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(3)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: java.rmi.Remote lookup(java.lang.String)]
Sep 07, 2016 9:47:05 AM sun.rmi.server.UnicastServerRef logCallException
FINE: RMI TCP Connection(3)-127.0.0.1: [127.0.0.1] exception:
java.rmi.NotBoundException: rmi://localhost:1099/Server1
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$241(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
What am I doing wrong here?

In Client.java change
registry.lookup("rmi://127.0.0.1:1099/Server1")
to
registry.lookup("Server1");

As you are using Registry rather than Naming to do the lookup, you should omit the URL part of the lookup string, just as you did when binding. You should just lookup Server1, just as you did when binding.

Related

Libaries correctly extracted?

A few days ago I switched from the Eclipse IDE to IntelliJ IDE. This is my first time I am coding with IntelliJ. I don't know how I can describe my Problem. :D Where is the Problem? I use this Java Application as a Plugin in my Bungeecord-Proxy
Stacktrace:
20:46:16 [WARNING] Exception encountered when loading plugin:
CloudNet-RestAPI java.lang.ExceptionInInitializerError at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:264) at
com.sun.proxy.$Proxy9.(Unknown Source) at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739) at
com.sun.jersey.server.impl.application.WebApplicationImpl$26.run(WebApplicationImpl.java:1626)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.jersey.server.impl.application.WebApplicationImpl.createProxy(WebApplicationImpl.java:1623)
at
com.sun.jersey.server.impl.application.WebApplicationImpl.(WebApplicationImpl.java:335)
at
com.sun.jersey.server.impl.container.WebApplicationProviderImpl.createWebApplication(WebApplicationProviderImpl.java:55)
at
com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:66)
at
com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:160)
at
com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:264)
at
com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:246)
at
com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117)
at
com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92)
at me.singulativ.restapi.main.RestAPI.onEnable(RestAPI.java:17) at
net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:227)
at net.md_5.bungee.BungeeCord.start(BungeeCord.java:266) at
net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:56)
at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15) Caused by:
java.lang.RuntimeException: java.lang.ClassNotFoundException:
com.sun.ws.rs.ext.RuntimeDelegateImpl at
javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:122)
at
javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:91)
at javax.ws.rs.core.EntityTag.(EntityTag.java:35) ... 24
more Caused by: java.lang.ClassNotFoundException:
com.sun.ws.rs.ext.RuntimeDelegateImpl at
net.md_5.bungee.api.plugin.PluginClassloader.loadClass0(PluginClassloader.java:53)
at
net.md_5.bungee.api.plugin.PluginClassloader.loadClass(PluginClassloader.java:27)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:264) at
javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:62) at
javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:155) at
javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:105)
... 26 more
Code:
package me.singulativ.restapi.main;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
import net.md_5.bungee.api.plugin.Plugin;
import java.io.IOException;
public class RestAPI extends Plugin {
private HttpServer server;
#Override
public void onEnable() {
try {
String host = "http://localhost:12345/";
server = HttpServerFactory.create(host);
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onDisable() {
server.stop(0);
}
}
Dependencies
Artifacts

Can not Start Java RMI Server and Client

I am new for Java RMI. I tried to start simple basic example java RMI with eclipse. But I cannot start Server and Client code. please help me to solve this problem.
Server Code:
MyServer.java
package in.rmi.test;
import java.rmi.*;
public class MyServer {
public static void main(String args[]) {
try {
Adder stub = new AdderRemote();
Naming.rebind("rmi://localhost:5000/sonoo", stub);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Adder.java
package in.rmi.test;
import java.rmi.*;
public interface Adder extends Remote{
public int add(int x,int y)throws RemoteException;
}
AdderRemote.java
package in.rmi.test;
import java.rmi.*;
import java.rmi.server.*;
public class AdderRemote extends UnicastRemoteObject implements Adder{
private static final long serialVersionUID = 1L;
AdderRemote()throws RemoteException{
super();
}
public int add(int x,int y){
return x+y;
}
}
Exception while start Server
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: in.rmi.test.Adder
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:420)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:271)
at sun.rmi.transport.Transport$2.run(Transport.java:202)
at sun.rmi.transport.Transport$2.run(Transport.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:198)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:378)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:177)
at in.rmi.test.MyServer.main(MyServer.java:11)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: in.rmi.test.Adder
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:271)
at sun.rmi.transport.Transport$2.run(Transport.java:202)
at sun.rmi.transport.Transport$2.run(Transport.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:198)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: in.rmi.test.Adder
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.rmi.server.LoaderHandler$Loader.loadClass(LoaderHandler.java:1206)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:278)
at sun.rmi.server.LoaderHandler.loadClassForName(LoaderHandler.java:1219)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:729)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:673)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:610)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:646)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:265)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1566)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1779)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
... 17 more
MyClient.java
package in.rmi.test;
import java.rmi.*;
public class MyClient {
public static void main(String args[]) {
try {
System.setProperty("java.security.policy", "AllPermission.policy");
System.setSecurityManager(new RMISecurityManager());
Adder stub = (Adder) Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34, 4));
} catch (Exception e) {
System.out.println(e);
}
}
}
I solved this problem by refering the following link
http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/enhancements-7.html
http://www.math.uni-hamburg.de/doc/java/tutorial/rmi/running.html
Start RMI Registory as like following :
rmiregistry -J-Djava.rmi.server.codebase=file:///home/visva/Downloads/1/ 5000 &
file:///home/visva/Downloads/1/ - is class path

RMI server ClassNotFoundException when client makes call

I am trying to make a simple RMI execution engine to run on my local machine so that other java programs can easily run code on a second processor. (In the future this will be expanded to allow more processors).
The server starts up just fine, and the client appears to locate the registry ok, but when it tries to call the execution engine (and passes it a parameter) it causes a ClassNotFoundException.
I recognize that this is similar to many other questions on stack overflow, but as far as I can tell, all the other ones have to do with the client not being able to download the server's classes rather than the server not being able to download the client's classes.
Here is my code (copied almost exactly from this sample code):
RMIServer eclipse project
ComputeEngine Interface:
package interfaces;
import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ComputeEngine_IF extends Remote {
/**
* #param task The task object specifying the computation to be performed
* #return
* #throws RemoteException
*/
<T extends Serializable> T compute(TaskWithoutInput<T> task) throws RemoteException;
}
TaskWithoutInput interface:
package interfaces;
import java.io.Serializable;
public interface TaskWithoutInput<T> extends Serializable {
public T compute();
}
ComputeEngine Class:
package server;
import interfaces.ComputeEngine_IF;
import interfaces.TaskWithoutInput;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class ComputeEngine implements ComputeEngine_IF{
public ComputeEngine() {
super();
}
#Override
public <T extends Serializable> T compute(TaskWithoutInput<T> task) throws RemoteException {
return task.compute();
}
public static void main(String[] args) {
if(System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Compute";
ComputeEngine_IF engine = new ComputeEngine();
ComputeEngine_IF stub = (ComputeEngine_IF) UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind(name, stub);
System.out.println("Registry bound!");
}
catch (Exception e) {
System.err.println("ComputeEngine exception:");
e.printStackTrace();
}
}
}
RMIClient eclipse project
GetAnswerTask class
package client;
import interfaces.TaskWithoutInput;
public class GetAnswerTask implements TaskWithoutInput<Integer> {
private static final long serialVersionUID = 1L;
#Override
public Integer compute() {
return Integer.valueOf(42);
}
}
Client class
package client;
import interfaces.ComputeEngine_IF;
import interfaces.TaskWithoutInput;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class RmiClientExample {
public static void main(String args[]) {
if(System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Compute";
Registry registry = LocateRegistry.getRegistry("localhost");
ComputeEngine_IF comp = (ComputeEngine_IF) registry.lookup(name);
TaskWithoutInput<Integer> getAnswer = new GetAnswerTask();
Integer answer = comp.compute(getAnswer);
System.out.println("Answer: " + answer);
}
catch (Exception e) {
System.err.println("RmiClientExample exception:");
e.printStackTrace();
}
}
}
Both projects contain an identical security policy file (in RMIServer it is called server.policy and in RMIClient it is called client.policy)
grant {
permission java.security.AllPermission;
};
I will, of course, restrict the permissions more once I get this working.
Building/Running
Since the code is pretty close to something I copied out of an example, my guess is that the code is right, or at least close, but that my mistake is in compiling/running the code. The example wasn't written for eclipse, so I don't have exact instructions.
The first thing I did was use eclipse's jar export wizard to jar the interface package into Interfaces.jar which I just placed in my RMIServer folder.
Then I run ComputeEngine with the following defines:
-Djava.rmi.server.codebase=file:/c:/Users/nate/workspace/RMIServer/Interfaces.jar
-Djava.rmi.server.hostname=localhost
-Djava.security.policy=c:/Users/nate/workspace/RMIServer/src/server.policy
The compute engine seems to run just find and outputs the print statement in the code.
I then run the client with the defines:
-Djava.rmi.server.codebase=file:/c:/Users/nate/workspace/RMIClient/bin/
-Djava.security.policy=c:/Users/nate/workspace/RMIClient/src/client.policy
And I get the following error message:
RmiClientExample exception:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: client.GetAnswerTask
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.compute(Unknown Source)
at client.RmiClientExample.main(RmiClientExample.java:24)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: client.GetAnswerTask
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: client.GetAnswerTask
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.rmi.server.LoaderHandler$Loader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClassForName(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at sun.rmi.server.UnicastRef.unmarshalValue(Unknown Source)
at sun.rmi.server.UnicastServerRef.unmarshalParametersUnchecked(Unknown Source)
at sun.rmi.server.UnicastServerRef.unmarshalParameters(Unknown Source)
... 13 more
Any thoughts on what I might be doing wrong?
GetAnswerTask implements Serializable, so it will be serialized to the server when you call ComputeEngine_IF.compute(). So GetAnswerTask needs to be available at the server, on its classpath, and it isn't.
For those still struggling with oracle RMI tutorial, whose server doesn't download serializable class from given client's codebase (and stubbornly seeks class in its own codebase) - try to add
-Djava.rmi.server.useCodebaseOnly=false
to server's arguments

ClassNotFoundException is thrown on attempt to start RMI server in a web service running under Jetty

I know it is a common question, but after reading a dozen of similar ones on SO and a lot of tries I still can't solve my problem.
I want to start RMI in Zimbra (8.6.0, jetty-distribution-9.1.5.v20140505) web service extension.
The server interface class is located inside the service extension jar
$ jar -ft /opt/zimbra/lib/ext/addserver/addserver.jar | grep AddServer
com/gussy/zimbra/TestAddServer.class
com/gussy/zimbra/TestAddServerIntf.class
I tried to set java options via zmlocalconfig, like
zmlocalconfig -e zimbra_zmjava_options="-Xmx256m -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2 -Djdk.tls.client.protocols=TLSv1,TLSv1.1,TLSv1.2 -Djava.net.preferIPv4Stack=true -Djava.rmi.server.useCodebaseOnly=true -Djava.rmi.server.codebase=file:///opt/zimbra/lib/ext/addserver/addserver.jar"
and tried
export CLASSPATH=/opt/zimbra/lib/ext/addserver/addserver.jar && zmmailboxdctl restart
also tried to setup java options from inside java code
System.setProperty("java.rmi.server.codebase", "file:///opt/zimbra/lib/ext/addserver/addserver.jar");
System.setProperty("java.rmi.server.useCodebaseOnly", "false");
But can't fix the problem.
TestAddService.java
import com.zimbra.cs.extension.ExtensionHttpHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class TestAddService extends ExtensionHttpHandler {
private static final Logger logger = LoggerFactory.getLogger(TestAddService.class);
public TestAddService() {
try {
Registry registry = LocateRegistry.createRegistry(1099);
TestAddServer testAddServer = new TestAddServer();
Naming.rebind("TestAddServer", testAddServer);
} catch (RemoteException | MalformedURLException e) {
logger.error(null,e);
}
}
#Override
public String getPath() {
return "/testadd";
}
#Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {}
}
TestAddServer.java
package com.gussy.zimbra;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class TestAddServer extends UnicastRemoteObject implements TestAddServerIntf {
private static final long serialVersionUID = 3527439814680102697L;
protected TestAddServer() throws RemoteException {}
#Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}
TestAddServerIntf.java
package com.gussy.zimbra;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface TestAddServerIntf extends Remote {
int add(int a, int b) throws RemoteException;
}
Exception
2016-03-30 12:47:10,601 ERROR [main] [] TestAddService -
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: com.gussy.zimbra.TestAddServerIntf (no security manager: RMI class loader disabled)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:420)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:178)
at sun.rmi.transport.Transport$1.run(Transport.java:175)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:557)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:177)
at com.gussy.zimbra.TestAddService.<init>(TestAddService.java:25)
at pro.prokator.zimbra.ext.casem.SubscriptumExtension.init(SubscriptumExtension.java:53)
at com.zimbra.cs.extension.ExtensionUtil.initAll(ExtensionUtil.java:116)
at com.zimbra.cs.util.Zimbra.startup(Zimbra.java:263)
at com.zimbra.cs.util.Zimbra.startup(Zimbra.java:178)
at com.zimbra.soap.SoapServlet.init(SoapServlet.java:126)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:582)
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:372)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:847)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:300)
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1359)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1352)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:744)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:497)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:125)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:154)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:125)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:125)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:125)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.server.handler.DebugHandler.doStart(DebugHandler.java:140)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:125)
at org.eclipse.jetty.server.Server.start(Server.java:358)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
at org.eclipse.jetty.server.Server.doStart(Server.java:325)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1250)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1174)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:297)
at org.eclipse.jetty.start.Main.start(Main.java:727)
at org.eclipse.jetty.start.Main.main(Main.java:103)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: com.gussy.zimbra.TestAddServerIntf (no security manager: RMI class loader disabled)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport$1.run(Transport.java:178)
at sun.rmi.transport.Transport$1.run(Transport.java:175)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:557)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.gussy.zimbra.TestAddServerIntf (no security manager: RMI class loader disabled)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:556)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:646)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:255)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1559)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
... 13 more
The Registry doesn't have the class mentioned on its CLASSPATH.
This can only happen when you run the Registry as a separate process.
This not the real code. The exception is thrown by TestAddService, which you haven't posted.
The simplest solution is to run the Registry in your JVM, as per the code you did post, which can't possibly throw this exception.

Java RMI simple Hello World program throwing RemoteException

I have this strange problem.
I'm trying to write a simple Hello World java program using RMI.
Here are my files, all belonging to the same package:
Hello.java:
package com.something;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote{
String hello() throws RemoteException;
}
Server.java
package com.something;
import com.something.Hello;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello{
#Override
public String hello() throws RemoteException {
return "hello world!!!!";
}
public static void main (String args[]) throws RemoteException{
//System.setProperty("java.rmi.server.hostname", "10.0.0.5");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Hello";
Hello s = new Server();
Hello stub =
(Hello) UnicastRemoteObject.exportObject(s, 0);
Registry registry = LocateRegistry.getRegistry(8888);
registry.rebind(name, stub);
System.out.println("Hello bound");
} catch (Exception e) {
System.err.println("Hello exception:");
e.printStackTrace();
}
}
}
Client.java:
package com.something;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Hello";
Registry registry = LocateRegistry.getRegistry("localhost");
Hello h = (Hello) registry.lookup(name);
System.out.println(h.hello());
} catch (Exception e) {
System.err.println("My exception:");
e.printStackTrace();
}
}
}
Before runnig the code, in the terminal, I run:
rmiregistry 8888
I run the java code with the following flag:
-Djava.security.policy=/Users/nataliazon/Documents/workspace/SiecioweRMI/src/server.policy
And here's my server.policy file:
grant {
permission java.security.AllPermission;
};
When I run the Server program like that, the following exception is thrown:
Hello exception:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: com.something.Hello
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:400)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at com.something.Server.main(Server.java:30)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: com.something.Hello
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:390)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ClassNotFoundException: com.something.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:709)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:653)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:590)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1530)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1492)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
... 12 more
And the Client program throws this:
My exception:
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at com.something.Client.main(Client.java:18)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 5 more
How can this problem be solved?
java.lang.ClassNotFoundException: com.something.Hello
The Registry and the client don't have that class on their classpath.
In the case of the Registry the simplest solution is to start it in the server JVM. via LocateRegistry.createRegistry(): or else start it in such a way that the class is on its classpath; there are several ways to accomplish that.
java.rmi.ConnectException: Connection refused to host: localhost
That is caused by specifying "localhost" instead of the server host when calling Naming.lookup() or when calling LocateRegistry.getRegistry() prior to calling Registry.lookup(). The Registry isn't ubiquitous, it is located at the server host.
NB you don't need the SecurityManager unless you are using the RMI codebase feature or you have some other requirement for it.

Categories