[英]Port already in use exception with java RMI using CMD
while Running RMI using java and CMD.使用 java 和 CMD 运行 RMI。 java.rmi.server.ExportException is displayed.
显示 java.rmi.server.ExportException。 The Exception is that, port is already in use.
例外是,端口已在使用中。
The server interface服务器接口
import java.rmi.*;
public interface AdditionInterface extends Remote {
public int add(int a, int b) throws RemoteException;
}
The Interface implementation接口实现
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionClient {
public static void main(String[] args) {
try {
String host="";
Registry registry = LocateRegistry.getRegistry(host);
AdditionInterface hello = (AdditionInterface) registry.lookup("Addition");
int result = hello.add(9, 2);
System.out.println("Result is: " + result);
} catch (Exception ex) {
System.out.println("HelloClient Exception" + ex);
}
}
}
The Server Registry class服务器注册表 class
package chapter40;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionServer {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry();
AdditionInterface obj = new Addition();
registry.rebind("Addition", obj);
System.out.println("Addition Server is ready");
} catch (Exception ex) {
System.out.println("Addition Server failed" + ex);
}
}
}
The client program客户端程序
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionClient {
public static void main(String[] args) {
try {
String host="";
Registry registry = LocateRegistry.getRegistry(host);
AdditionInterface hello = (AdditionInterface) registry.lookup("Addition");
int result = hello.add(9, 2);
System.out.println("Result is: " + result);
} catch (Exception ex) {
System.out.println("HelloClient Exception" + ex);
}
}
}
I separated the server from the client classes in two different projects for clarity sake为了清楚起见,我在两个不同的项目中将服务器与客户端类分开
I searched through various threads and finally got this to work in a way different from [ but also a combination from what I'd seen ]我搜索了各种线程,最后让它以不同于[但也是我所看到的组合]的方式工作
RUN THE SERVER PROGRAM THUS (from the command prompt):运行服务器程序(从命令提示符):
change the directory to the source folder for the server cd c:\Users\Heavenly\workspace\RMIServerSide\src
将目录更改为服务器的源文件夹
cd c:\Users\Heavenly\workspace\RMIServerSide\src
compile the classes javac chapter40/*.java
编译类
javac chapter40/*.java
start the RMI registry start rmiregistry
启动 RMI 注册表
start rmiregistry
start the registry server class containing the main() method.启动包含 main() 方法的注册表服务器 class。 NOTE THE LINE OF CODE IS SLIGHTLY DIFFERENT
注意代码行略有不同
start java -cp. chapter40.AdditionServer
start java -cp. chapter40.AdditionServer
(the server is now set) start java -cp. chapter40.AdditionServer
(服务器已经设置好了)
RUN THE CLIENT PROGRAM运行客户端程序
Change the directory to the source folder cd c:\Users\Heavenly\workspace\RMIClientSide\src
将目录更改为源文件夹
cd c:\Users\Heavenly\workspace\RMIClientSide\src
compile all the classes javac chapter40/*.java
编译所有类
javac chapter40/*.java
3.Run the client program java chapter40.AdditionClient
(everything is running now) 3.运行客户端程序
java chapter40.AdditionClient
(现在一切都在运行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.