Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging embedded Tomcat from v6 to v7 causes InitialContext lookup to fail
    primarykey
    data
    text
    <p>I'm using JUnit test cases to exercise my web service using embedded Tomcat. Under Tomcat 6 everything was working fine, but when I switched my project to Tomcat 7 I'm coming unstuck.</p> <p>The test code to setup the embedded Tomcat server is as follows:</p> <h3>Tomcat 6</h3> <pre><code>Embedded container = new Embedded(); container.setCatalinaHome("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11"); container.setRealm(new MemoryRealm()); container.setName("Catalina"); Engine engine = container.createEngine(); container.addEngine(engine); Host host = container.createHost("localhost", "/DecoderServiceTest"); Context rootContext = container.createContext("/DecoderServiceTest", System.getProperty("user.dir") + "/build/web"); host.addChild(rootContext); engine.setName("Catalina"); engine.addChild(host); engine.setDefaultHost("localhost"); container.addEngine(engine); Connector connector = container.createConnector(InetAddress.getLocalHost(), 4321, false); container.addConnector(connector); container.start(); </code></pre> <p>As the embedded API has changed between versions 6 and 7, I've changed my own code to the following:</p> <h3>Tomcat 7</h3> <pre><code>Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0.11"); tomcat.setPort(1234); tomcat.addWebApp("/DecoderServiceTest", System.getProperty("user.dir")+"/build/web"); tomcat.setHostname("localhost"); tomcat.start(); </code></pre> <p>The actual web service starts up fine when I execute the JUnit test (I can use my web browser and see the WSDL being served up).</p> <p>However, in the constructor of my web service I intialise some variables based on the values in the <code>web.xml</code> file (which is located in <code>System.getProperty("user.dir")+"/build/web/WEB-INF/web.xml"</code>), as follows:</p> <pre><code> Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); int thumbnailSize = (Integer) envCtx.lookup("thumbnail-pixel-size"); </code></pre> <p>Where my <code>web.xml</code> file contains the following entry:</p> <pre><code>&lt;env-entry&gt; &lt;env-entry-name&gt;thumbnail-pixel-size&lt;/env-entry-name&gt; &lt;env-entry-type&gt;java.lang.Integer&lt;/env-entry-type&gt; &lt;env-entry-value&gt;64&lt;/env-entry-value&gt; &lt;/env-entry&gt; </code></pre> <p>When I try and create the <code>envCtx</code> object I get a <a href="http://download.oracle.com/javase/1.5.0/docs/api/javax/naming/NamingException.html" rel="nofollow">NamingException</a> with the message that <code>Name java:comp is not bound in this Context</code>. I'm confused because it worked fine with Tomcat 6. Have I missed something in the setup of Tomcat 7 that I had previously defined in the setup of Tomcat 6?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload