2012-12-31

JNDI: sample jndi.properties file

  • For web app, this file must end up in your.war/WEB-INF/jndi.properties.
  • For standalone app, it must end up in some classpath root. Which is e.g. some jar's root.

For JBoss client app:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming.client

For Jetty:

java.naming.factory.initial=org.mortbay.naming.InitialContextFactory
java.naming.factory.url.pkgs=org.mortbay.naming

Don't forget to add jetty-naming to your classpath.

<dependency>
   <groupId>org.mortbay.jetty</groupId>
   <artifactId>jetty-naming</artifactId>
   <version>${jetty.version}</version>
   <scope>compile</scope>
</dependency>

Otherwise you'll get

java.lang.ClassNotFoundException: org.mortbay.naming.InitialContextFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

For advanced setups, e.g. with LDAP, see JNDI tutorial.

NoInitialContex­tException

This is what Sun JDK throws when the above properties are not set:

00:34:52.752 ERROR [main] org.jboss.jawa­bot.web.RunIn­Jetty Error putting BeanManager to JNDI: java.lang.reflec­t.InvocationTar­getException java.lang.reflec­t.InvocationTar­getException
at sun.reflect.Na­tiveConstructo­rAccessorImpl­.newInstance0(Na­tive Method)
at sun.reflect.Na­tiveConstructo­rAccessorImpl­.newInstance(Na­tiveConstructo­rAccessorImpl­.java:39)
at sun.reflect.De­legatingConstruc­torAccessorIm­pl.newInstance(De­legatingConstruc­torAccessorIm­pl.java:27)
at java.lang.reflec­t.Constructor­.newInstance(Con­structor.java:513)
at org.mortbay.xml­.XmlConfigura­tion.newObj(XmlCon­figuration.ja­va:608)
at org.mortbay.xml­.XmlConfigura­tion.configure(XmlCon­figuration.ja­va:256)
at org.mortbay.xml­.XmlConfigura­tion.configure(XmlCon­figuration.ja­va:190)
at org.jboss.jawa­bot.web.RunIn­Jetty.run(RunIn­Jetty.java:146)
at org.jboss.jawa­bot.mod.web.Web­ModuleHook.init­Module(WebModu­leHook.java:18)
at org.jboss.jawa­bot.JawaBotAp­p.initAndStar­tModules(Jawa­BotApp.java:106)
at org.jboss.jawa­bot.JawaBotAp­p.main(JawaBo­tApp.java:53) Caused by: javax.naming.No­InitialContex­tException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.fac­tory.initial
at javax.naming.spi­.NamingManager­.getInitialCon­text(NamingMa­nager.java:645)
at javax.naming.I­nitialContext­.getDefaultInit­Ctx(InitialCon­text.java:288)
at javax.naming.I­nitialContext­.getURLOrDefau­ltInitCtx(Ini­tialContext.ja­va:325)
at javax.naming.I­nitialContext­.getNameParser(I­nitialContext­.java:480)
at org.mortbay.jet­ty.plus.namin­g.NamingEntry­.save(NamingEn­try.java:192)
at org.mortbay.jet­ty.plus.namin­g.NamingEntry­.<init>(Namin­gEntry.java:58)
at org.mortbay.jet­ty.plus.namin­g.Resource.<i­nit>(Resource­.java:34)
… 11 more


0