java.rmi.server.ExportException:端口已在使用:1099;
我正在编写一个JavaRMI程序来计算数字的阶乘 这是接口:java.rmi.server.ExportException:端口已在使用:1099;,java,server,rmi,Java,Server,Rmi,我正在编写一个JavaRMI程序来计算数字的阶乘 这是接口: import java.rmi.Remote; import java.rmi.RemoteException; public interface FactorialRMI extends Remote{ public void setNumber(int val) throws RemoteException; public int calculateFactorial() throws RemoteExcept
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;
}
}
那么这就是实现接口的类:
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;
}
}
以下是客户端代码:
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());
}
}
}
我没有使用eclipse。
我需要在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
java.rmi.server.ExportException:端口已在使用中:1099;嵌套异常是:
java.net.BindException:地址已在使用:net\u Bind
位于java.rmi/sun.rmi.transport.tcp.tcpttransport.listen(tcpttransport.java:335)
位于java.rmi/sun.rmi.transport.tcp.tcpttransport.exportObject(tcpttransport.java:243)
位于java.rmi/sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
位于java.rmi/sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
位于java.rmi/sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:234)
位于java.rmi/sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:220)
位于java.rmi/sun.rmi.registry.RegistryImpl$2.run(RegistryImpl.java:196)
位于java.rmi/sun.rmi.registry.RegistryImpl$2.run(RegistryImpl.java:193)
位于java.base/java.security.AccessController.doPrivileged(AccessController.java:689)
位于java.base/java.security.AccessController.doPrivileged(AccessController.java:873)
位于java.rmi/sun.rmi.registry.RegistryImpl。(RegistryImpl.java:193)
位于java.rmi/sun.rmi.registry.RegistryImpl$5.run(RegistryImpl.java:531)
位于java.rmi/sun.rmi.registry.RegistryImpl$5.run(RegistryImpl.java:529)
位于java.base/java.security.AccessController.doPrivileged(AccessController.java:689)
位于java.rmi/sun.rmi.registry.RegistryImpl.createRegistry(RegistryImpl.java:528)
位于java.rmi/sun.rmi.registry.RegistryImpl.main(RegistryImpl.java:551)
原因:java.net.BindException:地址已在使用中:net\u Bind
位于java.base/java.net.PlainSocketImpl.bind0(本机方法)
位于java.base/java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:132)
位于java.base/java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:436)
位于java.base/java.net.ServerSocket.bind(ServerSocket.java:386)
位于java.base/java.net.ServerSocket(ServerSocket.java:248)
位于java.base/java.net.ServerSocket(ServerSocket.java:140)
位于java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createServerSocket(TCPDirectSocketFactory.java:45)
位于java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:666)
位于java.rmi/sun.rmi.transport.tcp.tcpttransport.listen(tcpttransport.java:324)
... 还有15个
如何解决此问题?如果您在windows上运行,您可以找到在端口上运行的进程,并使用下面的命令终止该进程,以便释放该端口
netstat -ano | findstr :1099
taskkill /pid "EnterProcessIdHere" /F
对于linux
lsof -i :1099
kill EnterProcessIdHere
我尝试过这个命令“netstat-ano | findstr:1099”,但是没有任何东西显示RMI注册表已经在运行,或者其他非法使用同一端口的东西。NB“我被要求在jdk的bin文件夹中运行这个程序”:设置这个要求的人不知道他在说什么。NB你的API设计很差。它无法正确支持多个客户端。您只需要一个方法:
calculateFactorial(int number)
,而不需要number
字段。