Olá como faz o Java RMI? Implantando interface Somador:
package exercicioUam;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Somador extends Remote{
public int soma(int num1, int num2) throws RemoteException;
}
/////
package exercicioUam;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class SomadorRemoto extends UnicastRemoteObject implements Somador {
private static final long serialVersionUID = 1L;
protected SomadorRemoto() throws RemoteException{
super();
}
@Override
public int soma(int num1, int num2) throws RemoteException {
return num1 + num2;
}
public static void main(String[] args) {
try {
Somador somador = new SomadorRemoto();
java.rmi.Naming.rebind("rmi://localhost:8000/SomadorService", somador);
System.out.println("Servidor RMI pronto.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Aparece mensagem de erro no console
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:625)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:217)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:204)
at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:345)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:150)
at java.rmi/java.rmi.Naming.rebind(Naming.java:177)
at exercicioUam.SomadorRemoto.main(SomadorRemoto.java:22)
Caused by: java.net.ConnectException: Connection refused: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:589)
at java.base/sun.nio.ch.Net.connect(Net.java:578)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:583)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:751)
at java.base/java.net.Socket.connect(Socket.java:686)
at java.base/java.net.Socket.(Socket.java:555)
at java.base/java.net.Socket.(Socket.java:324)
at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
… 6 more
Quando tenta compilar o rmic aparece erro de comando inválido
Existiria algum outro código que substitua?