21

As the title suggests, what does "javax.naming.NoInitialContextException" mean in non technical terms? And what are some general suggestions to fix it?

EDIT (From the console):

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
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at cepars.app.ConnectionHelper.getConnection(ConnectionHelper.java:25)
    at cepars.app.ConnectionHelper.getConnection(ConnectionHelper.java:10)
    at cepars.review.ReviewDAO.getJobList(ReviewDAO.java:30)
    at cepars.review.Test.main(Test.java:43)
java.lang.NullPointerException
    at cepars.review.ReviewDAO.getJobList(ReviewDAO.java:31)
    at cepars.review.Test.main(Test.java:43)
cepars.app.DAOException
    at cepars.review.ReviewDAO.getJobList(ReviewDAO.java:39)
    at cepars.review.Test.main(Test.java:43)
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
jdbcnewbie.
  • 839
  • 3
  • 8
  • 9
  • 1
    It just means that there is an exceptional circumstance wherein no initial context can be created and the code thus cannot continue with whatever it is told to do. The message and the stacktrace contain much more important information. Share them as well in order to be able to fix the root cause of the problem. – BalusC Jun 17 '11 at 14:25
  • The duplicate does not have an accepted answer. Please do not state that questions have an answer when they have no accepted answer. – David Williams Apr 09 '14 at 21:51
  • I have had something like this when in java ee was using CDI i.e. @Injection and try too run Junit integration tests without RunWith(Arquillian.class)! – Michał Ziobro Jun 08 '15 at 09:50
  • https://docs.oracle.com/javase/tutorial/jndi/ops/faq.html#1 – ROMANIA_engineer Nov 20 '15 at 13:25

4 Answers4

34

It means that there is no initial context :)

But seriously folks, JNDI (javax.naming) is all about looking up objects or resources from some directory or provider. To look something up, you need somewhere to look (this is the InitialContext). NoInitialContextException means "I want to find the telephone number for John Smith, but I have no phonebook to look in".

An InitialContext can be created in any number of ways. It can be done manually, for instance creating a connection to an LDAP server. It can also be set up by an application server inside which you run your application. In this case, the container (application server) already provides you with a "phonebook", through which you can look up anything the application server makes available. This is often configurable and a common way of moving this type of configuration from the application implementation to the container, where it can be shared across all applications in the server.

UPDATE: from the code snippet you post it looks like you are trying to run code stand-alone that is meant to be run in an application server. In this case, the code attempting to get a connection to a database from the "phonebook". This is one of the resources that is often configured in the application server container. So, rather than having to manage configuration and connections to the database in your code, you can configure it in your application server and simple ask for a connection (using JNDI) in your code.

pap
  • 27,064
  • 6
  • 41
  • 46
  • 4
    I liked the phonebook analogy. Personally still confused about details but your answer gave me the conceptual overview. – user2316667 May 30 '14 at 14:10
14

It basically means that the application wants to perform some "naming operations" (e.g. JNDI or LDAP lookups), and it didn't have sufficient information available to be able to create a connection to the directory server. As the docs for the exception state,

This exception is thrown when no initial context implementation can be created. The policy of how an initial context implementation is selected is described in the documentation of the InitialContext class.

And if you dutifully have a look at the javadocs for InitialContext, they describe quite well how the initial context is constructed, and what your options are for supplying the address/credentials/etc.

If you have a go at creating the context and get stuck somewhere else, please post back explaining what you've done so far and where you're running aground.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
6

Just read the docs:

This exception is thrown when no initial context implementation can be created. The policy of how an initial context implementation is selected is described in the documentation of the InitialContext class.

This exception can be thrown during any interaction with the InitialContext, not only when the InitialContext is constructed. For example, the implementation of the initial context might lazily retrieve the context only when actual methods are invoked on it. The application should not have any dependency on when the existence of an initial context is determined.

But this is explained much better in the docs for InitialContext

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • @Oracle: `InitialContext` is a very dumb, meaningless name. It's a combination of nothing-saying words _initial_ and _context_, which can be used for anything and everything, everywhere. Also the paragraph above uses the word _context_ 8 (eight) times to explain the word _InitialContext_. A better name would be `JavaNamingAndDirectoryInitialContext` or at least `NamingAndDirectoryInitialContext`. Or better rethink the whole naming and give names that mean something. Of course, JNDI is a little abstract, but _InitialContext_ is really a bad choice I think. – aliopi Apr 05 '17 at 07:52
  • 2
    Agreed. But you should probably address that criticism at Oracle. You're shooting the messenger here – Sean Patrick Floyd Apr 05 '17 at 07:54
-2

In extremely non-technical terms, it may mean that you forgot to put "ejb:" or "jdbc:" or something at the very beginning of the URI you are trying to connect.