The Java Naming and Directory Interface (JNDI) is a Java API for looking up named resources and directory entries through one uniform interface. Applications use it to find a DataSource, a JMS queue or an environment value that the application server configured, and to query directory services such as LDAP, without hard-coding how those resources are created.
JNDI is part of Java SE (the java.naming module, package javax.naming) and has been since Java 1.3. It is also a well-known security risk when names come from untrusted input, which is covered below.
A naming service maps names to objects, like a file system maps paths to files. A directory service is a naming service whose entries also carry attributes you can search, such as a user entry in LDAP with mail and uid attributes.
| Interface | Purpose |
|---|---|
Context | Bind, look up, rename and list names |
InitialContext | Starting point; reads its configuration and returns the right provider |
DirContext | Adds attribute storage and search |
InitialDirContext | Starting point for directory operations |
JNDI is built in two layers. Your code uses the API, and a service provider (SPI) talks to the actual system. The JDK ships providers for LDAP, DNS and RMI registry. The CORBA provider was removed in Java 11.
new InitialContext() finds its provider from environment properties, either passed in a Hashtable or Properties, set as system properties, or supplied in a jndi.properties file. Inside an application server the container configures this for you, so an empty constructor works.
The classic use is a container-managed connection pool. Tomcat, for example, declares it in context.xml:
The application then finds it under the java:comp/env namespace:
In Jakarta EE or Spring-managed code you rarely call lookup yourself. Use @Resource(name = "jdbc/appDb") injection in Jakarta EE, or point Spring's JndiObjectFactoryBean or spring.datasource.jndi-name at the name. The code stays the same across environments, and only the container configuration changes.
Context can also write to a writable provider, though many providers are read-only.
bind throws NameAlreadyBoundException if the name exists, so use rebind to overwrite. Always close contexts you create with ctx.close() when you are done, to release the provider's resources.
For LDAP, use InitialDirContext and search with a filter. This example runs against a server over TLS.
The {0} placeholder with an argument array prevents LDAP filter injection. Never concatenate user input into the filter string.
If an attacker controls the string passed to lookup, they can point it at their own LDAP or RMI server, which can return a reference that makes the JVM load and run remote code. This is the mechanism behind the Log4Shell vulnerability in Log4j. To stay safe:
Context.lookup.java:comp/env names that your own configuration defines.JDBC talks to a database. JNDI is how an application finds a configured DataSource that wraps a JDBC pool, so connection settings live outside the code.
java:comp/env mean?It is the application's private environment namespace in a Jakarta EE or servlet container, where resource references and environment entries are visible to that component only.
Usually not. Spring Boot reads data source settings from properties. JNDI is mostly needed when deploying a WAR to a server that provides managed resources.
No. It remains a Java SE API. The IIOP and CORBA parts were removed, while LDAP, DNS and RMI providers stay.
JNDI gives Java code a single API for naming and directory services. Use InitialContext and java:comp/env names to obtain container-managed resources, InitialDirContext for LDAP searches, and parameterized filters for safety. Treat any externally supplied name as dangerous, since JNDI injection can lead to remote code execution.