get Initial Context - Java JNDI

Java examples for JNDI:Context

Description

get Initial Context

Demo Code


//package com.java2s;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getInitialContext());
    }//from www.  j a v a 2 s  . c o m

    private static Context initialContext;

    public static Context getInitialContext() throws NamingException {
        if (initialContext == null) {
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream("jndi.properties"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            initialContext = new InitialContext(properties);
        }
        return initialContext;
    }
}

Related Tutorials