Java Repository
This site in NOT designed to be a Java Tutorial.
Contents:
javac - Java compiler.
Database:
javac |
Java compiler.
Download JDK 6 Update 7 from [1]. After install the package, set the System path variables PATH and CLASSPATH.
set PATH=%PATH%;c:\programme\jdk\bin
set CLASSPATH=.
The compiler can be called with
javac -d binaries file1.java file2.java
where -d <path> points to the output path that saves the compiled files.
Contents:
JavaRMI - Starting Using JavaTM RMI.
Database:
JavaRMI |
Starting Using JavaTM RMI.
The example below shows the use of JavaRMI to call object methods in a remote host. Explanations are found as comments in code.
Convert.java download/Convert.java.
package example.math; import java.rmi.Remote; import java.rmi.RemoteException; /*! Remote Interface Design called Convert. A remote interface extends the interface java.rmi.Remote and declares a set of remote methods. Each remote method must declare java.rmi.RemoteException (or a superclass of RemoteException) in its throws clause, in addition to any application-specific exceptions. The available method are: help - return to the caller a string with the equations used to convert from degreesC to Kelvin and vice versa C2K - return to the caller the correspondig Kelvin value from a given Temperature in degrees Celsius K2C - return to the caller the correspondig degrees Celsius value from a given Temperature in Kelvin. */ public interface Convert extends Remote { String help() throws RemoteException; double C2K(double Celsius) throws RemoteException; double K2C(double Kelvin) throws RemoteException; }
Server.java download/Server.java.
package example.math; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; /*! Server implements the Methods on the Convert interface. It has a main method that creates an instance of the remote object implementation, exports the remote object, and then binds that instance to a name in a Java RMI registry. */ public class Server implements Convert { public Server() {} //part of the class but not into the remote interface. /*! The Methods offered by the server are implemented below. The method sayHello does not need to declare that it throws any exception because the method implementation itself does not throw RemoteException nor does it throw any other checked exceptions*/ public String help() { return "\tdegrees Celsius = Kelvin - 273.15\n" "\tKelvin = degrees Celsius + 273.15"; } public double C2K(double C) { System.out.println("Convert.C2K running..."); return C+273.15; } public double K2C(double K) { System.out.println("Convert.K2C running..."); return K-273.15; } // End Methods implementation public static void main(String args[]) { try { /*! Create instance of Server and export it. The static method UnicastRemoteObject.exportObject exports the supplied remote object, see implementation of Convert.java, to receive incoming remote method invocations on an anonymous TCP port and returns the stub for the remote object to pass to clients. */ Server obj = new Server(); Convert stub = (Convert) UnicastRemoteObject.exportObject(obj, 0); /*! Bind the remote object's stub in the registry with the name 'ConvertTemp'. Once a remote object is registered on the server, callers can look up the object by name, obtain a remote object reference, and then invoke remote methods on the object. Default registry port is 1099. If no registry is running on TCP port 1099 of the local host when the bind method is invoked, the server will fail with a RemoteException.*/ Registry registry = LocateRegistry.getRegistry(); registry.bind("ConvertTemp", stub); System.err.println("Server listening..."); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } }
Client.java download/Client.java.
package example.math; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; /*! Client side program. */ public class Client { private Client() {} public static void main(String[] args) { //null is used associating with the local host address. String host = (args.length < 1) ? null : args[0]; try { /*! Connect with the remote host to obtains the stub for the registry by invoking the static LocateRegistry.getRegistry method. Next, the client invokes the remote method lookup on the registry stub to obtain the stub for the remote object from the server's registry. */ Registry registry = LocateRegistry.getRegistry(host); Convert stub = (Convert) registry.lookup("ConvertTemp"); /*! Call the methods on the remote object's stub and use the standard output to print incoming messages. */ System.out.println(stub.help()); System.out.println("30 degrees C => " + stub.C2K(30.) + " K"); System.out.println("333.15 K => " + stub.K2C(333.15) + " degrees C"); } catch (Exception e) { System.err.println("Client exception: " + e.toString()); e.printStackTrace(); } } }
Compile with javac, -d <path> give the output path to save the compiled files, in the example below binaries.
javac -d binaries Convert.java Client.java Server.java
To run the files
cd binaries
start rmiregistry
java -classpath . example.math.Server
java -classpath . example.math.Client
or
start java -classpath . example.hello.Client
Bibliography
1. Sun . Java SE Download . URL: http://java.sun.com/javase/downloads/index.jsp .