I have the following RMI Connection code which returns a com.sun.proxy.$Proxy0 cannot be cast to Client.AdditionInterface error. I created two separate packages Client & Server and putted the Interface in both of them. Here is my complete code:
package Serveur;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
public class AdditionServer {
public static void main (String[] argv) {
try {
LocateRegistry.createRegistry(1099);
Addition Hello = new Addition();
Naming.rebind("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC", Hello);
System.out.println("Addition Server is ready.");
}
catch (Exception e) {
System.out.println("Addition Server failed: " + e);
}
}
}
//////////////////////////////////////
package Serveur;
import java.rmi.*;
import java.rmi.server.*;
public class Addition extends UnicastRemoteObject implements AdditionInterface {
public Addition () throws RemoteException {
super();
}
#Override
public int add(int a, int b) throws RemoteException {
int result=a+b;
return result;
}
#Override
public String aff(int a, int b) throws RemoteException {
return String.valueOf(add(a, b));
}
}
////////////////////////////
package Client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface AdditionInterface extends Remote {
public int add(int a,int b) throws RemoteException;
public String aff(int a, int b) throws RemoteException;
}
////////////////////////////
package Client;
import java.rmi.*;
public class AdditionClient {
public static void main (String[] args) throws Exception {
AdditionInterface add = (AdditionInterface) Naming.lookup("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC");
System.out.println("Result is :"+add.add(9, 10));
System.out.println(add.aff(26, 45));
}
}
Any help please? Thank you.
As in both a Client.AdditionInterface for the client and a Server.AdditionInterface for the server? Effectively the error is saying Server.AdditionInterface cannot be cast to Client.AdditionInterface.
With RMI you must have the same interface class on both client and server, but then clearly the server implementation class (AdditionServer) just on the server. The interface could be in a shared package (e.g. common.AdditionInterface ).
Related
Here is the interface
package Server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RmiSomme extends Remote{
public abstract void getInformation(String nom, String prenom, int age) throws RemoteException;
}
Here is the inplementation
package Server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class RmiSommeImpl extends UnicastRemoteObject implements RmiSomme{
private static final long serialVersionUID = 1L;
protected RmiSommeImpl() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
#Override
public void getInformation(String nom, String prenom, int age) throws RemoteException {
// TODO Auto-generated method stub
System.out.println(nom + " " + prenom);
}
}
and this is the server class
package Server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class Server {
public static void main(String[] args) throws RemoteException, MalformedURLException {
try {
RmiSommeImpl rmi = new RmiSommeImpl();
LocateRegistry.createRegistry(11);
Naming.rebind("rmi://localhost:11/RMI", rmi);
System.out.println(rmi.toString());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
When I start the server class this error keep showing up :
Unable to make public abstract void Server.RmiSomme.getInformation(java.lang.String,java.lang.String,int) throws java.rmi.RemoteException accessible: module RMIServer does not "exports Server" to module java.rmi
package RMI_Package;
import java.rmi.server.*;
import java.rmi.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public String sayHello(){
return "Server says,'Hey'";
}
public MyRemoteImpl() throws RemoteException{}
public static void main(String [] args){
try{
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello",service);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
This code is from Head First Java Book when i run it, it throws the java.net.MalformedURLException.
As specified by the Naming documentation, the first parameter of bind should be a valid URL.
As an example (taken from here):
Naming.bind("rmi://localhost:8800/YourObject", service);
This question already has an answer here:
RMI connection refused on localhost
(1 answer)
Closed 5 years ago.
I am developing a simple server based calculation program in java and rmi. However, The Server is not initializing with the following exception.
Initializing Server
Remote Server Error:Connection refused to host: 192.168.1.3; nested exception is:
java.net.ConnectException: Connection refused: connect
I have turned off my microsoft firewall. After that also, the problem persists. Here is the code I am running :
import java.rmi.*;
import java.rmi.Naming.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
import java.util.*;
interface mathInterface extends Remote
{
public int add(int a,int b) throws RemoteException;
public int subt(int a,int b) throws RemoteException;
public int mult(int a,int b) throws RemoteException;
public int div(int a,int b) throws RemoteException;
}
public class mathServer extends UnicastRemoteObject implements mathInterface
{
public mathServer() throws RemoteException
{
System.out.println("Initializing Server");
}
public int add(int a,int b)
{
return(a+b);
}
public int subt(int a,int b)
{
return(a-b);
}
public int mult(int a,int b)
{
return(a*b);
}
public int div(int a,int b)
{
return(a/b);
}
public static void main(String args[])
{
try
{
mathServer ms=new mathServer();
java.rmi.Naming.rebind("MathServ",ms);
System.out.println("Server Ready");
}
catch(RemoteException RE)
{
System.out.println("Remote Server Error:"+ RE.getMessage());
System.exit(0);
}
catch(MalformedURLException ME)
{
System.out.println("Invalid URL!!");
}
}
}
Kindly help me sort out this issue.
You haven't started the RMI Registry.
My problem is simple: I need to access variable history (which is declared in class BinaryServer) from another class.I'm using more classes to run this code.It's just simple client and server made of sockets.Client sends to server binary code/text and server translates it to text/binary code and sends it back to client.I can provide all classes if needed.
BinaryServer class
import java.net.*;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import graphics.gui;
public class BinaryServer extends gui implements ActionListener,Runnable
{
private ServerSocket server;
private Socket client;
public String text;
private BufferedReader reader;
public static ArrayList<String> history;
public static String binary_letter;
public static String[] letter;
public static int i;
public static String[] binary;
public static String sendback;
public static void main(String[] args)throws IOException
{
BinaryServer instance=new BinaryServer();
gui.buildframe(310,360,"Binary translator server");
gui.buildpane(300,300,true);
gui.buildbutton(300,20,"Translate");
instance.server(63400);
}
public void server(int port)throws IOException
{
history=new ArrayList<String>(100);
server=new ServerSocket(port);
button.addActionListener(this);
while(true)
{
client=server.accept();
reader=new BufferedReader(new InputStreamReader(client.getInputStream()));
text=reader.readLine();
history.add(text);
message.setText(message.getText()+"\n"+text+": ");
}
}
#Override
public void actionPerformed(ActionEvent e)
{
Thread response=new Thread(new BinaryServer());
if(text.contains("0"))
{
int length=text.length();
letter=new String[length+1];
sendback="";
int begin=-8;
int end=0;
for(i=1;i<=length/8;i++)
{
begin=begin+8;
end=i*8;
binary_letter=text.substring(begin,end);
Libary.translate();
message.setText(message.getText()+letter[i]);
sendback=sendback+letter[0+i];
}
}
else
{
int length=text.length();
letter=new String[length+1];
binary=new String[length+1];
sendback="";
for(i=1;i<=length;i++)
{
letter[i]=text.substring(i-1,i);
Libary.encode();
message.setText(message.getText()+binary[i]);
sendback=sendback+binary[0+i];
}
}
response.start();
}
public void run()
{
try
{
Socket feedback=new Socket("localhost",63403);
PrintWriter writer=new PrintWriter(feedback.getOutputStream(),true);
writer.println(sendback);
feedback.close();
return;
}
catch(IOException exc)
{
System.out.println("");
}
}
}
BinaryHistory class (The one I want access variable from)
public class BinaryHistory
{
public static void main(String[] args)
{
show();
}
public static void show()
{
System.out.println(BinaryServer.history);
}
When I access variable history from class BinaryHistory, it's alway null.
If you only declare the variable, regardless of the type or whether or not it's static, it will get a default value of null.
You have to initialize the variable too:
public static ArrayList<String> history = new ArrayList<>();
I'm implementing a simple RMI Server Client program in JAVA. I'm new to this actually. i have four java files.
Stack.java
import java.rmi.*;
public interface Stack extends Remote{
public void push(int p) throws RemoteException;
public int pop() throws RemoteException;
}
StackImp.java
import java.rmi.*;
import java.rmi.server.*;
public class StackImp extends UnicastRemoteObject implements Stack{
private int tos, data[], size;
public StackImp()throws RemoteException{
super();
}
public StackImp(int s)throws RemoteException{
super();
size = s;
data = new int[size];
tos=-1;
}
public void push(int p)throws RemoteException{
tos++;
data[tos]=p;
}
public int pop()throws RemoteException{
int temp = data[tos];
tos--;
return temp;
}
}
RMIServer.java
import java.rmi.*;
import java.io.*;
public class RMIServer{
public static void main(String[] argv) throws Exception{
StackImp s = new StackImp(10);
Naming.rebind("rmi://localhost:2000/xyz", s);
System.out.println("RMI Server ready....");
System.out.println("Waiting for Request...");
}
}
RMIClient.java
import java.rmi.*;
public class RMIClient{
public static void main(String[] argv)throws Exception{
Stack s = (Stack)Naming.lookup("rmi://localhost:2000/xyz");
s.push(25);
System.out.println("Push: "+s.push());
}
}
I'm using JDK1.5. The sequence in which i compiled the files is, first i compiled Stack.java then i compiled StackImp.java then i used this command rmic StackImp this all was successful. But when i tried to run the registry this way rmiregistery 2000, command prompt took too long. Nothing happened. I'm doing this all at my home PC. And this PC is not on the network. Please suggest me what to do to successfully work with this program.
command prompt took too long. Nothing happened.
Nothing is supposed to happen - the registry is running, and you can now start your server from another command prompt.
Alternatively, if you're only running the one RMI server process on this machine you can run the registry in the same process as the RMI server:
import java.rmi.*;
import java.rmi.registry.*;
import java.io.*;
public class RMIServer{
public static void main(String[] argv) throws Exception{
StackImp s = new StackImp(10);
Registry reg = LocateRegistry.createRegistry(2000);
reg.rebind("xyz", s);
System.out.println("RMI Server ready....");
System.out.println("Waiting for Request...");
}
}
This way you don't need a separate rmiregistry command, just run the server (which includes the registry) and then the client (which talks to the registry that is running in the server process).