java.rmi.NotBoundException - java

i have written small RMI chat program and its compiling properly.but when i try to run Client program it results exception "java.rmi.NotBoundException - ServerInterface"
Server program runs without any errors..please help me to solve this.
here is some of Client code
public static void main (String[] args)
{
String address = "rmi://localhost/ServerInterface";
try
{
ServerInterface si= (ServerInterface) Naming.lookup(address);
new Thread(new Client(si)).start();
}
catch (Exception e)
{
System.err.println(e.toString()) ;
}
}

It looks like you are attempting to look up a name that is not bound.
public class NotBoundException extends Exception
A NotBoundException
is thrown if an attempt is made to lookup or unbind in the registry a
name that has no associated binding.

A NotBoundException is thrown if an attempt is made to lookup or unbind in the registry a name that has no associated binding.
What is your server code look like? This exception you are having most likely caused by server not set up properly.
I think in your server code you are bind with name ChatServer
Naming.rebind("ChatServer", new Server());
But in your client code you are using ServerInterface name
String address = "rmi://localhost/ServerInterface";
For more details Naming

Just make sure your registry.rebind(ClassName.class.getSimpleName(), new ClassImplementaion()) is match to your ClassImplementation.

Related

How to set various parameters for Java RMI based communication?

While performing a client-server communication with various forums, I am unable to perform Remote-object's lookup on the client machine.
The errors which I receive are ConnectIOException(NoRouteToHostException), and sometimes ConnectException and sometimes someother.
This is not what I want to ask. But, the main concern is how should I setup client platform and server platform --- talking about networking details --- this is what I doubt interferes with my connection.
My questions :-
How should I edit my /etc/hosts file on both client-side and server-side? Server's IP- 192.168.1.8 & Client's IP-192.168.1.100. Means, should I add the system name in both the files:
192.168.1.8 SERVER-1 # on the server side
192.168.1.100 CLIENT-1 # on the client side
Should I edit like this? Can this be one of the possible concerns? I just want to remove any doubts left over to perform the rmi-communication!
Also, I am also setting Server's hostname property using System.setProperty("java.rmi.server.hostname",192.168.1.8); on the server side. Should I do the same on the client-side too?
I've read about setting classpath while running the java program on both server-side as well as the client-side. I did this too,but,again the same exceptions. No difference at all. I've read that since Java update 6u45, classpaths aren't necessary to include! Please throw some light on this too...
If I am missing something, Please enlighten about the same too. A brief idea/link to resources are most preferred.
You don't need any of this unless you have a problem. The most usual problem is the one described in the RMI FAQ #A.1, and editing the hosts file of the server or setting java.rmi.server.hostname in the server JVM is the solution to that.
'No route to host' is a network connectivity problem, not an RMI problem, and not one you'll solve with code or system property settings.
Setting the classpath has nothing to do with network problems.
Here is server example of which transfers an concrete class. This class must be exist in server and client classpath with same structure
Message:
public class MyMessage implements Serializable {
private static final long serialVersionUID = -696658756914311143L;
public String Title;
public String Body;
public MyMessage(String strTitle) {
Title = strTitle;
Body = "";
}
public MyMessage() {
Title = "";
Body = "";
}
}
And here is the server code that gets an message and returns another message:
public class SimpleServer {
public String ServerName;
ServerRemoteObject mRemoteObject;
public SimpleServer(String pServerName) {
ServerName = pServerName;
}
public void bindYourself() {
try {
mRemoteObject = new ServerRemoteObject(this);
java.rmi.registry.Registry iRegistry = LocateRegistry.getRegistry(RegistryContstants.RMIPort);
iRegistry.rebind(RegistryContstants.CMName, mRemoteObject);
} catch (Exception e) {
e.printStackTrace();
mRemoteObject = null;
}
}
public MyMessage handleEvent(MyMessage mMessage) {
MyMessage iMessage = new MyMessage();
iMessage.Body = "Response body";
iMessage.Title = "Response title";
return iMessage;
}
public static void main(String[] server) {
SimpleServer iServer = new SimpleServer("SERVER1");
iServer.bindYourself();
while (true) {
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
and here is the remote interface of server remote object:
public interface ISimpleServer extends java.rmi.Remote{
public MyMessage doaction(MyMessage message) throws java.rmi.RemoteException;
}
all you need is adding MyMessage class both in server and client classpath.

Server RMI stop automatically when I start it

I'm frensh so sorry for my english.
I'm trying to start a server rmi but the application stop whitout error :
Here my code:
public class Server {
public static void main(String[] args) {
try {
RemoteFunction skeleton = (RemoteFunction) UnicastRemoteObject.exportObject(new FunctionImpl(), 0);
int port = Integer.parseInt(Jndiprop.getString("port"));
Registry registry = LocateRegistry.createRegistry(port);
registry.rebind(Jndiprop.getString("url"), skeleton);
System.out.println("Rmi start");
} catch (Exception e) {
e.printStackTrace();
}
}
}
the port and the url are ok.
Someone can help me ?
You must store the Registry in a static variable. Otherwise it can be garbage-collected, which leads to a train of events that allows the whole JVM to exit.

Server breaks on Naming.rebind

I'm doing an agenda on server and everything was going fine, I started the RMI registry and the server was working fine, I test the code and I was able to login.
now I want to start putting the codes in methods and calling them in the client from the server,and when I put the Naming.rebind("server", i); in the server to call methods the code breaks.
My project is contain three classes: server, client and interface that calls the methods from the server to the client. This is the code of the server:
public class ServerAgendas extends UnicastRemoteObject
implements InterfaceServer {
private static final long serialVersionUID = 1L;
protected ServerAgendas() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
static String Clientusername;
static String Clientpassword;
public static void main (String args[]) throws NotBoundException,
IOException {
System.out.println("1");
try{
InterfaceServer i = new ServerAgendas();
System.out.println("2");
Naming.rebind("server",i);
System.out.println("Serveur agendas pret");
}
catch (Exception e){
System.err.println("Error "+e.getMessage());
}
ServerSocket ASocket = new ServerSocket(portNumber);
Socket connectionSocket = ASocket.accept();
}
public boolean login(String ClientUsername, String ClientPassword)
throws IOException, SQLException {
}
in the client side I use this to call the methods:
InterfaceServer i = (InterfaceServer)Naming.lookup("server");
System.out.println(i.login(username, password));
and the interface contain:
public interface InterfaceServer extends Remote{
public boolean login(String Clientusername, String Clientpassword)
throws RemoteException, IOException, SQLException;
I run the registry at first then I run the server, the console shows:
1
2
Error RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: InterfaceServer
I used the 1 and 2 to see where it is breaking, it is breaking on:
Naming.rebind("server",i);
What do I need to do to avoid this exception occurring?
The class mentioned in the message isn't available to the Registry on its CLASSPATH.
The simplest solution to this and several other problems is to run the Registry inside the server JVM, with LocateRegistry.createRegistry().
Otherwise you will have to figure out how to start the rmiregistry command with a CLASSPATH argument, or else use the codebase feature, which is another kettle of fish altogether.

ClassNotFoundException with JGroups

I have an issue with JGroups where after building my project, running it produces this error:
Caused by: java.lang.ClassNotFoundException: org.jgroups.ReceiverAdapter
My class looks something like this -
import org.jgroups.ReceiverAdapter;
import org.jgroups.Channel;
import org.jgroups.JChannel;
public class MyClass extends ReceiverAdapter implements MyInterface {
Channel channel;
String state = "state";
public MyClass() {
super();
start();
}
public void start() {
try {
channel = new JChannel();
channel.setReceiver(this);
channel.connect("ServerCluster");
channel.getState(null, 0);
System.out.println("Connected to cluster");
} catch (Exception e) {
System.out.println("Failed to connect to cluster");
}
}
public void getState(OutputStream output) throws Exception {
System.out.println("get response");
}
public void setState(InputStream input) throws Exception {
System.out.println("set test");
}
}
Running the project from IntelliJ produces no errors, but does not produce the desired prints from getState() and setState() either. I tried creating a brand new project in the Eclipse IDE, but the same is happening there too. Connecting has been working fine, states is a new addition to my project.
Running java MyClass from the command line fires the error seen at the start of this question. The JGroups jar seems to be added to the classpath properly as org.jgroups.Channel and org.jgroups.Channel (among others) are being found.
There is a SimpleChat program provided by the JGroup devs, but when I created a new project for this I encountered the same problem.
Edit
So it turns out I have to explicitly set the classpath when running from the CLI. But still, when running the code it seems like the getState() and setState() methods are never called as there are no print statements. SimpleChat doesn't print received state... like it is meant to.
Does anyone have a solution?
Best.
So, I on the JChannel I was using RpcDispatcher and it seems I can't use the dispatcher and the getState() and setState() methods on the same channel. Simple solution: create a second channel. Seems my knowledge on the fundamentals of JGroups is lacking!

Java Inetaddress, Swing extends, and throws Exception

In a project that I'm current working on, I'm using Eclipse with the Jigloo Gui Builder.
The builder creates the class with this class line:
public class ChatServer extends javax.swing.JFrame {
String fromclient;
String ToClient;
String serverName = InetAddress.getLocalHost().getHostAddress();
String clientName = "";
...
But Inetaddress gives an error that says "Unhandled exception type UnknownHostException".
Looking through one of the other tutorial codes that I'm trying to learn off, it has the "throws Exception" on the main class. I need it to have a "throws Exception" in the public class ChatServer because I require it to be a variable throughout the whole program.
Does anyone know the proper syntax or another alternative?
Sorry to all, but I'm pretty new to Java.
Put the assignment of serverName in the constructor of ChatServer, where you either try-catch the exception yourself and do something appropriate or let the constructor throw an UnknownHostException (if possible). Maybe there is a constructor already in the class? If not, specify one with for example:
public ChatServer() {
try {
serverName = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException ex) {
// Maybe set serverName to something default
...
}
}

Categories

Resources