// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package jndi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Demo {
    public static void main(String[] args) throws RemoteException, NamingException {
        LocateRegistry.createRegistry(8888);
        Hashtable<String,Object> props = new Hashtable<>();
        props.put(Context.INITIAL_CONTEXT_FACTORY,  "com.sun.jndi.rmi.registry.RegistryContextFactory");
        props.put(Context.PROVIDER_URL, "rmi://localhost:8888/");
        Context ctx = new InitialContext(props);
        ctx.bind("name", "This is a test");
        System.out.println(ctx.lookup("name"));
    }
}
S
