Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialcontext in a standalone Java program

Tags:

I'm using a JNDI for creating connection pool. It works great in a web application. I believe the InitialContext is provided by the tomcat server.

Context initContext  = new InitialContext(); Context envContext  = (Context)initContext.lookup("java:/comp/env"); dataSource = (DataSource)envContext.lookup("jdbc/testdb"); 

But when I try to call the same utility from a standalone Java program, the initContext object is null. How can I explicitly provide all the necessary properties that Context object is expecting.

Error : javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

like image 501
Satish Jonnala Avatar asked Dec 03 '13 19:12

Satish Jonnala


People also ask

What is InitialContext in Java with example?

The initial context implements the Context interface and provides the starting point for resolution of names. When the initial context is constructed, its environment is initialized with properties defined in the environment parameter passed to the constructor, and in any application resource files.

Why JNDI is used in Java?

JNDI, part of the J2EE specification, provides naming and directory functionality for Java applications. Because JNDI is defined independently of any specific naming or directory service implementation, it enables Java applications to access different naming and directory services using a single API.

What does InitialContext lookup do?

InitialContext ic = new InitialContext(); Object obj = ic. lookup(); If name is empty, returns a new instance of this context (which represents the same naming context as this context, but its environment may be modified independently and it may be accessed concurrently).

What is JNDI lookup?

The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and resources (in the form of Java objects) via a name. Like all Java APIs that interface with host systems, JNDI is independent of the underlying implementation.


2 Answers

Here is an example adapted from the accepted answer but doing everything inline to avoid creating extra classes.

public static void main(String[] args) {     setupInitialContext();     //do something that looks up a datasource }  private static void setupInitialContext() {     try {         NamingManager.setInitialContextFactoryBuilder(new InitialContextFactoryBuilder() {              @Override             public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException {                 return new InitialContextFactory() {                      @Override                     public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {                         return new InitialContext(){                              private Hashtable<String, DataSource> dataSources = new Hashtable<>();                              @Override                             public Object lookup(String name) throws NamingException {                                  if (dataSources.isEmpty()) { //init datasources                                     MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();                                     ds.setURL("jdbc:mysql://localhost:3306/mydb");                                     ds.setUser("mydbuser");                                     ds.setPassword("mydbpass");                                     dataSources.put("jdbc/mydbname", ds);                                      //add more datasources to the list as necessary                                 }                                  if (dataSources.containsKey(name)) {                                     return dataSources.get(name);                                 }                                  throw new NamingException("Unable to find datasource: "+name);                             }                         };                     }                  };             }          });     }     catch (NamingException ne) {         ne.printStackTrace();     } } 
like image 82
Scott Nelson Avatar answered Oct 02 '22 07:10

Scott Nelson


You could also create your own custom context.

LocalContext ctx = LocalContextFactory.createLocalContext(); ctx.addDataSource("jdbc/testdb", driverName, url, usr, pwd); 

See Running Beans Locally that use Application Server Data Sources for more details.


newUPDATE

You can use the class org.springframework.mock.jndi.SimpleNamingContextBuilder of Spring. e.g.:

  • Setup:

    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); builder.bind("jdbc/Oracle", ods); builder.activate(); 
  • Use:

    DataSource ds = InitialContext.doLookup("jdbc/Oracle"); 
like image 27
Paul Vargas Avatar answered Oct 02 '22 07:10

Paul Vargas