Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Last active May 24, 2018 20:52
Show Gist options
  • Save fmbenhassine/942db98afa13a1e940a6 to your computer and use it in GitHub Desktop.
Save fmbenhassine/942db98afa13a1e940a6 to your computer and use it in GitHub Desktop.
JNDI hello world #lab
package io.github.benas.labs.javase.jndi;
import java.io.Serializable;
import java.rmi.Remote;
public class Foo implements Remote, Serializable {
private String bar;
public Foo(String bar) {
this.bar = bar;
}
public String getBar() {
return bar;
}
}
java.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory
java.naming.provider.url=rmi://localhost:2000
package io.github.benas.labs.javase.jndi;
import javax.naming.*;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws NamingException, IOException {
Context ctx = new InitialContext();
NamingEnumeration<NameClassPair> bindings = ctx.list(ctx.getNameInNamespace());
while (bindings.hasMore()) {
NameClassPair ncp = bindings.next();
System.out.println(ncp.getName());
}
Foo foo = new Foo("bar");
ctx.bind("foo", foo);
Foo f = (Foo) ctx.lookup("foo");
System.out.println("res = " + f.getBar());
ctx.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment