{"id":276,"date":"2010-06-15T12:27:24","date_gmt":"2010-06-15T18:27:24","guid":{"rendered":"http:\/\/dennissherman.com\/dennis\/?p=276"},"modified":"2010-06-15T12:27:24","modified_gmt":"2010-06-15T18:27:24","slug":"standalone-jndi","status":"publish","type":"post","link":"http:\/\/dennissherman.com\/dennis\/2010\/06\/standalone-jndi\/","title":{"rendered":"Standalone JNDI"},"content":{"rendered":"<p>I have a need at work to implement a standalone command line Java program that needs to be able to use local JNDI.\u00a0 I&#8217;ve got it working, but it was way harder to figure out than it should have been, and I couldn&#8217;t find any blog postings that used the version of code that I used.\u00a0 So, I&#8217;m documenting here.<\/p>\n<p>If you don&#8217;t already know what any of this means, move along, nothing to see here.<\/p>\n<p>I&#8217;m using the JBoss JNDI provider, which is in the default JBoss distribution.\u00a0 I&#8217;m using JBoss 4.2.2.GA<\/p>\n<p>First, add the jars you need to your classpath:<\/p>\n<ul>\n<li>jnpserver.jar<\/li>\n<li>jbossall-client.jar<\/li>\n<\/ul>\n<p>Next, write a jndi.properties file with these entries:<\/p>\n<pre>java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory\r\njava.naming.factory.url.pkgs=org.jboss.naming\r\n<\/pre>\n<p>Make sure the jndi.properties file is on your classpath.<\/p>\n<p>Then, include this in your code.\u00a0 I suggest at the top of your main(), where it gets run at start:<\/p>\n<pre>try {\r\n NamingBeanImpl jnpServer = new NamingBeanImpl();\r\n jnpServer.start();\r\n\r\n } catch (Exception e) {\r\n \/\/ TODO Auto-generated catch block\r\n e.printStackTrace();\r\n }\r\n\r\n<\/pre>\n<p>That&#8217;s it, you now have JNDI available in your program.\u00a0 There is probably more that can be done with the jndi.properties file to load objects at start, but I&#8217;ll learn that as I need it.<\/p>\n<p>To add things to the context programmatically:<\/p>\n<pre> Object object = \"TestApp\";\r\n String name = \"config.appName\";\r\n Context context = new InitialContext();\r\n context.bind(name, object);<\/pre>\n<p>To look up an object in the context:<\/p>\n<pre> Context context = new InitialContext();\r\n Object object = context.lookup(name);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a need at work to implement a standalone command line Java program that needs to be able to use local JNDI.\u00a0 I&#8217;ve got it working, but it was way harder to figure out than it should have been, and I couldn&#8217;t find any blog postings that used the version of code that I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/posts\/276"}],"collection":[{"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":3,"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":279,"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/posts\/276\/revisions\/279"}],"wp:attachment":[{"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dennissherman.com\/dennis\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}