I'm following the tutorial: https://docs.oracle.com/javase/tutorial/jndi/index.html
My adventure started while setting a JNDI name for a datasource with the WildFly application server. The name started with "java:/". I was curious on what it was and how it worked.
I have Apache Directory LDAP server setup locally and I'm able to connect to it with:
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=JNDITutorial");
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
try {
Context ctx = new InitialContext(env);
Object obj = ctx.lookup("cn=Rosanna Lee,ou=People");
} catch (NamingException e) {
e.printStackTrace();
}
My confusion is the JNDI name "java:/".
Can someone please explain what "java:/" is and how I can use JNDI to interact with it?
My assumption is its a directory located somewhere on my computer.
Thank you.
The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and resources (in the form of Java objects) via a name. Like all Java APIs that interface with host systems, JNDI is independent of the underlying implementation.
To view this administrative console page, click Applications > Application Types > WebSphere enterprise applications > application > EJB JNDI names.
The names of system-provided objects, such as JTA UserTransaction objects, are stored in the environment naming context java:comp/env. The Java EE platform allows a component to name user-defined objects, such as enterprise beans, environment entries, JDBC DataSource objects, and message connections.
A name that is bound within a context is the JNDI name of the object. In Specifying a Resource Reference, for example, the JNDI name for the JDBC resource (or data source) is jdbc/ejbTutorialDB .
The explanation is in the name: JNDI is the "Java Naming and Directory Interface". It is part of the Java EE specification and provides an API for java clients to discover and look up data and objects by name. These objects are accessible via certain contexts, e.g.
The names of system-provided objects, such as JTA UserTransaction objects, are stored in the environment naming context java:comp/env. The Java EE platform allows a component to name user-defined objects, such as enterprise beans, environment entries, JDBC DataSource objects, and message connections. An object should be named within a subcontext of the naming environment according to the type of the object. For example, enterprise beans are named within the subcontext java:comp/env/ejb, and JDBC DataSource references are named within the subcontext java:comp/env/jdbc.
ref: http://docs.oracle.com/cd/E19798-01/821-1841/girdr/index.html
As Pawel noted in his comment, the Wildfly docs are very helpful here:
The Java EE platform specification defines the following JNDI contexts:
In addition to the standard namespaces, WildFly also provides the following two global namespaces:
So "java:/" is just a global namespace (and context) in Wildfly and should be confused with a folder. It is simply a "named address" in a directory to access objects and services like JDBC, EJB, LDAP, etc.
For further information, the Java EE spec is useful:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With