RMI(java.net.ConnectException: Connection refused: connect) - java

I am trying to run my server named SampleServer. I am using windows and this is what i did:
in cmd:
javaw rmiregistry 1099
cd C:\Users\Home\workspace\RMI\src
java -Djava.security.policy=policy SampleServer 1099
i get the following error:
binding //localhost:1099/Sample
New instance of Sample created
Sample server failed:Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
I've tried using a different port # such as 4719 for rmiregistry but i receive the same error. I made sure that my firewall was disabled but the problem persist. I made sure that the port is not already being used. I really hope someone can help me.
Picture of my desktop with folders of project, eclipse window and cmd open:
http://s22.postimg.org/uq00qzslr/picyture.png
Code:
SampleServer:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class SampleServer {
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("usage: java SampleServer rmi_port");
System.exit(1);
}
// Create and install a security manager
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
// first command-line argument is the port of the rmiregistry
int port = Integer.parseInt(args[0]);
String url = "//localhost:" + port + "/Sample";
System.out.println("binding " + url);
Naming.rebind(url, new Sample());
// Naming.rebind("Sample", new Sample());
System.out.println("server " + url + " is running...");
}
catch (Exception e) {
System.out.println("Sample server failed:" + e.getMessage());
}
}
}
SampleClient:
import java.rmi.Naming;
import java.rmi.RemoteException;
public class SampleClient {
public static void main(String args[]) {
try {
if (args.length < 3) {
System.err.println("usage: java SampleClient host port string... \n");
System.exit(1);
}
int port = Integer.parseInt(args[1]);
String url = "//" + args[0] + ":" + port + "/Sample";
System.out.println("looking up " + url);
SampleInterface sample = (SampleInterface)Naming.lookup(url);
// args[2] onward are the strings we want to reverse
for (int i=2; i < args.length; ++i)
// call the remote method and print the return
System.out.println(sample.invert(args[i]));
} catch(Exception e) {
System.out.println("SampleClient exception: " + e);
}
}
}
SampleInterface:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SampleInterface extends Remote {
public String invert(String msg) throws RemoteException;
}
Sample:
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.*;
// this is the class with remote methods
public class Sample
extends UnicastRemoteObject
implements SampleInterface {
private int a;
public Sample() throws RemoteException {
System.out.println("New instance of Sample created");
a = 1;
}
public String invert(String m) throws RemoteException {
// return input message with characters reversed
System.out.println("invert("+m+") a=" + a);
return new StringBuffer(m).reverse().toString();
}
}
policy:
grant {
permission java.security.AllPermission;
};

javaw rmiregistry 1099
Stop right there. This is already wrong. 'rmiregistry' is an executable, not the name of a Java class you can execute with 'java' or 'javaw'. Just use 'rmiregistry'.

This error occurs when there is no service running on the port, you are trying to connect. As said by EJP, rmiregistry is a tool which can be started by rmiregistry & in the background (JDK 7). I would recommend you that you check your firewall or connectivity issue with the port.

Related

Java RMI permission denied to LAN address, works for 127.0.0.1

I am implementing a Client application and a Server application on my Windows computer using two terminals that communicate with each other.
However I cannot get the Client to run. The Server and Java RMI registry run successfully. I have 3 files: Client.java, Server.java and Hello.java
import java.rmi.registry.LocateRegistry; //Client.java
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.registry.Registry; //Server.java
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.Remote; //Hello.java
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
All three Java files are located in C:\Program Files\Java\jdk1.8.0_321\bin. After compiling them to the same directory, I run the following commands in the command prompt as administrator.
start rmiregistry -J-Djava.class.path=./
Java RMI registry starts successfully in a new command prompt window (no output).
start java Server
Server starts sucessfully in a new command prompt which outputs "Server ready".
java Client
This command is unsuccessful and outputs the following error:
java.net.SocketException: Permission denied: connect
java.rmi.ConnectIOException: Exception creating connection to: 192.168.1.13; nested exception is:
java.net.SocketException: Permission denied: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:635)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:131)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:235)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:180)
at com.sun.proxy.$Proxy0.sayHello(Unknown Source)
at Client.main(Client.java:15)
Caused by: java.net.SocketException: Permission denied: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at java.net.Socket.connect(Socket.java:555)
at java.net.Socket.<init>(Socket.java:451)
at java.net.Socket.<init>(Socket.java:228)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:617)
... 7 more
C:\Program Files\Java\jdk1.8.0_321\bin>_
I am unsure how to get the Client running successfully. I disabled windows firewall and am not using a VPN.

java.rmi.server.ExportException: Port already in use: 1099;

I am writing a Java RMI program to calculate the factorial of a number.
This is the interface:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FactorialRMI extends Remote{
public void setNumber(int val) throws RemoteException;
public int calculateFactorial() throws RemoteException;
}
Then this is the class that implements the interface:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class FactorialRMIImpl extends UnicastRemoteObject implements FactorialRMI{
private int number;
public FactorialRMIImpl(String name) throws RemoteException {
super();
try {
Naming.rebind("myFactorial",this);
}catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
#Override
public void setNumber(int val) throws RemoteException {
this.number=val;
}
#Override
public int calculateFactorial() throws RemoteException {
int p=1;
for(int i=1;i<=this.number;i++)
p=p*i;
return p;
}
}
Here is the server code:
public class FactorialRMIServer {
public static void main(String [] args) {
try {
FactorialRMIImpl myFactorial=new FactorialRMIImpl("myFactorial");
System.out.println("Server is ready...");
}catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
}
And here is the client code:
import java.rmi.Naming;
import java.util.*;
public class FactorialRMIClient {
public static void main(String [] args) {
try {
FactorialRMI myFact=(FactorialRMI)Naming.lookup("myFactorial");
Scanner scan=new Scanner(System.in);
System.out.println("Enter a number to calculate its factorial: ");
int n=scan.nextInt();
myFact.setNumber(n);
System.out.println("The factorial of this number is: "+myFact.calculateFactorial());
}catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
}
I am not using eclipse.
i am required to run this program in the bin folder of the jdk.
i am using jdk1.8
to compile the java file i put these file in the bin folder and run the command:
javac *.java
then i run the commands
rmic FactorialRMIImpl
start rmiregistry
start java FactorialRMIServer
start java FactorialRMIClient
in the last 2 command i get this error:
Exception: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
The command start rmiregistry gives me an error:
java.rmi.server.ExportException: Port already in use: 1099; nested exception is:
java.net.BindException: Address already in use: NET_Bind
at java.rmi/sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:335)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:243)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
at java.rmi/sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
at java.rmi/sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:234)
at java.rmi/sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:220)
at java.rmi/sun.rmi.registry.RegistryImpl$2.run(RegistryImpl.java:196)
at java.rmi/sun.rmi.registry.RegistryImpl$2.run(RegistryImpl.java:193)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:689)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:873)
at java.rmi/sun.rmi.registry.RegistryImpl.<init>(RegistryImpl.java:193)
at java.rmi/sun.rmi.registry.RegistryImpl$5.run(RegistryImpl.java:531)
at java.rmi/sun.rmi.registry.RegistryImpl$5.run(RegistryImpl.java:529)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:689)
at java.rmi/sun.rmi.registry.RegistryImpl.createRegistry(RegistryImpl.java:528)
at java.rmi/sun.rmi.registry.RegistryImpl.main(RegistryImpl.java:551)
Caused by: java.net.BindException: Address already in use: NET_Bind
at java.base/java.net.PlainSocketImpl.bind0(Native Method)
at java.base/java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:132)
at java.base/java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:436)
at java.base/java.net.ServerSocket.bind(ServerSocket.java:386)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:248)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:140)
at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createServerSocket(TCPDirectSocketFactory.java:45)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:666)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:324)
... 15 more
How can I solve this?
if you are running on windows, you can find the process running on port and kill that process using below command, so that port will be freed
netstat -ano | findstr :1099
taskkill /pid "EnterProcessIdHere" /F
For linux
lsof -i :1099
kill EnterProcessIdHere

ERROR CANNOT FIND SYMBOL when compiling a Java file

I want to create a RMI application. I set the java path in the System Variables and started with these steps
javac Hello.java
javac HelloImpl.java
javac HelloServer.java
javac HelloClient.java
start rmiregistry
start java -Djava.security.policy=policy Server
start java -Djava.security.policy=policy Client
But in the second step I have this problem
C:\Users\HP\eclipse\SimpleRMIExample\src>javac Hello.java
C:\Users\HP\eclipse\SimpleRMIExample\src>javac HelloImpl.java
HelloImpl.java:4: error: cannot find symbol
public class HelloImpl extends UnicastRemoteObject implements Hello {
^
symbol: class Hello
1 error
Code
//Hello.java
import java.rmi.*;
public interface Hello extends Remote {
public String getGreeting() throws RemoteException;
}
//HelloImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
// No action needed here.
}
public String getGreeting() throws RemoteException {
return ("Hello there!");
}
}
//HelloServer.java
import java.rmi.*;
public class HelloServer {
private static final String HOST = "localhost";
public static void main(String[] args) throws Exception {
// Create a reference to an implementation object...
HelloImpl temp = new HelloImpl();
// Create the string URL holding the object's name...
String rmiObjectName = "rmi://" + HOST + "/Hello";
// (Could omit host name here, since 'localhost‘ would be assumed by default.)
// 'Bind' the object reference to the name...
Naming.rebind(rmiObjectName, temp);
// Display a message so that we know the process has been completed...
System.out.println("Binding complete...\n");
}
}
//HelloClient.java
import java.rmi.*;
public class HelloClient {
private static final String HOST = "localhost";
public static void main(String[] args) {
try {
// Obtain a reference to the object from the registry and typecast it into the
// appropriate
// type...
Hello greeting = (Hello) Naming.lookup("rmi://" + HOST + "/Hello");
// Use the above reference to invoke the remote object's method...
System.out.println("Message received: " + greeting.getGreeting());
} catch (ConnectException conEx) {
System.out.println("Unable to connect to server!");
System.exit(1);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
}
Problem finally solved here are the steps
Edit the System Environment Variables -> Environment Variables -> User variables
-> CLASSPATH (add if not found) -> path-project-bin-folder
(C:\Users\HP\eclipse\SimpleRMIExample\bin)
for 1st time only
Reboot
for 1st time only
Clean Project
Command: start rmiregistry (on bin folder of your project)
Run project

connecting client-server RMI

I am confused on how the client actually makes the connection to the server if the server is remote(not on the same machine as the client). My code works fine using localhost but i cant figure out how the client actually makes the connection to the server host so that it looks up the rmiregistry. I'm confused on what gets stored in the registry for the server, is it Sample or localhost? This may be dumb but i tried to convert localhost to its ipaddress on the client side and do String url = "//" + server + ":" + 1099 + "/Sample"; where server is the ip from getbyname() but i get a exception: java.rmi.NotBoundException: 127.0.0.1:1099/Sample
That was with the client and server on both machines. I'm just trying to figure out how the two connect remotely but it didn't even work on the same machine using the ip address of localhost.
Client:
import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class SampleClient {
public static void main(String args[]) {
String url = "//" + "localhost" + ":" + 1099 + "/Sample";
SampleInterface sample = (SampleInterface)Naming.lookup(url);
} catch(Exception e) {
System.out.println("SampleClient exception: " + e);
}
}
}
Server:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class SampleServer {
public static void main(String args[]) throws IOException {
// Create and install a security manager
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
String url = "//localhost:" + 1099 + "/Sample";
System.out.println("binding " + url);
Naming.rebind(url, new Sample());
// Naming.rebind("Sample", new Sample());
System.out.println("server " + url + " is running...");
}
catch (Exception e) {
System.out.println("Sample server failed:" + e.getMessage());
}
}
}
The server should bind to a Registry running on 'localhost'.
The client should lookup a Registry at the server host.
It's as simple as that.
I'm confused on what gets stored in the registry for the server, is it Sample or localhost?
Neither. You're confusing three different things:
The hostname, in this case 'localhost'.
The bind-name, in this case 'Sample'.
The object which is bound, which is the remote stub.

how to solve java.rmi.ConnectException in java program?

i have created one simple java RMI program for understanding how it works.But when i trying to run my server side it raising the following exception.
EDIT: We are using proxy connection...
Remote exception: java.rmi.ConnectException: Connection refused to host: 10.7.150.18; nested exception is:
java.net.ConnectException: Connection refused: connect
This is my Server side code for your reference...
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SampleServerImpl extends UnicastRemoteObject implements SampleServer
{
SampleServerImpl() throws RemoteException
{
super();
}
#Override
public int sum(int a,int b) throws RemoteException
{
return a + b;
}
public static void main(String[] args)
{
try
{
//set the security manager
//System.setSecurityManager(new RMISecurityManager());
//create a local instance of the object
SampleServerImpl Server = new SampleServerImpl();
//put the local instance in the registry
Naming.rebind("//10.7.150.18:9999" , Server);
System.out.println("Server waiting.....");
}
catch (java.net.MalformedURLException me)
{
System.out.println("Malformed URL: " + me.toString());
}
catch (RemoteException re)
{
System.out.println("Remote exception: " + re.toString());
}
}
}
Please guide me to get out of this issue...
The Naming class provides methods for storing and obtaining references to remote objects in a remote object registry. Each method of the Naming class takes as one of its arguments a name that is a java.lang.String in URL format (without the scheme component) of the form:
//host:port/name
add a name with in the URL format
//10.7.150.18:9999/Server

Categories

Resources