Powered by Blogger.

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
Share on Google Plus

About Unknown

Author is a Tech savvy and Web Enthusiast by nature and really love to help users by providing how-to posts and tech tutorials. He is a founder and author of technolamp.co.in and programming9.com. YouTube Channel to SUBSCRIBE:
    https://www.youtube.com/user/nvrajasekhar
Available: info@programming9.com
    Blogger Comment
    Facebook Comment

0 comments: