title: How do I lookup a JNDI Datasource from outside a web container?
body: <p>I have the following environment set up:</p>
  
  <ul>
  <li>Java 1.5</li>
  <li>Sun Application Server 8.2</li>
  <li>Oracle 10 XE</li>
  <li>Struts 2</li>
  <li>Hibernate</li>
  </ul>
  
  <p>I'm interested to know how I can write code for a Java client (i.e. outside of a web application) that can reference the JNDI datasource provided by the application server.</p>
  
  <p>The ports for the Sun Application Server are all at their defaults.  There is a JNDI datasource named jdbc/xxxx in the server configuration, but I noticed that the Hibernate configuration for the web application uses the name java:comp/env/jdbc/xxxx instead.</p>
  
  <p>Most of the examples I've seen so far involve code like</p>
  
  <pre><code>Context ctx = new InitialContext();
  ctx.lookup("jdbc/xxxx");
  </code></pre>
  
  <p>But it seems I'm either using the wrong JNDI name, or I need to configure a jndi.properties or other configuration file to correctly point to a listener?  I have appserv-rt.jar from the Sun Application Server which has a jndi.properties inside of it, but it does not seem to help.</p>
  
  <p>There's a similar question here, but it doesn't give any code / refers to having iBatis obtain the JNDI Datasource automatically: <a href="https://stackoverflow.com/questions/39053/accessing-datasource-from-outside-a-web-container-through-jndi">Accessing Datasource from Outside A Web Container (through JNDI)</a></p>