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.
Related
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 ).
I am trying to setup a simple rmi server
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.*;
public class EmployeeServer extends UnicastRemoteObject implements Employee{
private String name,id;
private int salary;
private static final long serialVersionUID=1L;
public EmployeeServer(String n,String i,int s) throws RemoteException {
super();
name=n;
id=i;
salary=s;
}
public String getName() throws RemoteException {
return name;
}
public String getID() throws RemoteException {
return id;
}
public int getSalary() throws RemoteException {
return salary;
}
public int augmentSalary(int howMuch) throws RemoteException {
if(howMuch>0){
salary+=howMuch;
}
return salary;
}
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
System.setSecurityManager(new SecurityManager());
EmployeeServer server=new EmployeeServer("Red","0123",5000);
Naming.rebind("Red", server);
System.out.println("Server Ready!");
}
}
With this code alone i was getting the error
Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
After A LOT of googling i finally wrote my policy file
grant codeBase "file:/Utenti/Super/workspacenew/PDEsercizioStubSkeleton2.0/bin/EmployeeServer.class"
{
permission java.net.SocketPermission "127.0.0.1:1099", "connect, resolve";
};
And put it in my project folder "PDEsercizioStubSkeleton2.0".
Now i was trying to include this policy file in my jvm run configurations putting this in the VM arguments:
java -Djava.security.manager -Djava.security.policy=server.policy
But then it gives me the "Error: could not find or load the main class java".
I found out it just checks the first word wrote, because i modified "java" with "jaa" and got the same error with the misspelled name. What am i doing wrong?
I have 2 projects. One works fine in ever department. I downloaded and modified it to better understand it. The 2nd one is a project in development phase.
Now, both these projects have almost exactly the same RMI package, which works fine in the first project, but not in the 2nd.
My test classes in each package are essentially identical as well.
The main difference is what objects there are attempting to access, which are both interfaces in a database package.
Now, the database package in the 2nd project otherwise works absolutely fine, it just wont work with the RMI.
In short:
database package works fine
RMI package works fine
RMI package and database together does not work fine.
Here is my DBInterface
public interface DB extends Remote {
public String[] read(int recNo) throws RecordNotFoundException;
public void update(int recNo, String[] data, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;
public void delete(int recNo, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;
public int[] find(String[] criteria);
public int create(String[] data) throws DuplicateKeyException, IOException;
public long lock(int recNo) throws RecordNotFoundException;
public void unlock(int recNo, long cookie)
throws RecordNotFoundException, SecurityException;
}
and here is my RMIInterface
public interface RMIInterface extends Remote{
public DB getClient() throws RemoteException;
}
My RMIImplementation
public class RMIImplementation extends UnicastRemoteObject
implements RMIInterface {
private static String dbLocation = null;
private DB a;
public RMIImplementation() throws RemoteException{
}
public RMIImplementation(String dbLocation) throws RemoteException{
System.out.println(dbLocation);
this.dbLocation = dbLocation;
}
public static DB getRemote(String hostname, String port)
throws RemoteException {
String url = "rmi://" + hostname + ":" + port + "/DvdMediator";
try {
RMIInterface factory
= (RMIInterface) Naming.lookup(url);
// at this point factory equals Proxy[RMIInterface,................etc
// i want the return to equal Proxy[DB,..............etc
return (DB) factory.getClient();
} catch (NotBoundException e) {
throw new RemoteException("Dvd Mediator not registered: ", e);
}
catch (java.net.MalformedURLException e) {
throw new RemoteException("cannot connect to " + hostname, e);
}
}
public DB getClient() throws RemoteException {
try {
a = new ContractorDatabase(dbLocation);
}
catch(Exception e){
System.out.println("NewClass exception: " + e.toString());
}
return a;
}
And my the RMI registry
public class RegDvdDatabase {
private RegDvdDatabase() {
}
public static void register()
throws RemoteException {
register(".", java.rmi.registry.Registry.REGISTRY_PORT);
}
public static void register(String dbLocation, int rmiPort)
throws RemoteException {
Registry r = java.rmi.registry.LocateRegistry.createRegistry(rmiPort);
r.rebind("DvdMediator", new RMIImplementation(dbLocation));
}
}
Getting these two to work together throws a
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to sjdproject.remote.RMIImplementation
Can u please help me find the database issue that prevents it from working.
You must cast it to the remote interface.
EDIT The Registry reference r in your server code must be static. I can't see any good reason for locating the client lookup code inside the implementation class. That class should only exist at the server, not the client.
If you dont have a debugger, I would suggest using reflection on the provided object and see which interfaces it implements. It appears to be a proxy object, so must implement some interfaces.
for(Class clazz : factory.getClass().getInterfaces()) {
System.out.println(clazz.getSimpleName());
}
My suspicion with multiple deployments is of course the jvm version and the classpath. Can you verify that they match?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have problem that when i run my rmi server i got an exception notbound exception
even i export the remote object and bind it to registry
here is my remote interface code
import java.rmi.Remote;
public interface fact extends Remote {
public int factory(int a);
}
and here the interface implementation
public class factimport implements fact {
#Override
public int factory(int a) {
int mult=1;
for (int i=1;i<=a;i++)
mult=mult*i;
return mult;
}
}
and server code
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject {
/**
*
*/
private static final long serialVersionUID = 1L;
protected Server() throws RemoteException {
super();
}
public static void main() throws RemoteException, MalformedURLException{
factimport fi=new factimport();
Registry reg=LocateRegistry.createRegistry(1099);
reg.rebind("factobject", exportObject(fi));
System.out.println("server started");
}
}
and client
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
/**
* #param args
* #throws NotBoundException
* #throws RemoteException
* #throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
// TODO Auto-generated method stub
Registry reg=LocateRegistry.getRegistry("127.0.0.1",1099);
factimport x=(factimport)reg.lookup("factobject");
System.out.println(x.factory(5));
}
}
Unless you're running the server and client on the same host, which makes RMI pretty pointless, your getRegistry() call in the client needs to be modified to point to the server host, not the client host (itself).
Your remote method must be declared to throw RemoteException in the remote interface.
Unless you have run rmic on the remote interface, which you can't have done or you would have detected (2), you need to change the exportObject() call to exportObject(fi, 0), for reasons explained in the preamble to the Javadoc for UnicastRemoteObject.
Your Server.main() method doesn't have a correct signature so it won't execute. It should be public static void main(String[] args) ...
You should make the Registry reg variable in the server static to prevent it from being garbage-collected.
In your client, your variable of type factimport should be of type fact, and you should cast the lookup result to fact.
If the BindException was really the result of running this code in this state on a single machine, it can only mean that you ignored runtime exceptions when starting the server.
Your code is a little unorthodox. I don't know if this will help, but it might.
Normally, the remote object implementation would extend UnicastRemoteObject. So, you have this factimport class:
public class factimport implements fact extends UnicastRemoteObject {
#Override
public int factory(int a) {
int mult=1;
for (int i=1;i<=a;i++)
mult=mult*i;
return mult;
}
}
And the server class:
public class Server {
/**
*
*/
private static final long serialVersionUID = 1L;
protected Server() throws RemoteException {
super();
}
public static void main() throws RemoteException, MalformedURLException{
factimport fi=new factimport();
Registry reg=LocateRegistry.createRegistry(1099);
reg.rebind("factobject", fi);
System.out.println("server started");
}
}
I hope this will work.
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).