ClassNotFoundException with RMI - java

i wrote a program in netbeans with RMI that client has error
error :
java.rmi.UnmarshalException: error unmarshalling return; nested
exception is: java.lang.ClassNotFoundException: rmiserver.Message
(no security manager: RMI class loader disabled) at
sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
but sever does not any error!
interfaace code:
package rmiclient;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Message extends Remote {
void sayHello(String name) throws RemoteException;
}
interface implementation is:
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);
}
}
server code is:
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();
}
}
client code is:
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();
}
}
thanks :).

In your stacktrace:
java.lang.ClassNotFoundException: rmiserver.Message
but as per data you have provided, your Message interface is declated with package package rmiclient; and You haven't created any rmiserver.Message class.
Correct the package name.

I have two package. rmiclient and rmiserver. that in rmiclient are "message" and "main" . in rmiserver are "message" and "main" and "messageimpl"
That's your problem. This doesn't satisfy the contract. You can't just copy Message to another package and expect to have it be treated the same as the original. The remote stub implements the same remote interfaces that the remote object does. Not another interface with the same name in another package.
You have to deploy rmiserver.Message to the client. Just like the error message says, really.

Related

JNDI Reference+RMI, load remote .class failed

my java version is: 1.8.0_282
this is client:
import java.rmi.registry.*;
import javax.naming.*;
public class RegistryClient {
public static void main(String[] args) throws Exception {
System.setProperty("com.sun.jndi.rmi.object.trustURLCodebase", "true");
Context registry = new InitialContext();
registry.lookup("rmi://127.0.0.1:1099/Demo");
System.out.println("done");
}
}
this is server:
import java.rmi.registry.*;
import javax.naming.*;
import com.sun.jndi.rmi.registry.ReferenceWrapper;
public class RegistryServer {
public static void main(String[] args) throws Exception {
Registry registry = LocateRegistry.createRegistry(1099);
Reference refObj = new Reference(
"xxx",
"RMIRegistryDemoRemote",
"http://127.0.0.1:8000/"
);
ReferenceWrapper hello = new ReferenceWrapper(refObj);
registry.bind("Demo", hello);
System.out.println("[!] server is ready");
}
}
this is the interface and implement of RMIRegistryDemo:
import java.rmi.*;
public interface RMIRegistryDemo extends Remote {
String sayHello(String name) throws Exception;
}
import java.rmi.server.*;
import java.rmi.*;
public class RMIRegistryDemoImpl extends UnicastRemoteObject implements RMIRegistryDemo {
public RMIRegistryDemoImpl() throws Exception {}
String id = "10";
#Override
public String sayHello(String name) {
System.out.println(id);
return "Hi, " + name;
}
}
this is the remote .class:
import java.io.IOException;
public class RMIRegistryDemoRemote {
public RMIRegistryDemoRemote() throws IOException {
final Process process = Runtime.getRuntime().exec("/System/Applications/Calculator.app/Contents/MacOS/Calculator");
}
}
after:
run RegistryServer
deployed a web server to send RMIRegistryDemoRemote.class
run RegistryClient
the client just prints "done", and no access log in my weblog:
# overflow in ~/Downloads/test [16:16:44]
» javac RegistryClient.java && java RegistryClient
done
# overflow in ~/Downloads/test/remote [16:20:05]
» python -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
what causes it?

How to get IP address of a web service client?

I created a web service using Eclipse (luna), Tomcat8 and Axis2.To do, I wrote in Java a class 'AddOperator' defining the service as folow:
public class AddOperator {
public int add(int x,int y) {
return x+y;
}
}
I created also a client who consomms this service using the class'AddClientOpp' as folow:
public class AddClientOpp {
public static void main(String[] args) throws RemoteException {
AddOperatorStub stub = new AddOperatorStub();
Add add = new Add();
add.setX(25);
add.setY(30);
System.out.println(stub.add(add).get_return());
}
}
Now, I need to obtain the client's IP adress consomming my web service. I searched and found a few methods and portions of codes that would allow to do this, such as:
import javax.servlet.http.HttpServletRequest;
//...
private static String getClientIp(HttpServletRequest request) {
String remoteAddr = "";
if (request != null) {
remoteAddr = request.getHeader("X-FORWARDED-FOR");
if (remoteAddr == null || "".equals(remoteAddr)) {
remoteAddr = request.getRemoteAddr();
}
}
return remoteAddr;
}
or:
import java.net.*;
import java.io.*;
import java.applet.*;
public class GetClientIP extends Applet {
public void init() {
try {
InetAddress Ip =InetAddress.getLocalHost();
System.out.println("IP:"+Ip.getHostAddress());
} catch(Exception e) {
e.printStackTrace();
}
}
}
But I do not know where to integrate these code portions or methods into my web service creation process described above!!
if someone has an idea about thi please tell me what i have to do. it's verry important for my project!!
thanks.
You can use the following code to retrieve the client address:
MessageContext.getCurrentMessageContext().getProperty(MessageContext.REMOTE_ADDR)

Kryonet RMI, cannot wait for response on connection's update thread

I would like to make a game using LibGDX and Kryonet library, using RMI. So I created clean project. What I want to do for now is, setup server to listen on port 10048 and on new connection to print client's name which will I get by calling a method on client's class...
Here is the code:
ICardsTableImpl.java
package clzola.cardstable.client;
public interface ICardsTableGameImpl {
public String getName();
}
CardsTableServer.java
package clzola.cardstable.server;
import clzola.cardstable.client.ICardsTableGameImpl;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;
import com.esotericsoftware.minlog.Log;
import java.io.IOException;
import java.util.HashMap;
public class CardsTableServer extends Server {
private HashMap<Integer, Connection> connections;
public CardsTableServer() throws IOException {
connections = new HashMap<Integer, Connection>();
addListener(new NetworkListener(this));
Kryo kryo = getKryo();
ObjectSpace.registerClasses(kryo);
kryo.register(ICardsTableGameImpl.class);
bind(10048);
}
#Override
protected Connection newConnection() {
Player player = new Player();
addConnection(player);
return player;
}
public void addConnection(Connection connection) {
this.connections.put(connection.getID(), connection);
}
public Connection getConnection(int connectionId) {
return this.connections.get(connectionId);
}
public Connection removeConnection(int connectionId) {
return this.connections.remove(connectionId);
}
public static void main(String[] args) throws IOException {
Log.set(Log.LEVEL_DEBUG);
CardsTableServer server = new CardsTableServer();
server.start();
}
}
NetworkListener.java
package clzola.cardstable.server;
import clzola.cardstable.client.ICardsTableGameImpl;
import com.badlogic.gdx.Gdx;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;
public class NetworkListener extends Listener {
private CardsTableServer server;
public NetworkListener(CardsTableServer server) {
this.server = server;
}
#Override
public void connected(Connection connection) {
Player player = ((Player) connection);
ICardsTableGameImpl game = ObjectSpace.getRemoteObject(player, 0, ICardsTableGameImpl.class);
player.name = game.getName(); // This is where I get excpetion...
Gdx.app.log("Server", "Player name: " + player.name);
}
#Override
public void disconnected(Connection connection) {
server.removeConnection(connection.getID());
}
}
Player.java
package clzola.cardstable.server;
import com.esotericsoftware.kryonet.Connection;
public class Player extends Connection {
public String name;
}
CardsTableGame.java
package clzola.cardstable.client;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;
public class CardsTableGame extends ApplicationAdapter implements ICardsTableGameImpl {
SpriteBatch batch;
Stage stage;
Client client;
String name = "Lazar";
ObjectSpace objectSpace;
#Override
public void create () {
batch = new SpriteBatch();
stage = new Stage(new ScreenViewport(), batch);
try {
client = new Client();
client.start();
Kryo kryo = client.getKryo();
ObjectSpace.registerClasses(kryo);
kryo.register(ICardsTableGameImpl.class);
ObjectSpace objectSpace = new ObjectSpace();
objectSpace.register(0, this);
objectSpace.addConnection(client);
client.connect(5000, "127.0.0.1", 10048);
} catch (Exception e) {
Gdx.app.log("CardsTableGame", e.getMessage(), e);
}
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public String getName() {
return this.name;
}
}
After running it, I get exception on the server side:
Exception in thread "Server" java.lang.IllegalStateException: Cannot wait for an RMI response on the connection's update thread.
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.waitForResponse(ObjectSpace.java:420)
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.invoke(ObjectSpace.java:408)
at com.sun.proxy.$Proxy0.getName(Unknown Source)
at clzola.cardstable.server.NetworkListener.connected(NetworkListener.java:24)
at com.esotericsoftware.kryonet.Server$1.connected(Server.java:48)
at com.esotericsoftware.kryonet.Connection.notifyConnected(Connection.java:214)
at com.esotericsoftware.kryonet.Server.acceptOperation(Server.java:417)
at com.esotericsoftware.kryonet.Server.update(Server.java:249)
at com.esotericsoftware.kryonet.Server.run(Server.java:372)
at java.lang.Thread.run(Thread.java:745)
And I have no idea why... What am I doing wrong??
(This is the first time ever I am trying to use RMI)
The Listener is executed by the Kryonet-update-thread. This thread is checking the socket regularly to receive the messages. Calling game.getName() makes the caller wait until the answer was delivered over the network. If you do that on the update thread you'd probably put your server in deadlock because kryonet cannot receive the answer it is waiting on, since you block the update thread. This is why it throws the exception.
In an rmi example from the kryonet git they solve this problem by using a Listener working on its own thread.
// The ThreadedListener means the network thread won't be blocked when waiting for RMI responses.
client.addListener(new ThreadedListener(new Listener() {
public void connected (final Connection connection) {
TestObject test = ObjectSpace.getRemoteObject(connection, 42, TestObject.class);
// Normal remote method call.
assertEquals(43.21f, test.other());
// Make a remote method call that returns another remote proxy object.
OtherObject otherObject = test.getOtherObject();
// Normal remote method call on the second object.
assertEquals(12.34f, otherObject.value());
// When a remote proxy object is sent, the other side recieves its actual remote object.
connection.sendTCP(otherObject);
}
}));

Array Index Out of Bounds Exception - RMI DynamicClient

import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RMISecurityManager;
import java.rmi.server.RMIClassLoader;
import java.util.*;
import java.lang.reflect.Constructor;
public class DynamicClient {
public DynamicClient (String[] args) throws Exception {
Properties p = System.getProperties();
String url = p.getProperty("java.rmi.server.codebase");
Class ClasseClient = RMIClassLoader.loadClass(url,"ClientDistant");
Constructor [] C = ClasseClient.getConstructors();
C[0].newInstance( new Object[] {args} );
}
public static void main (String[] args) {
System.setSecurityManager( new RMISecurityManager() );
try {
DynamicClient cli = new DynamicClient(args);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
This code is supposed to dynamically charge the class ClientDistant from a web server and execute the treatment, however it displays an error message:
C:\Users\DELL\Desktop>java -Djava.security.policy=client.policy -Djava.rmi.serv
er.useCodebaseOnly=false -Djava.rmi.server.codebase=http://localhost:8080/RMI/
DynamicClient
java.lang.ArrayIndexOutOfBoundsException: 0
where RMI is a directory's in the java web application named RMI ( I m using GlassFish server).
Any solutions to load ClientDistant and execute its treatment?

Java RMI application failed to run

I created a simple Java RMI application in netbeans 7.0.1 ( win 7 + jdk 7u1). I have two separate projects:
RMI_Client contains:
MyClient class:
package rmi_client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MyClient
{
public static void main(String[] args)
{
String msg = "Hello RMI";
rmiInterface stub;
Registry reg;
try
{
reg = LocateRegistry.getRegistry("localhost");
stub = (rmiInterface) reg.lookup("Hello");
try
{
stub.hello(msg);
}
catch(Exception e)
{
System.out.println("Remote method exception thrown: " +e.getMessage());
}
}
catch(Exception e)
{
System.err.println("Client exception thrown: " + e.toString());
e.printStackTrace();
}
}
}
rmiInterface interface:
package rmi_client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface rmiInterface extends Remote
{
void hello(String message) throws RemoteException;
}
RMI_Server contains:
rmiInterface interface - exactly the same as above
rmiImpl class:
package rmi_server;
import java.rmi.RemoteException;
public class rmiImpl implements rmiInterface
{
#Override
public void hello(String message) throws RemoteException
{
System.out.println("Server:" + message);
}
}
MyServer class:
package rmi_server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class MyServer
{
public static void main(String[] args)
{
try
{
rmiImpl robj = new rmiImpl();
rmiInterface stub = (rmiInterface) UnicastRemoteObject.exportObject(robj, 0);
Registry reg = LocateRegistry.createRegistry(1099);
reg.rebind("Hello", stub);
System.out.println("Server is ready to listen: ");
}
catch (Exception e)
{
System.err.println("Server exception thrown: " + e.toString());
}
}
}
If I'm doing something wrong above please let me know. First I start RMI_Server application, then when I run RMI_Client I get errors:
Client exception thrown: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at rmi_client.MyClient.main(MyClient.java:28)
Caused by: java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:554)
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:257)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1549)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1511)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
... 2 more
Where is the problem ?
RMI_Server contains:
rmiInterface interface - exactly the same as above
No it isn't. This message:
java.lang.ClassNotFoundException: rmi_server.rmiInterface
says that rmiinterface is in the rmi_server package, which isn't 'the same as above'.
It has to be exactly the same class. Not a similar class in a different package.
So what you need is three packages: server, client, and shared. The shared package needs to contain the remote interface and any application classes it relies on.

Categories