Setup TomEE and IntelliJ

🛠️ Requirements


☕ Install Java SE

Go to the website provided in the requirements section and choose the latest Java version available.

image

Based on your OS download the appropriate package:

  • Windows: x64 Installer - it will automatically setup Java with PATH variables.

  • MacOS: x64/Arm64 (based on your chipset architecture) DMG Installer - automatically setup as the Windows one.

  • Linux: Debian/RPM/Arm Package - self contained package, extract it somewhere and follow the instruction under the Troubleshooting section.

Open CMD/iTerm/Linux terminal, check if java is correctly installed with the following command:

1
java -version

You should get an output like this:


🪶 Apache TomEE 9.0

Install TomEE

Go to the link provided and download the TomEE Plus ZIP package version 9.0.0 M7 and extract it anywhere you prefer.

Take note of this location because it will be required later!

If you want to use Thymeleaf, make sure to download TomEE 8.0 and select JavaEE 8 later in this guide unless it will not work!


📦 Install MySQL Community Edition

Download the MySQL Community package based on your OS:

  • Windows: MSI Installer - strongly suggested because the wizard guides you through each step.

  • MacOS: DMG Archive - strongly suggested for the same reason.

  • Linux: Archive based on your distro.

Installation for Windows

After downloading the MySQL Installer (x86, 32-bit), launch it and follow the installation wizard.
You can choose to install the Developer default (which provides all the tools we need and others not useful for our purpose) or the Custom Installation.

pic1

If you select the Custom Installation, make sure to select at least the following required tools:

  • MySQL Server

  • MySQL Workbench

  • MySQL Shell

  • Connector/J

pic2

During the installation procedure, the wizard may ask you to manually install some software to solve some requirements: please check if the specific tool is required for our purpose.
If not (like in the picture below), you can go back and, through Custom Installation, deselect the unnecessary tools and proceed without installing anything.

pic3


⛵ Install IntelliJ Ultimate IDEA and create a JavaEE project

If you don’t have it create a Jetbrains student account then download IntelliJ IDEA Ultimate for your OS.

⚠️ IMPORTANT - Intellij Ultimate 2021+ versions are needed to support JavaEE!
Disable Power Save Mode if active and make sure “Tomcat and TomEE” plugin is enabled under Settings>Plugins>Installed!

Create a Java Enterprise Edition project

Open IntelliJ, navigate to File>New>Project, select the Java Enterprise template.

Select Web application as the project template, Java as the language, Maven for the build system.

Select the JDK previously downloaded, Intellij should detect it automatically.

kek

Select Jakarta EE 9 and Web Profile.

kek

Setup artifacts

Access the artifact setup section by going into File>Project Stucture>Artifacts>.

Delete any artifact already created by Intellij and click +>From Modules

Click elements on the Available Elements tab to include any extra dependencies.

⚠️ IMPORTANT - After you include a new dependency in the pom.xml file, you need to include it in the artifact too!

artifact

Setup TomEE Application Server

Select from the top right corner Add configuration and scroll down until you reach TomEE Server>Local.

Name it and under Application server select the TomEE archive directory.

kek

In the Deployment tab, click on + then Artifact and select the previously created one.

kek

⚠️ IMPORTANT - After adding the Artifact in the Deployment tab, check the Application context matches with the artifact name. (for example test:war_exploded -> /test_war_exploded).
You can change them if you want, but make sure they match to make everything work.

pic4

Test the Web Application

To test your application click on the Run button on the right corner, close to the configuration you have just created.

After a few seconds, a new tab should appear in your default browser and the content of the index.jsp file should be shown.

The URL used in the browser should be the one chosen previously.

Setup Web Descriptor for JDBC

Replace the whole content of <your-path-to-tomee></your-path-to-tomee>/conf/tomee.xml file with the following configuration, remember to replace:

  • endpoint -> localhost
  • point -> MySQL port (default 3306)
  • schema_name -> the schema created on MySQL
  • username and password -> own credentials of MySQL

⚠️ IMPORTANT - Each time you will see “Replace with your own configuration” while reading this guide, it means you need to use your own parameters previously set up.

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="MySQL Database" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://endpoint:port/schema_name <!--Replace with your own configuration-->
UserName username <!--Replace with your own configuration-->
Password password <!--Replace with your own configuration-->
</Resource>
</tomee>

Add MySQL Java Connector

If you installed MySQL on Windows using the installer or if you manually downloaded the connector, go under File>Project Structure>Global Libraries>+>Java and add the connector.

⚠️ IMPORTANT - The connector jar file can be found under the MySQL installation folder (MySQL installer creates two folders both in Program Files and Program Files(x86)).

Then navigate to the connector’s folder, copy it, and place it under <path_to_tomee>/lib (The whole configuration won’t work if this step is not executed!).

At this point, you should be able to start the server by clicking on Run in the top right corner.

The servlet automatically created by Intellij should be under https://localhost:8080/artifact_name/api/hello-world.


Test database connection

Replace the HelloServlet with the following two configurations based on your JavaEE/JakartaEE version:

  • JavaEE 8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package <your.package.definition>; //Replace with your own configuration

import java.io.*;
import java.sql.DriverManager;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
private static final long serialVersionUID = 1L;

public void init() {
message = "Hello World!";
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
final String DB_URL = "jdbc:mysql://localhost:3306/<your_schema_name>"; //Replace with your own configuration
final String USER = "<your_db_username>"; //Replace with your own configuration
final String PASS = "<your_db_password>"; //Replace with your own configuration
String result = "Connection worked";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
DriverManager.getConnection(DB_URL, USER, PASS);
} catch (Exception e) {
result = "Connection failed"; e.printStackTrace();
}
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println(result);
out.close();
}

public void destroy() {
}
}
  • JakartaEE 9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package <your.package.definition>; //Replace with your own configuration

import java.io.*;
import java.sql.DriverManager;

import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;

public void init() {
message = "Hello World!";
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
final String DB_URL = "jdbc:mysql://localhost:3306/<your_schema_name>"; //Replace with your own configuration
final String USER = "<your_db_username>"; //Replace with your own configuration
final String PASS = "<your_db_password>"; //Replace with your own configuration
String result = "Connection worked";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
DriverManager.getConnection(DB_URL, USER, PASS);
} catch (Exception e) {
result = "Connection failed"; e.printStackTrace();
}
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println(result);
out.close();
}

public void destroy() {
}
}

💡 Useful tips

Convert Eclipse Project to IntelliJ Project

Open a project with Eclipse right-click on Java Project and click Configure and you should see the Convert to Maven Project option.

This will generate an incomplete pom.xml that you need to fill with all the required dependencies.

image

Setup schema or Import Dump MySQL

Open MySQL Workbench, click on the Schemas tab, right-click Create Schema ..., name it, and apply.

image

If you want to import a dump, go to Server>Database Import, select Import from Self-Contained File, select the dump and click on Start import.

image

image

Your database url will be something like: jdbc:mysql://localhost:3306/schema_name.


🧯 Troubleshooting

java command not found

  • Windows:

    Go to Start>Edit the system environment variables then click on Environment Variables, in the section User section variables for [your username] click on Path and Edit.

    Click on Browse and navigate to the Java installation folder (something like path\to\java\jdk.version\bin) then exit and try checking the version into a new CMD window.

  • Linux/MacOS:

    Open your terminal and type nano ~/.bashrc on Linux or nano ~/.bash_profile on MacOS, add the following line export PATH=$PATH:/path/to/java/bin, save and restart terminal to check the version.