I am getting the following error:
Exception in thread "main" java.rmi.NotBoundException: Server
at java.rmi/sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:234)
at java.rmi/sun.rmi.registry.RegistryImpl_Skel.dispatch(RegistryImpl_Skel.java:133)
at java.rmi/sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:468)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:298)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:303)
at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:279)
at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:380)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:123)
at client.RMIClient.startClient(RMIClient.java:17)
at client.FahrradClient.main(FahrradClient.java:12)
Can anybody look at my code and tell me where I went wrong? I'd be more than grateful. Would even send a small tip or something lmao I am that desperate.
package server;
import shared.RMI_Interface;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class FahrradServer {
public static void main(String[] args) throws IOException, AlreadyBoundException {
RMI_Interface server = new ConfigImpl();
Registry registry = LocateRegistry.createRegistry(1099);
Runtime.getRuntime().exec("rmiregistry 1099");
registry.bind("Server", server);
System.out.println("Server started");
}
}
package client;
import shared.Fahrrad;
import shared.RMI_Interface;
import java.rmi.*;
import java.rmi.registry.*;
public class RMIClient {
private RMI_Interface server;
public RMIClient() {}
public void startClient() throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
server = (RMI_Interface)registry.lookup("Server");
}
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) {
Fahrrad result = null;
try {
result = server.configureFahrrad(lenkertyp, material, schaltung, griff);
} catch (RemoteException e) {
e.printStackTrace();
throw new RuntimeException("Could not contact server");
}
return result;
}
}
package server;
import shared.Fahrrad;
import shared.RMI_Interface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ConfigImpl implements RMI_Interface {
public void ConfigImpl() throws RemoteException {
UnicastRemoteObject.exportObject(this, 0);
}
#Override
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) throws RemoteException {
Fahrrad f = new Fahrrad();
f.setLenkertyp(lenkertyp);
f.setGriff(griff);
f.setMaterial(material);
f.setSchaltung(schaltung);
if (!lenkertyp.equals( "Faltbarlenker") && !lenkertyp.equals( "Rennradlenker") && !lenkertyp.equals( "Bullhornlenker")) {
throw new IllegalArgumentException("Ungültiger Lenktypinput");
}
if (!material.equals( "Aluminium") && !material.equals( "Stahl") && !material.equals( "Kunststoff")){
throw new IllegalArgumentException("Ungültiges Material!");
}
if (!schaltung.equals("Kettenschaltung") && !schaltung.equals( "Tretlagerschaltung") && !lenkertyp.equals( "Nebenschaltung")) {
throw new IllegalArgumentException("Ungültige Schaltung!");
}
if (!griff.equals( "Kunststoffgriff") && !griff.equals( "Ledergriff") && !lenkertyp.equals( "Schaumstoffgriff")) {
throw new IllegalArgumentException("Ungültige Schaltung!");
}
if (lenkertyp.equals("Faltbarlenker") || lenkertyp.equals("Rennradlenker")) {
if (!material.equals( "Aluminium") && !material.equals( "Kunststoff")) {
throw new IllegalArgumentException(lenkertyp + " kann nur aus Aluminium oder Kunststoff bestehen.");
}
}
if (material.equals("Stahl")) {
if (!schaltung.equals("Kettenschaltung")) {
throw new IllegalArgumentException("Materialtyp "+ material + " kann nur Kettenschaltung haben!");
}
}
if (material.equals("Kunststoff")) {
if (griff.equals("Kunststoffgriff")) {
throw new IllegalArgumentException("Nur Kunststoffmaterial kann Kunststoffgriff haben!");
}
}
if (lenkertyp.equals( "Bullhornlenker") || lenkertyp.equals("Faltbarlenker")) {
if (griff.equals("Ledergriff")) {
throw new IllegalArgumentException("Nur Rennladlenker können Ledergriffe haben!");
}
}
return f;
}
}
package server;
import shared.RMI_Interface;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class FahrradServer{
public static void main(String[] args) throws IOException, AlreadyBoundException {
RMI_Interface server = new ConfigImpl();
Registry registry = LocateRegistry.createRegistry(1099);
Runtime.getRuntime().exec("rmiregistry 1099");
registry.bind("Server", server);
System.out.println("Server started");
}
}
package shared;
public class Fahrrad {
public String lenkertyp;
public String material;
public String schaltung;
public String griff;
public void setLenkertyp(String lenkertyp){
this.lenkertyp=lenkertyp;
}
public String getLenkertyp() {
return lenkertyp;
}
public void setMaterial(String material){
this.material=material;
}
public String getMaterial() {
return material;
}
public void setSchaltung(String schaltung) {
this.schaltung=schaltung;
}
public String getSchaltung() {
return schaltung;
}
public void setGriff(String griff){
this.griff=griff;
}
public String getGriff() {
return griff;
}
}
package shared;
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating Remote interface for our application
public interface RMI_Interface extends Remote {
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) throws RemoteException;
}
I know the code isn't perfect. I will change it but the most important thing is to get the client and server run independently. My client sadly doesn't run.
The steps to follow when creating a RMI application.
Create the "remote" interface.
Your interface RMI_Interface is OK. Nothing to do here.
Create a class that implements the remote interface.
I made the constructor empty. (The rest of the code is unchanged.)
public ConfigImpl() throws RemoteException {
// Empty.
}
Create a RMI server. Here I made a few changes.
package server;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import shared.RMI_Interface;
public class FahrradServer {
public static void main(String[] args) {
try {
ConfigImpl server = new ConfigImpl();
RMI_Interface stub = (RMI_Interface) UnicastRemoteObject.exportObject(server, 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("Server", stub);
System.out.println("Server started");
}
catch (AlreadyBoundException | RemoteException x) {
x.printStackTrace();
}
}
}
Note that calling method createRegistry() will launch the rmiregistry so no need for this line of your code:
Runtime.getRuntime().exec("rmiregistry 1099");
Run the server, i.e. java server.FahrradServer
(I assume you know the correct command that you need to actually launch your server.)
Note that the server program will run forever – unless you kill it.
Write the client. The client will run in a separate JVM so the client needs a main() method so that you can launch it via the java command.
package client;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import shared.Fahrrad;
import shared.RMI_Interface;
public class RMIClient {
private RMI_Interface server;
public void startClient() throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry(1099);
server = (RMI_Interface) registry.lookup("Server");
}
public Fahrrad configureFahrrad(String lenkertyp,
String material,
String schaltung,
String griff) throws RemoteException {
return server.configureFahrrad(lenkertyp, material, schaltung, griff);
}
public static void main(String[] args) {
RMIClient client = new RMIClient();
try {
client.startClient();
Fahrrad f = client.configureFahrrad("Faltbarlenker",
"Aluminium",
"Kettenschaltung",
"Kunststoffgriff");
System.out.println("Fahrrad: " + f);
}
catch (RemoteException | NotBoundException x) {
x.printStackTrace();
}
}
}
Since the return value of the remote interface method is a class that you wrote, that class must implement interface Serializable, i.e.
public class Fahrrad implements java.io.Serializable
Now you can launch your RMI client, e.g. java client.RMIClient
Refer to the RMI trail of Oracle's java tutorials.
As I'm trying to achieve my first RMI program, I'm having troubles testing it.
I compiled my programs and tried to launch my server but an error then occurs.
About my server: 1 interface + 1 class implementing it + my server class
Interface
package src;
import java.rmi.*;
public interface Information extends Remote {
// methods than can be called remotly
public String getInformation() throws RemoteException;
}
Class Implementing it
package src;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class InformationImpl extends UnicastRemoteObject implements Information {
protected InformationImpl() throws RemoteException {
super();
}
public String getInformation() throws RemoteException {
System.out.println("method getInformation()");
return "hello";
}
}
My server class :
package src;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class LanceServeur {
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
InformationImpl informationImpl = new InformationImpl();
Naming.rebind("MyServer", informationImpl);
System.out.println("Server launched");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
And my client class :
package src;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
public class lanceClient {
public static void main(String[] args) {
System.out.println("Lancement du client");
try {
Remote r = Naming.lookup("rmi://localhost:1099/TestRMI");
System.out.println(r);
if (r instanceof Information) {
String s = ((Information) r).getInformation();
System.out.println("chaine renvoyee = " + s);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
System.out.println("end of client");
}
}
If you want to see the correct lines, check my github it's easier :
https://github.com/TLprojet/MUD_Java
About the error, here it is:
java.rmi.ConnectIOException: Exception creating connection to: 0.0.21.214; nested exception is:
java.net.SocketException: Invalid argument or cannot assign requested address
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:631)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:209)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:196)
at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:147)
at java.rmi/java.rmi.Naming.rebind(Naming.java:177)
at src.LanceServeur.main(LanceServeur.java:16)
Caused by: java.net.SocketException: Invalid argument or cannot assign requested address
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at java.base/java.net.Socket.connect(Socket.java:540)
at java.base/java.net.Socket.<init>(Socket.java:436)
at java.base/java.net.Socket.<init>(Socket.java:213)
at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 6 more
My chat works fine on the localhost but when I try to do it on the remote server, it doesn't go on.
RemoteClass >
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ChatInterface extends Remote {
public String getName() throws RemoteException;
public void sendMsg(String message) throws RemoteException;
public void setInterface (ChatInterface x) throws RemoteException; }
ClientImpl >
import Interface.ChatInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ChatClient extends UnicastRemoteObject implements ChatInterface{
private String name;
private ChatInterface server;
public ChatClient(String name) throws RemoteException{
this.name = name;
}
#Override
public String getName() throws RemoteException {
return this.name;
}
#Override
public void sendMsg(String message) throws RemoteException {
System.out.println(message);
}
#Override
public void setInterface(ChatInterface server) throws RemoteException {
this.server = server;
} }
ClientMain >
import Interface.ChatInterface;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
public class Client{
public static void main(String[] args){
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
Registry registry = LocateRegistry.getRegistry("hostname", port);
ChatInterface chatClient = new ChatClient("User01");
ChatInterface chatServer = (ChatInterface) registry.lookup("ChatServer");
System.out.println("[System] Chat is ready");
chatServer.setInterface(chatClient);
String message = "["+chatClient.getName()+"] got connected";
chatServer.sendMsg(message);
Scanner s = new Scanner(System.in);
while (true) {
message = s.nextLine().trim();
message = "["+ chatClient.getName()+"]" + message;
chatServer.sendMsg(message);
}
} catch (Exception e) {
System.out.println("[System] Server failed: " + e);
System.exit(1);
}
} }
ServerImpl >
import Interface.ChatInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class ChatServer extends UnicastRemoteObject implements ChatInterface{
private String name;
private ArrayList<ChatInterface> clients;
public ChatServer(String name)throws RemoteException{
this.name = name;
this.clients = new ArrayList<>();
}
#Override
public String getName() throws RemoteException {
return this.name;
}
#Override
public void sendMsg(String message) throws RemoteException {
broadcast(message);
}
#Override
public synchronized void setInterface(ChatInterface client) throws RemoteException {
clients.add(client);
}
public ArrayList<ChatInterface> getClients(){
return clients;
}
private void broadcast(String message) throws RemoteException {
for (ChatInterface client : clients) {
client.sendMsg(message);
}
} }
ServerMain >
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server {
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(9876);
Registry registry = LocateRegistry.getRegistry(port);
ChatServer chatServer = new ChatServer("Server");
registry.bind("ChatServer", chatServer);
System.out.println("Server ready :)");
} catch (Exception e) {
System.out.println("[System] Server failed: " + e.toString());
System.exit(1);
}
}
}
The server doesn't do anything at this line
chatServer.setInterface(chatClient);
But doesn't provoke any error. It continues to run for both client & server.
If you have any advice, thank you
File: SelectServerIntf.java:
import java.rmi.*;
import java.util.*;
public interface SelectServerIntf extends Remote{
HashMap executeSelect() throws RemoteException;
}
File: SelectServerImpl.java:
import java.rmi.*;
import java.sql.*;
import java.rmi.server.*;
import java.util.*;
public class SelectServerImpl extends UnicastRemoteObject implements SelectServerIntf {
public SelectServerImpl() throws RemoteException
{
}
public HashMap executeSelect() throws RemoteException
{
String DRIVER = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3306/abc2";
String Query="select * from student";
Connection con=null;
Statement stat=null;
HashMap hm=null;
try
{
Class.forName(DRIVER);
}
catch(ClassNotFoundException cn)
{
System.out.println("ClassNotFound"+cn);
}
try
{
con= DriverManager.getConnection(url,"root","root");
stat=con.createStatement();
ResultSet rs=stat.executeQuery(Query);
hm=new HashMap();
while(rs.next())
{
int rno=rs.getInt(1);
String name=rs.getString(2);
hm.put(new Integer(rno),name);
}
con.close();
}
catch(SQLException se)
{
System.out.println("SQLException"+se);
}
return(hm);
}
}
File: SelectServer.java:
import java.rmi.*;
import java.net.*;
public class SelectServer {
public static void main(String args[])
{
try
{
SelectServerImpl sip=new SelectServerImpl();
Naming.rebind("SELECT-SERVER", sip);
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
}
}
File: SelectClient.java:
import java.rmi.*;
import java.util.*;
import java.net.*;
public class SelectClient {
public static void main(String args[])
{
String rmiurl="rmi://"+args[0]+"/SELECT-SERVER";
try
{
SelectServerIntf sit=(SelectServerIntf)Naming.lookup(rmiurl);
HashMap hm2=sit.executeSelect();
int sz=hm2.size();
for(int i=1;i<sz;i++)
{
if(hm2.containsKey(new Integer(i)))
System.out.println(i+":"+hm2.get(new Integer(i)));
}
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}
The above RMI program on execution gives
"Class not found Exception:com.mysql.jdbc.Driver"
SQLException:No Suitable Driver found for jdbc:mysql://localhost:3306/abc2
where abc2 is my database containing respective tables.
The above given connection url and drivers works properly for every code but acccept this rmi.
Experts do you find any modification??
You need to put mysql jdbc driver jars in your server's classpath. It will enable your server to load driver class while calling executeSelect()
hi i am using this codes for rmi
RmiServer.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.
// This method is called from the remote client by the RMI.
// This is the implementation of the gReceiveMessageInterfaceh.
public void receiveMessage(String x) throws RemoteException
{
System.out.println(x);
}
public RmiServer() throws RemoteException
{
try{
// get the address of this host.
thisAddress= (InetAddress.getLocalHost()).toString();
}
catch(Exception e){
throw new RemoteException("can't get inet address.");
}
thisPort=3232; // this port(registryfs port)
System.out.println("this address="+thisAddress+",port="+thisPort);
try{
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry( thisPort );
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
throw e;
}
}
static public void main(String args[])
{
try{
RmiServer s=new RmiServer();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
RmiClient.java
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient
{
static public void main(String args[])
{
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
try{
// get the �gregistry�h
registry=LocateRegistry.getRegistry(
serverAddress,
(new Integer(serverPort)).intValue()
);
// look up the remote object
rmiServer=
(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}
ReceiveMessageInterface.java
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote
{
public void receiveMessage(String x) throws RemoteException;
}
This works fine normally , but when the a computer is connected to internet through mobile or it shares internet from other pc it doesn't work
I get this error.
java.net.connectexception connection timeout
when i tried to telnet it fails to connect but when i try to run this program that pc
to my pc it works.
Please let me know how to solve this issue.
Sounds like a firewall or proxy server issue.