Search code examples
javarmi

Make a class file accessible via HTTP


How can I make a class file accessible over the network? The goal is to be able to download the .class file onto another machine and create an instance of that class. So if I am able to host the file from the working machine I can use URLClassLoader on the receiving machine to get it.


Solution

  • If you're really using RMI you can do it with the RMI codebase feature. Put your JAR file somewhere on an HTTP server, and start your server JVM with

     -Djava.rmi.server.codebase=<URL of the JAR file>
    

    Make sure the URL is correct: you can check it with a browser. It should try to start a download of the JAR, which you can cancel once you see it works. Then your client needs to be run under a SecurityManager and an appropriate .policy file.

    If you want to load client classes into the server, the client JVM needs to be started with the codebase argument, and the server needs the security manager and the .policy file. This is pretty unusual.

    When you have this configured correctly, it is all automatic. No ClassLoader code of yours required at all.