Hi,
I've just worked through the RMI tutorial from Sun's website and was wondering how other people found it.
(Its the one by jGuru, after a quick check back to the website to check the URL I notice that theres another one under the subheading
Java, which might have been better to start on),
(
http://developer.java.sun.com/developer/onlineTraining/rmi/ )
I had a few problems getting examples to run, some down to my PC's setup, but with others I felt the example could have had better instructions - but perhaps I was missing something (I don't really like web tutorials, I'm never sure if theres some important information hidden away behind a link that I've missed).
After the tutorial I decided to start writing my own code to help my brain get to grips with it, and started with the simplest RMI system I could think of.
In case its of use to anyone getting started with RMI, and also for the more selfish reason that I'd like to know if I'm misunderstanding things I thought I'd show my example of a simple RMI system, and how to run it, and also list a few of the problems (and solutions) I had in getting the example code from the tutorial.
First the problems with getting examples from the tutorial to run.
(I'm using a Windows 98 machine)
1. I couldn't get the class's to compile at the command prompt.
I fixed this by setting CLASSPATH in autoexec.bat to include the current directory. I've since experimented by removing the CLASSPATH setting in autoexec.bat, and can compile the class's at the commend prompt no problem. I've recently installed JDK 1.4, JBuilder 6, and reinstalled JDK 1.4 because JBuilder installation seemed to have messed up some registry setting to point to its version of the JDK, so perhaps one of these actions has removed the need for a CLASSPATH setting in autoexec.bat, I'm not sure.
2. I found that I was getting the following exception after starting the client :-
java.rmi.UnknownHostException: Unknown host: remotehost; nested exception is:
java.net.UnknownHostException: remotehost
I fixed this by replacing remotehost in the following line with localhost:1099 (I think just localhost would work as well, as 1099 is the default port)
Calculator c = (Calculator) Naming.lookup("rmi://remotehost/CalculatorService");
3. I then got the following NoRouteToHostException :-
C:\javatutorials\rmi\example>java CalculatorClient
RemoteException
java.rmi.ConnectIOException: Exception creating connection to: 4.0.0.3; nested exception is:
java.net.NoRouteToHostException: Host unreachable: connect
This was a problem specific to my PC's setup. I found out about using ipconfig at the dos prompt, to display ip details, which for me gave details about 3 adapters , one of which had address 4.0.0.3. The other two were explained by my having changed modems a while back , so had two modems installed. After a bit of playing around with the network settings, I noticed something called "Microsoft TV Data Adapter", which I tried removing and "hey presto!" the adapter with address 4.0.0.3 had gone and the calculator example worked.
(I've no idea what "Microsoft TV Data Adapter" is , but I haven't got anything to do with TV on my PC so I'm hoping it will be OK to have removed it!)
4. In later examples I found that it was necesarry to set a security policy. I did this by creating a text file called permit with the following in:-
and running the server with the as follows
java -Djava.security.policy=permit CalculatorServer
(I think this is something that has been required since JDK1.2)
The permit file probably has more than is strictly necesary, file permission probably isn't required for the examples.
=========================================================================================
Now for my simple RMI system.
See code below - SimpleClient.java, SimpleServerIntf.java and SimpleServer.java.
Put all three files in the same directory and open up three dos prompts, at that directory.
Compile each file.
(You may need to set CLASSPATH in your autoexec.bat e.g
SET CLASSPATH=.;C:\j2sdk1.4.0\jre\classes;C:\javatutorials\rmi\example;C:\java\rmi
C:\java\rmi\simple>javac SimpleServerIntf.java
C:\java\rmi\simple>javac SimpleServer.java
C:\java\rmi\simple>javac SimpleClient.java
Then create the skeleton and stub files for the SimpleServer class that are used by RMI to provide the link between the client and server. Do this using the rmic command as follows at a dos prompt.
C:\java\rmi\simple>rmic SimpleServer
If you're running a firewall disable it.
Now start the RMI registry using the command rmiregistry, no news is good news - if nothing happens then the registry is up and running OK.
In one of the other DOS windows start the server, output should be as follows :-
C:\java\rmi\simple>java SimpleServer
Creating server
SimpleServer constructor
Binding service
service bound
Then in another DOS window start a client :-
C:\java\rmi\simple>java SimpleClient
client starting
Got reference to Server
value got = 1
And thats it ! A working RMI system. Obviously if you've got a network to play with change localhost to the network name of the PC (or whatever) that the server is on.