Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JNDI Name java:/

Tags:

java

jndi

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.

like image 525
Brian Avatar asked Jul 15 '15 19:07

Brian


People also ask

What is JNDI name in Java?

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.

How can I know my JNDI name?

To view this administrative console page, click Applications > Application Types > WebSphere enterprise applications > application > EJB JNDI names.

Where are JNDI name stored?

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.

What is JNDI name example?

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 .


1 Answers

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:

  • java:comp - The namespace is scoped to the current component (i.e. EJB)
  • java:module - Scoped to the current module
  • java:app - Scoped to the current application
  • java:global - Scoped to the application server

In addition to the standard namespaces, WildFly also provides the following two global namespaces:

  • java:jboss
  • java:/

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:

  • (Java 6) http://docs.oracle.com/cd/E19798-01/821-1841/girdr/index.html
  • (Java 7, PDF) http://download.oracle.com/otn-pub/jcp/java_ee-7-fr-spec/JavaEE_Platform_Spec.pdf
like image 65
sprockets Avatar answered Sep 22 '22 00:09

sprockets