<path_ to_java_home>\bin\javac.exe Calculator.java Biên dịch Implement và tạo Stub, Skeleton:. <path_ to_java_home>\bin\javac.exe CalculatorImpl.java[r]
(1)VÍ DỤ KỸ THUẬT RMI
Calculator.java
import java.rmi.*;
public interface Calculator extends Remote{ public int sum(int n) throws RemoteException;
// sum = 1+2+3+…+n
}
CalculatorImpl.java
import java.rmi.*;
public class CalculatorImpl implements Calculator{ public int sum(int n) throws RemoteException{
System.out.println("Client request to calculate!"); int s=0;
for (int i=1;i<n;i++) s=s+i; return s; }
}
CaclculatorServer.java
import java.rmi.server.*; import java.rmi.*;
public class CalculatorServer{
public static void main(String args[]){ try{
CalculatorImpl c=new CalculatorImpl(); System.out.println("Exporting Calculator "); UnicastRemoteObject.exportObject(c); Naming.bind("rmi://localhost/MyCalculator",c); System.out.println("Register Calculator!"); }catch(Exception e){
System.out.println(e); }
} }
CalculatorClient.java
import java.rmi.*;
public class CalculatorClient{
public static void main(String args[]){ try{
System.out.println("Finding Object ");
(2)//Call methods of c
System.out.println("Sum(12)= " + c.sum(12)); }catch(Exception e){
System.out.println(e); }
} }
Biên dịch:
Biên dịch interface:
<path_ to_java_home>\bin\javac.exe Calculator.java Biên dịch Implement tạo Stub, Skeleton:
<path_ to_java_home>\bin\javac.exe CalculatorImpl.java
<path_ to_java_home>\bin\rmic.exe CalculatorImpl.class –vcompat
Biên dịch Server Client:
<path_ to_java_home>\bin\javac.exe CalculatorServer.java <path_ to_java_home>\bin\javac.exe CalculatorClient.java
Thực thi:
Kích hoạt đăng ký RMI
<path_ to_java_home>\bin\rmiregistry.exe Kích hoạt server
<path_ to_java_home>\bin\java.exe CalculatorServer.class Kích hoạt client