Java RMI – How to setup java.rmi.server.codebase in Eclipse

A typical problem when running and building java RMI Applications is the following Exception:

Registering server with rmiregistry
Error happened: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: com.ibm.pbo.rmi.HelloInterface

To handle that problem, you have to set the following JVM Argument when running the ServerApplication:

-Djava.rmi.server.codebase=file:/abc/def

When using Eclipse, you can set this Argument in the Run Configuration:

Right Click on the ServerApplication java file -> Run As -> Run Configuration

Go to the Arguments Tab and insert the codebase argument in the VM Arguments Section:

 

Be careful with the trailing slash at the end. This slash is NECESSARY.

When I tried to point to the src directory of my Project it did not work, but if it points to the bin directory it worked fine.

-Djava.rmi.server.codebase=file:/home/philboss/Projekte/Eclipse_workspace/JavaRMI/bin/

Have fun with Java RMI 🙂

versioned jar with ant

Versioned jars (like MyCode-1.2.jar) can be easily generated with ant.

First you have to set the necessary Properties:

<property name="major-version-number" value="0" />
<property name="project-name" value="${ant.project.name}" />
<property name="src" value="src"/>
<property name="build" value="bin" />

Then you need a compile-task:

<target name="compile" description="compile the source" >
    <javac srcdir="${src}" destdir="${build}"/>
</target>

Then you need a method for generating jars:

<target name="jar-version">
  <buildnumber />
  <property name="version-number" value="${major-version-number}.${build.number}" />
  <jar basedir="bin" destfile="build-version/${project-name}-${version-number}.jar">
    <manifest>
     <attribute name="Built-By" value="${builder}" />
     <attribute name="Built-On" value="${build-info.current-date}" />
     <attribute name="Built-At" value="${build-info.current-time}" />
     <attribute name="Implementation-Version" value="${version-number}" />
    </manifest>
  </jar>
</target>

At last you need a method that compiles the files, generates the build-number and the jar-File:

<target name="build-jar-version">
    <antcall target="compile" />
    <mkdir dir="build-version" />
    <antcall target="jar-version" />
</target>