java.rmi.server.ExportException:端口已在使用中:1099;

问题描述

我正在编写一个 Java RMI 程序来计算一个数的阶乘。

这是界面:

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;
}

那么这是实现接口的类:

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;
    }

}

这是服务器代码

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());
        }
    }
}

这是客户端代码

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());
        }
    }
}

我没有使用日食。 我需要在 jdk 的 bin 文件夹中运行这个程序。 我正在使用 jdk1.8

为了编译java文件,我将这些文件放在bin文件夹中并运行命令:

javac *.java

然后我运行命令

rmic FactorialRMIImpl
start rmiregistry
start java FactorialRMIServer
start java FactorialRMIClient

在最后 2 个命令中,我收到此错误

Exception: Connection refused to host: 127.0.0.1; nested exception is: 
java.net.ConnectException: Connection refused: connect

命令 start rmiregistry 给了我一个错误

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

我该如何解决这个问题?

解决方法

如果你在windows上运行,你可以找到运行在端口上的进程并使用下面的命令杀死该进程,这样端口就会被释放

netstat -ano | findstr :1099
taskkill /pid "EnterProcessIdHere" /F

对于Linux

lsof -i :1099
kill EnterProcessIdHere