[java] Tomcat in Intellij Idea Community Edition

Is it possible to run a web application using Tomcat Server in Intellij Idea Community Edition?

I tried to find some information about it but haven't achived any success.

This question is related to java apache tomcat intellij-idea

The answer is


Yes, you can use maven plugin, or simple java program. No need for IDE plugin. See for example Main class from https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat


If you use Gradle, you can try my script: https://github.com/Adrninistrator/IDEA-IC-Tomcat .This script will build files for web application, create a Tomcat instance, start Tomcat and load the web application.


Intellij Community does not offer Java application server integration. Your alternatives are

  1. buying Intellij licence,
  2. switching to Eclipse ;)
  3. installing Smart Tomcat plugin https://plugins.jetbrains.com/plugin/9492
  4. installing IDEA Jetty Runner plugin https://plugins.jetbrains.com/plugin/7505
  5. running the application server from Maven, Gradle, whatever, as outlined in the other answers.

I personally installed the Jetty Runner plugin (Jetty is fine for me, I do not need Tomcat) and I am satisfied with this solution. I had to deal with IntelliJ idea - Jetty, report an exception, though.


Using Maven, try tomcat7-maven-plugin:

  <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.tomcat.maven</groupId>
                  <artifactId>tomcat7-maven-plugin</artifactId>
                  <version>2.2</version>
                  <configuration>
                      <path>/</path>
                      <contextFile>src/main/webapp/WEB-INF/config/app-config.xml</contextFile>
                      <mode>context</mode>
                      <charset>UTF-8</charset>
                      <warDirectory>target/${project.artifactId}-${project.version}</warDirectory>
                  </configuration>
              </plugin>
          </plugins>
  </build>

Run it using tomcat7:run-war

More goals here



I think maven is not installed properly. check with mvn --v

or

Please check maven home path in env variables or you have created this project before the installation of maven


You can use tomcat plugin with gradle. For more information, you here.

apply plugin: 'tomcat'

dependencies {
    classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.4'
}

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

[tomcatRun, tomcatRunWar, tomcatStop]*.stopPort = 8090
[tomcatRun, tomcatRunWar, tomcatStop]*.stopKey = 'stfu'
tomcatRunWar.contextPath = "/$rootProject.name"
tomcatRunWar.ajpPort = 8000

if (checkBeforeRun.toBoolean()) {
    tomcatRunWar { dependsOn += ['check'] }
}

For Intellij 14.0.0 the Application server option is available under View > Tools window > Application Server (But if it is enable, i mean if you have any plugin installed)


Tomcat can also be integrated with IntelliJ Idea - Community Edition with Tomcat Runner Plugin.

Details below: https://plugins.jetbrains.com/plugin/8266-tomcat-runner-plugin-for-intellij


Well the question is already answered, however what I am writing here is just my observation so other fellows in the community can save some of their time. I tried running a spring-mvc project using the embedded tom-cat in Intellij communit edition.

First try I did was using the Gradle tom-cat plugin, however the problem that I faced there is the tomcat server just starts once, after that from the second start the build complains saying that a container is already running. There are so many open thread on the web about this, for some it works and for most of the people (almost 90% of the web threads that I broke my head with, faced the same problem of container not getting started the second time. The resolution is not there.

After wasting a lot lot of my time, I finally decided to switch to maven tom-cat plugin and do the same spring-mvc setup with maven that I did with gradle and VOILA! it worked in the first short.

So long story short, if you are setting up spring-mvc project in intellij community edition, please consider maven tomcat plugin.

Hope this helps somebody's hours of exploration across various web forums.


I am using intellij CE to create the WAR, and deploying the war externally using tomcat deployment manager. This works for testing the application however I still couldnt find the way to debug it.

  1. open cmd and current dir to tomcat/bin.
  2. you can start and stop the server using the batch files start.bat and shutdown.bat.
  3. Now build your app using mvn goal in intellij.
  4. Open localhost:8080/ **Your port number may differ.
  5. Use this tomcat application to deploy the application, If you get the authentication error, you would need to set the credentials under conf/tomcat-users.xml.

Tomcat (Headless) can be integrated with IntelliJ Idea - Community edition.

Step-by-step instructions are as below:

  1. Add tomcatX-maven-plugin to pom.xml

    <build>
        <plugins>
            <plugin>
                 <groupId>org.apache.tomcat.maven</groupId>
                 <artifactId>tomcat7-maven-plugin</artifactId>
                 <version>2.2</version>
                 <configuration>
                     <path>SampleProject</path>
                 </configuration>
            </plugin>
        </plugins>
    </build>
    
  2. Add new run configuration as below:

    Run >> Edit Configurations >> + >> Maven
    
    Parameters tab ...
    Name :: Tomcat
    Working Directory :: Project Root Directory
    Command Line :: tomcat7:run
    
    Runner tab ...
    VM Options :: <user needed options>
    JRE :: <project needed>
    
  3. Invoke Tomcat in Run/Debug mode directly from IntelliJ Run >> Run/Debug menu

NOTE: Though this is considered a hacking of using using Tomcat integration features of IntelliJ - Enterprise version features, but I would consider this a programmatic way integrating tomcat to the IntelliJ Idea - community edition.


The maven plugin and embedded Tomcat are usable work-arounds (I like second better because you can debug) but actual web server integration is a feature only available in intelij paid editions.


VM :-Djava.endorsed.dirs="C:/Program Files/Apache Software Foundation/Tomcat 8.0/common/endorsed" 
    -Dcatalina.base="C:/Program Files/Apache Software Foundation/Tomcat 8.0"  
    -Dcatalina.home="C:/Program Files/Apache Software Foundation/Tomcat 8.0" 
    -Djava.io.tmpdir="C:/Program Files/Apache Software Foundation/Tomcat 8.0/temp" 
    -Xmx1024M

Yes, its possible and its fairly easy.

  1. Near the run button, from the dropdown, choose "edit configurations..."
  2. On the left, click the plus, then maven and rename it "Tomcat" on the right side.
  3. for command line, enter "spring-boot:run"
  4. Under the runner tab, for 'VM Options', enter "-XX:MaxPermSize=256m -Xms128m -Xmx512m -Djava.awt.headless=true" NOTE: 'use project settings' should be unticked.
  5. For environment variables enter "env=dev"
  6. Finally, click Ok.

When you're ready to press run, if you go to "localhost:8080/< page_name > " you'll see your page.

My pom.xml file is the same as the Official spring tutorial Serving Web Content with Spring MVC

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-serving-web-content</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I used Jay Lin's answer. Highly recommend it.

If you never used Maven before and don't want to go deep into it: follow Jay Lin's answer, but also do this:

  1. right click on your project name -> Add Framework support -> Maven.

  2. Then install maven from here http://maven.apache.org/install.html. Do what it says, run the commands.

  3. Then install spring-boot from here https://mvnrepository.com.

  4. Then follow the error messages if there are any - maybe you would need to install some other stuff (just google it and that mvnrepository.com would come up). To install use this command:

    mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path

replace path with where you downloaded the jar file, replace version, group and artifact id with info from mvnrepository.com.

  1. Further errors I encountered:

I had to create a class in src/main/java (with simple System.out.println command in main) and add <start-class>main.java.Hello</start-class> in <properties> tag in pom.xml. Btw, the pom.xml should appear itself when you do the first action from my answer - copy paste Jay Lin's code there.

Another error I got was connected to JAVA_HOME variable and the verion stuff. Somewhy it thought jdk is 7th version and I was telling it was 8th. So I changed the java version tag in <properties> to this <java.version>1.7</java.version>.

Now it works fine! Good luck everyone.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to apache

Enable PHP Apache2 Switch php versions on commandline ubuntu 16.04 Laravel: PDOException: could not find driver How to deploy a React App on Apache web server Apache POI error loading XSSFWorkbook class How to enable directory listing in apache web server Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details How to enable php7 module in apache? java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer

Examples related to tomcat

Jersey stopped working with InjectionManagerFactory not found The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat Spring boot: Unable to start embedded Tomcat servlet container Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start Kill tomcat service running on any port, Windows Tomcat 8 is not able to handle get request with '|' in query parameters? 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE 403 Access Denied on Tomcat 8 Manager App without prompting for user/password Difference between Xms and Xmx and XX:MaxPermSize

Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle