September 23, 2010

A Program for Demonstrating JNDI (JAVAEE) using GLASSFISH

Aim: Write a program to demonstrate JNDI

program:

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
class MyLookupClass {
  public static void main(String[] args) {
    String myObject = " ";            // use your obj name
    Hashtable ep = new Hashtable(1);
    ep.put(Context.INITIAL_CONTEXT_FACTORY,
      "com.sun.jndi.fscontext.RefFSContextFactory");
    try {
      InitialContext ct = new InitialContext(ep);
Object obj = ct.lookup(myObject);
System.out.println("Object: " + obj);
ct.close();
    }
    catch (NamingException error) {
      System.err.println("Error:" + error.getMessage());
    }
  }
}


Execution steps:

step 1:
set the classpaths before the compilation and run.Here GLASSFISH is the Application server

a)set the foldername where we are executing the jndi
b)set the fscontext.jar
c)set the javaee.jar
d)set the jndi-properties.jar

step 2:

compile the code using "javac"
javac MyLookupClass.java

step 3:

Run the code using "java" at cmd prompt

java MyLookupClass

output:(Appears on the same cmd prompt)

Object: com.sun.jndi.fscontext.RefFSContext@1050e1f

No comments:

Post a Comment