Bom dia pessoa, sou novo no GUJ, mas mesmo assim, gostaria muito que me ajudassem. Estou começando a fazer Sistemas Distribuidos e preciso fazer uma aplicação em RMI ou WebServices, onde que eu digite 3 números no Client e o Server irá retornar qual é o maior número.
Resolvi usar o RMI, baseando em um código de exemplo do professor:
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public static void main (String args[]) {
try {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
//Create and export a remote object
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj,0);
//Register the remote object with a Java RMI registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.out.println("Server Ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Client.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class Client {
private Client() {}
public static void main (String args[]) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Para funcionar eu preciso abrir 3 CMD:
No primeiro irei iniciar o RMI:
rmiregistry
Depois irei compilar e executar Server.java e depois o Client.java
Este código funciona perfeitamente, então decidi fazer o meu:
MaiorNumero.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MaiorNumero extends Remote {
public float Maior(float num1,float num2, float num3) throws RemoteException;
}
Client.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.util.Scanner;
public class Client {
private Client() {}
public static void main (String args[]) {
Scanner numero = new Scanner (System.in);
System.out.println("Digite o primeiro número");
float num1 = numero.nextFloat();
System.out.println("\n");
System.out.println("Digite o segundo número");
float num2 = numero.nextFloat();
System.out.println("\n");
System.out.println("Digite o terceiro número");
float num3 = numero.nextFloat();
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
MaiorNumero stub = (MaiorNumero) registry.lookup("MaiorNumero");
float response = stub.Maior(num1, num2, num3);
System.out.println("response: " + response);
} 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 implements MaiorNumero {
public Server() {}
public float Maior(float num1,float num2, float num3) {
float maiorValor = Math.max(num1, num2);
maiorValor = Math.max(maiorValor, num3);
System.out.printf("O maior valor foi: " + maiorValor);
return maiorValor;
}
public static void main (String args[]) {
try {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
//Create and export a remote object
Server obj = new Server();
MaiorNumero stub = (MaiorNumero) UnicastRemoteObject.exportObject(obj,0);
//Register the remote object with a Java RMI registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("MaiorNumero", stub);
System.out.println("Server Ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Porém ao executar o servidor apresenta o seguinte erro:
D:\Pedro\Desktop\SD\Novo>java Server
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: MaiorNumero
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: MaiorNumero
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: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$0(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(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at Server.main(Server.java:30)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: MaiorNumero
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$0(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)
Caused by: java.lang.ClassNotFoundException: MaiorNumero
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.rmi.server.LoaderHandler$Loader.loadClass(LoaderHandler.java:1207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at sun.rmi.server.LoaderHandler.loadClassForName(LoaderHandler.java:1221)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:731)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:674)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:611)
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)
... 15 more
E depois que eu tentei executar o meu código, o exemplo Hello também apresenta erro no RMI.
Poderiam me guiar? Sou iniciante em JAVA e estou um pouco perdido