JNDI lookup in JBoss

This post illustrates a simple JNDI lookup in Jboss Server that can be used to lookup Data Sources etc.

1. Open a command prompt and set the class path to the jbossall-client.jar file.

set classpath=%classpath%;G:jboss-5.1.0.GAclientjbossall-client.jar

2. Compile and execute the JNDITest.java program.

import java.security.Security;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

public class JNDITest

{

public static void main(String args[]) throws Exception

{

Properties env = new Properties();

env.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.HttpNamingContextFactory");

env.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:8080/invoker/JNDIFactory");

Context ctx = new InitialContext(env);

System.out.println("Created InitialContext, env=" + env);

Object data = ctx.lookup("jmx/invoker/RMIAdaptor");

System.out.println("lookup(jmx/invoker/RMIAdaptor): " + data);

}

}

 

Common errors occurring while JNDI lookup.

1. Exception in thread “main” javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.HttpNamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.HttpNamingContextFactory]

Solution:- Set the classpath to jbossall-client.jar file under $JBOSS_HOME/client.

Best Regards.