Skip to main content

Getting Tomcat contexts to work in IntelliJ IDEA (and stop it starting Tomcat twice!)

If you run Tomcat within IDEA then you might have noticed a couple of things:
  1. Your application gets started, undeployed and then started again, increasing the start up time of Tomcat and reinitialising any load-on-startup servlets for a second time.

  2. Your carefully crafted <context> stanzas don't work as IDEA hasn't picked then up from either server.xml or META-INF/context.xml.
When you start Tomcat inside IDEA it copies the contents of your Tomcat conf directory to an IntelliJ system area (on Linux it's hidden in your home directory) and then runs Tomcat looking at those configuration files instead of the normal ones. In doing this it seems to ignore any context.xml files, any context stanzas of the server.xml and messes up any context files you might have in conf/Catalina/.

To get around this big mess and just use your Tomcat as if you ran it outside of IDEA you simply add a CATALINA_BASE environment variable to the configuration of Tomcat (within IDEA) pointing to your Tomcat directory:

Now when you start Tomcat within IDEA you should notice that your contexts are working correctly!

Technorati Tags: , , , ,


Juan Carlos said…
Hi Andrew;

When I add the environment variable, my webapp wasn't loaded :).

I was missed something?

abeacock said…
Juan Carlos,

I'm not sure what is happen on your system, could you please let me know what values the Tomcat log within IDEA prints out when you attempt to start Tomcat?

I'm specifically interested in:
Using JAVA_HOME: ?
Anonymous said…
The same problem. No "Using" in Tomcat log, just a strange parameter -Dcatalina.base=D:\Documents and Settings\username\.IntelliJIdea70\system\tomcat_Unnamed_projectname_some_crap_else
abeacock said…
What does it say in the Tomcat window within IDEA? This is where I was getting my "Using CATALINA_" statements from.

Which version of IDEA are you using (I'm still on v5.1)
Jamie said…
Thanks for putting this up, was stumped for a few mins.
Rolf said…
This still works well on IDEA 8.0.1.

I was going nuts trying to access the file system from inside a .war. Relative paths that worked in IDEA didn't work from a regular Tomcat webapp and vice versa.
Anonymous said…
Thanks a lot for the solution.... execelent, only need the environment variable to run perfectly
Anonymous said…
Awesomee! Thanks Buddy!
Anonymous said…
Thanks so much for this... I was really confused :-)
Emphyrio said…
Thanks, it works in IDEA 10.5.1 -- you'd think they might have fixed it by now.
Anonymous said… really saved lot of time for me :)
Anonymous said…
thanks.. it saved lot of time for me :)
Anonymous said…
thanks.. really saved lot of time :)
valy said…

You saved my day ... OR night :)
Anonymous said…
thank you very much.You save me sir...