Install JavaEE on IntelliJ IDEA Ultimate

Index

Requirements

Install Java JDK and JRE

Go to the website provided at the top of the document and choose which version of Java you prefer (in this guide we will use Java 15).

image

Download based on your OS, in this case Windows.

image

If you have downloaded the installer and followed its steps, Java should already be in your PATH.

Open CMD/iTerm/Linux terminal and check if everything is correct, if you type java -version you should see a similar output.

image

If java isn’t a recognized command follow the steps below otherwise go to the next step.

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/Mac

Open your terminal 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.

Install Apache Tomcat 9.0 or Apache TomEE 9.0

Decide which application server you need based on what project you are creating:

image

Install Tomcat

Go to the link provided and download based on you OS; for Windows use the installer, for MacOS/Linux use the tar.gz.

image

The installer automatically installs and runs Apache Service at startup; the tar.gz needs to be extracted and placed whenever you want and needs to be started manually by accessing the installation folder through terminal with cd path/to/installation/folder and running ./startup.

To check if Tomcat is installed correctly, go to Start>Monitor Tomcat>Service Status>Start on Windows or run folder/to/tomcat/./startup on Linux/MacOS and then navigate to http://localhost:8080 and you should see a page like this.

image

Next navigate to path/to/your/tomcat/installation/Tomcat 9.0/conf and edit server.xml and replace this two fields with this: <Server port="8005" shutdown="SHUTDOWN">.

Install TomEE

Download TomEE archive here.

Extract and place the folder in your preferred workspace and test the server by running path/to/tomee/bin/TomEE.exe on Windows and path/to/tomee/bin/startup.sh on MacOS/Linux.

Install MySQL Community Edition

For MacOS users click here.

Reach the website and download the installer; on the first tab select Developer Default, on Check requirements tab click on Execute, proceed until Accounts and Roles and set the database password.

Proceed until MySQL Installer shows the server installation on Connect to the server, insert root and previous password and select Check.

Install IntelliJ IDEA and create a project

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

Create a Java EE project

Open IntelliJ and select New Project, then select Java Enterprise tab and from Project JDK drop down menu, on detected JDKs, select the one you previously downloaded.

image

Select the latest Java EE edition and choose from Specifications every framework you need(for JPA/EJB support choose WebProfile), then click Next.

image

On Name input field type your project name and on group type the name you want (I recommend something like it.polimi.tiw).

image

Go to File>Project Structure>Project Settings>Project and check if Project language level version matches Project JDK version, select it otherwise.

image

Add Java MySQL Connector

On the same window go to Platform Settings>Global Libraries and select +, navigate to the installation folder of the connector chosen during the MySQL Community Edition installation (on Windows is C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.22.jar)

image

Setup Tomcat/TomEE Server

Close this window, go to the top right and click on Add configuration; on the new window opened press + and select Tomcat Server>Local or TomEE Server>Local for TomEE.

Choose the server name you like and then select Application Server>Configure and choose your Tomcat/TomEE installation directory.

image

Save and then in the bottom right select 💡Fix, on the Artifacts tab select +, choose Web Application Exploded, name it and double click all Available Elements.

image

Apply and exit.

Test the newly created configuration by killing every Tomcat Server already running and click in the top right corner Run.

image

Setup pom.xml and multi-modules

IntelliJ manages projects’ dependencies in pom.xml, you need to add in the <dependencies></dependencies> all the dependencies required by your project.

If you wish to had more dependency navigate to https://mvnrepository.com/, search the one you need and add it to the pom.xml file.
A simple and yet complete with all JavaEE dependencies with EJB and JPA support example can be seen below:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MissionExpensesWeb</groupId>
<artifactId>MissionExpensesWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api -->
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.ejb/jakarta.ejb-api -->
<dependency>
<groupId>jakarta.ejb</groupId>
<artifactId>jakarta.ejb-api</artifactId>
<version>4.0.0-RC2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>15</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

If you are creating or importing a multi-modules project (like Eclipse workspace), you need to specify module dependencies if you are using classes from another module.

This can be achieved by navigating to File>Project Structure>Modules and by adding your desired module with the + button, after that select the module that depends from the one just imported, add the module dependency and enable it.

image

image

image

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/db-name.

Setup Web Descriptor

Go to File>Project Structure>Facets, click on +>Web, choose a name, select + and choose web.xml.

image

This will create the descriptor under [project-name]/WEB-INF/WEB.XML.

For Tomcat in the web.xml setup database username/password and the database url found in the previous step like this example:

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
<?xml version="1.0" encoding="UTF-8"?>
<web>
<Resource id="myDatabase" type="DataSource">
JdbcDriver com.mysql.cj.jdbc.Driver
JdbcUrl jdbc:mysql://localhost:3306/db-name
UserName root
Password password
</Resource>

<Resource id="myDatabaseUnmanaged" type="DataSource">
JdbcDriver com.mysql.cj.jdbc.Driver
JdbcUrl jdbc:mysql://localhost:3306/db-name
UserName root
Password password
JtaManaged false
</Resource>

<Resource id="MissionDB" type="DataSource">
JdbcDriver com.mysql.cj.jdbc.Driver
JdbcUrl jdbc:mysql://localhost:3306/db-name
UserName root
Password password
</Resource>

<Resource id="MissionDBUnmanaged" type="DataSource">
JdbcDriver com.mysql.cj.jdbc.Driver
JdbcUrl jdbc:mysql://localhost:3306/db-name
UserName root
Password password
JtaManaged false
</Resource>

</web>

For TomEE in the TomEE installation folder/conf/tomee.xml like this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="MySQL Database" type="DataSource">
# MySQL example
#
# This connector will not work until you download the driver at:
# http://www.mysql.com/downloads/api-jdbc-stable.html
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://endpoint:port
UserName username
Password password
</Resource>
</tomee>

Setup Artifacts

In order to run our server we need to modify previously added artifacts or newly imported one by adding every Available Elements displayed on the right by double clicking, adding Compile Output and Sources then click on Apply.

image

Convert Eclipse Project to IntelliJ Project

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

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

image