Home » Java » RMI finally working

SHARE WITH FRIENDS:  
        
          

My research on how to use RMI wasn’t really a piece of cake.  In a first stage I tried an sample using the rmic to generate the stub which went fairly easy.  But afterwards I found out that this was not really the way to go since this solution was oldfashioned.  I than tried the general accepted way which is described on an Oracle tutorial

Coding went well since you don’t really have to do a lot but getting it to work that was another story.  Starting the service side of the story was a real pain.  You have to use a parameter called “java.rmi.server.codebase” at startup.  The codebase identifies the location of the remote class files or jar containing the classes.  Probably this definition is not 100% correct but it works for me.  Anyway.  I first had to setup a webserver (apache2) which serves the jar file so I can access it via a simple url.  Quite simple.  But the the proof of the pudding is in the eating so I started the server side and it complained that my remote class was not found.  I must have checked tens of times for typo’s but no clue what was wrong.  After some googling I found out that as of JDK 7 update 21, a parameter called “java.rmi.server.useCodebaseOnly” is set default to true, which was not the case before.  As a result (I use JDK7 update 40) the codebase parameter was ignored at startup.  Instead this parameter had to be foreseen for the rmiregistry command and than everything worked well.

So to summarize.  To start the server part you have to do the following (this is in the context of my concrete example)

  • rmiregistry -J-Djava.rmi.server.codebase=http://192.168.1.150/Example-1.5-SNAPSHOT.jar &
  • sudo java -cp Example-1.5-SNAPSHOT.jar:.:class:/opt/pi4j/lib/’*’:log4j-1.2.17.jar -Djava.rmi.server.hostname=192.168.1.150 be.byle.raspycar.RemoteTwoWheelDriveRaspyCar

Having got RMI working, it is time now to continue with the next steps and improving my code.

 

Have a nice Sunday evening,

Jan

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website