Charbel Alam
Charbel Alam

Reputation: 35

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?

Upvotes: 3

Views: 16103

Answers (2)

Natnael
Natnael

Reputation: 1

I had the same problem as this: if you use port number 8080 for RMI-related codes, you will never get this kind of error.

Upvotes: -1

sanjeevRm
sanjeevRm

Reputation: 1606

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

Upvotes: 5

Related Questions