Already on ArticleSlash?

Forgot your password? Sign Up

Working With JNDI In A J2SE Application

 


Visitors: 165

Java Naming and Directory Interface (JNDI) allows Java objects to be published to a directory service which can be accessed by other Java applications later. For this to work you need a JNDI service provider. All the J2EE servers come with a built-in implementation of the JNDI service. This makes most people assume that you can work with JNDI only in J2EE environment. It is possible for you to set up your own lightweight JNDI server, host your J2SE objects on this and make these objects available for other's use. There are varieties of JNDI service providers available. In this article, I am going to refer to the one provided by JBOSS.

The following . jar files must be added to your classpath in order to setup the server, host an object, and to access the same.

1) log4j. jar
2) jnpserver. jar
3) jboss-common. jar
4) concurrent. jar

In addition, you must also have the log4j. properties file in your classpath. These files are available with the Jboss binary distribution.

To setup the environment you can also type the following in a text file (setJNPEnv. cmd)

set JNP_HOME=C:jnp
set JNP_LIB=%JNP_HOME%lib

set JNP_PATH=%JNP_LIB%;%JNP_LIB%log4j. jar
set JNP_PATH=%JNP_PATH%;%JNP_LIB%jnpserver. jar
set JNP_PATH=%JNP_PATH%;%JNP_LIB%jboss-common. jar
set JNP_PATH=%JNP_PATH%;%JNP_LIB%concurrent. jar

set CLASSPATH=. ;%JNP_PATH%;%CLASSPATH%

Make sure to change the JNP_HOME environment variable's value to the folder, where you have extracted the jnp. zip.

To start the JNDI service, open a command prompt and type the following command:

c:work>setJNPEnv. cmd
c:work>start java org. jnp. server. Main

This opens a new window which represents the JNP (Jboss Naming Provider) server listening at port 1099.

Now you can store any object you want in the JNDI using the code below:

Context ctx=new InitialContext();
ctx. rebind("name1", “Vinod");
ctx. rebind("person1", new Person("Vinod", “Bangalore"));

Also, you can lookup and obtain an object using the code below:

Context ctx=new InitialContext();
String name=(String) ctx. lookup("name1");
Person p1=(Person) ctx. lookup("person1");

One last thing. You must provide the information about JNDI provider when you call new InitialContext(). This is usually done by placing a jndi. properties file in you working directory or supplying a java. util. Map object. Here is the content of jndi. properties file:

java. naming. provider. url=localhost:1099
java. naming. factory.initial=org. jnp.interfaces. NamingContextFactory
java. naming. factory. url. pkgs=org. jboss. naming

Vinod Kumar Kayartaya is a professional trainer in Bangalore, the IT capitol of India since 1995. He has delivered countless number of training sessions on various techonologies in most of the major IT companies in Bangalore, Mysore and Hyderabad. You can also visit http://thejavatrainer.com for more Java/J2ee resources.

This and related articles can also be found at http://kvinod.com

(543)

Article Source:


 
Rate this Article: 
 
Gain working experience in Canada by working as Canadian caregiver
Rated 4 / 5
based on 5 votes
ArticleSlash

Related Articles:

Enjoy The Benefits Of Working With A Dedicated Offshore Java Software ..

by: Daya Mukherjee (January 20, 2012) 
(Computers and Technology/Programming)

Controls for Attaining Continuous Application Security in the Web Application ..

by: Caleb Sima (August 08, 2007) 
(Internet and Businesses Online/Security)

C2010-568: IBM Tivoli Composite Application Manager for Application Diagnostics .

by: Velma W. Johnson (June 10, 2014) 
(Computers and Technology/Certification Tests)

iPhone Application Development – The Native place of Mobile Application ..

by: Adam Willss (August 17, 2012) 
(Communications/Mobile Cell Phone)

Web Application Development Company - Easily Can Provide Web Based Application

by: Shiny Jones (August 26, 2011) 
(Internet and Businesses Online/Web Development)

PHP Application Development How we can develop Web application cost-effective

by: Kethy Wright (February 18, 2010) 
(Computers and Technology/Software)

The Perils For Application Cracking - Application Protection

by: Jose Sogiros (February 02, 2010) 
(Computers and Technology/Software)

Mobile Application Testing- Best way to grow up business for mobile application .

by: Monrow Dido (June 27, 2009) 
(Communications/Mobile Cell Phone)

Iphone Application Development-Giant Application Development for Your Smart ..

by: Albert Hall (December 04, 2012) 
(Computers and Technology/iPhone Applications)

Gain working experience in Canada by working as Canadian caregiver

by: Pumnula Aaron (April 02, 2009) 
(Home and Family/Parenting)