JavaRush /Java Blog /Random EN /Setting up a local environment for Java EE development
zor07
Level 31
Санкт-Петербург

Setting up a local environment for Java EE development

Published in the Random EN group
In this article we will set up a working environment for running Java EE applications. Let me remind you: in the previous material we covered the basics of Java EE . Setting up a local environment for Java EE development - 1By environment we mean a set of various software components:
  • Java EE SDK;
  • Java EE Application Server GlassFish;
  • Setting up IntelliJ IDEA.
The main goals of this material:
  1. Show how you can prepare a working environment for working with Java EE applications.
  2. Show how you can deploy and run Java EE applications from IntelliJ IDEA on the GlassFish server.
Here's what we'll do for this:
  1. Download and install Java EE 7 SDK.
  2. Let's raise Java EE Application Server GlassFish.
  3. Let's learn how to start and stop the server via the command line.
  4. Let's configure IntelliJ IDEA to deploy applications to this server.
  5. Let's deploy and run the demo application, which, together with the GlassFish application server, is included in the Java EE 7 SDK.
This article will be useful for novice Java developers who are getting acquainted with Enterprise development in Java. Consider this a little preparation of the ground for further practice. You will understand the material if you are familiar with basic Windows command line (PowerShell) skills. Versions of software components used in this article:
  • GlassFish 4.0;
  • IntelliJ IDEA 2019.3.1 (Ultimate);
  • Java EE SDK 7.

Installing Java EE SDK

Important condition. Before installing the Java EE SDK, you must make sure that the Java SE SDK is pre-installed.
  1. First, we need to download the SDK. To do this, you can type in a search engine: “Java EE SDK 7”, or directly go to the sdk download page from the Oracle website.

  2. From the list of SDKs provided, you need to select the one that is suitable for your platform. The article describes installing the distribution: “java_ee_sdk-7-jdk7-windows-x64-ml.exe”

  3. Run the installation file. During startup, the following error may occur (if it does not occur, then you can simply continue with the installation):

    Setting up a local environment for Java EE development - 2

    This means that the installer was unable to find the path to the pre-installed Java Runtime Environment. The error can be corrected by passing the path to the JRE manually using the command line. To do this, you need to run the installation file via PowerShell and pass the path to the pre-installed JRE via the -j parameter.

    For example, like this:

    Setting up a local environment for Java EE development - 3
  4. The further installation procedure is quite typical for most programs. All you need to do is click on the Next and Install buttons.

    The program will install the Java EE SDK, as well as the GlassFish server, which, upon completion of the installation, will be up and ready for use.

  5. To make sure that the GlassFish server is working, after installation is complete, you can open the server admin panel, which will be available at: http://localhost:4848/

    Screenshot below:

    Setting up a local environment for Java EE development - 4

GlassFish starting and stopping via command line

So, the server is up and running, but the server start was initiated by the installation program. Below we will try to figure out how to start and stop the server yourself. First, let's reveal such an entity as a domain. A domain is a set of one or more GlassFish server instances managed by one administration server. When installing the GlassFish server, a default domain is created - domain1. The following components are associated with it:
  • Server port (default 8080);
  • Administration server port (default 4848);
  • Administrator username and password (default is admin and no password required by default).
Domain1 has default values ​​assigned to it. Setting up a local environment for Java EE development - 5To start and stop the server, the GlassFish distribution includes several scripts, which by default are located in the following directory: C:\glassfish4\glassfish\bin To start and stop the GlassFish server via the command line, you can use the asadmin script and its commands:
asadmin start-domain domain_name
asadmin stop-domain domain_name
Let's try to stop the server (since it is already running after installation) by running PowerShell from the subdirectory C:\glassfish4\glassfish\bin : Setting up a local environment for Java EE development - 6The server is stopped. Now let's run it: Setting up a local environment for Java EE development - 7

We analyze the first Java EE application, dukes-age

Next we'll look at a demo Java EE application: dukes-age. This application is included in the Java EE SDK along with the first-cup application. Descriptions of these applications are presented on the official Oracle documentation website in sections 3 and 4 of the introductory course on Java EE for beginner Java EE developers . In this article, we will briefly look at the dukes-age application and examine in more detail the process of launching this application on the GlassFish server from the IntelliJ IDEA development environment. The application is located inside the glassfish server directory. If you did not change the default path for the glassfish server when installing the Java EE SDK, you can find this application in the folder: C:\glassfish4\docs\firstcup\example\dukes-age . Probably every Java developer, beginner and experienced, has at least once seen the mascot of the Java programming language - Duke: Setting up a local environment for Java EE development - 8Few people know that Duke has a birthday. He was born on May 23, 1995, when the first demo version of Java technology was published. The dukes-age app, as the name suggests, provides information about Duke's age. Let's open this application in IntelliJ IDEA: File -> New -> Project From Existing Sources... Then go to the folder with the dukes-age application, located by default at C:\glassfish4\docs\firstcup\example\dukes-age , select the file pom.xml and click OK: Setting up a local environment for Java EE development - 9An application with the following structure will open: Setting up a local environment for Java EE development - 10Let's take a look at the class code DukesAgeResource:
package firstcup.dukesage.resource;

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 * REST Web Service
 *
 */
@Path("dukesAge")
public class DukesAgeResource {

    /** Creates a new instance of DukesAgeResource */
    public DukesAgeResource() {
    }

    /**
     * Retrieves representation of an instance of DukesAgeResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/plain")
    public String getText() {
        // Create a new Calendar for Duke's birthday
        Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);
        // Create a new Calendar for today
        Calendar now = GregorianCalendar.getInstance();

        // Subtract today's year from Duke's birth year, 1995
        int dukesAge = now.get(Calendar.YEAR) - dukesBirthday.get(Calendar.YEAR);
        dukesBirthday.add(Calendar.YEAR, dukesAge);

        // If today's date is before May 23, subtract a year from Duke's age
        if (now.before(dukesBirthday)) {
            dukesAge--;
        }
        // Return a String representation of Duke's age
        return "" + dukesAge;
    }
}
This is a JAX-RS RESTful web service with one method - getText(). If you examine the code of this method, it becomes clear that the method returns the number of years that have passed since Duke's birth. That is, the method returns Duke's age. A class is a resource in a REST context.
  1. The annotation above the class @Path("dukesAge")defines the URI path to which calls will be processed by this class.
  2. The annotation above the method @GETtells us that this method processes GET requests.
  3. The annotation @Produces("text/plain")specifies the media type of the response to the request. It is not difficult to guess that the method getText()will generate responses with media type: text/plain.
Let's also take a look at the web.xml configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>
In this file, inside the tag servlet-mapping, a tag url-patternwith the value is defined /webapi/*. Here a certain root point is defined, calls to which will be processed further by the service. If combined with the annotation @Path("dukesAge"), it turns out that to get information about Duke’s age, you need to contact the address /webapi/dukesAge.

Setting up IntelliJ IDEA to run a Java EE application on a GlassFish server

Now that we have a very general idea of ​​the dukes-age application (read more about it on the Oracle site ), let's configure IntelliJ IDEA so that we can run this application from our IDE. Setting up IntelliJ IDEA
  1. To do this, go to the menu Run -> Edit Configurations.
  2. The Run/Debug Configurations panel will open: Setting up a local environment for Java EE development - 11
  3. We need to add a new configuration. To do this, click on the plus sign, or use the Alt+Insert hotkey. In the list that opens, select GlassFish Server -> Local: Setting up a local environment for Java EE development - 12
  4. In the window that opens, let's start setting up the configuration: Setting up a local environment for Java EE development - 13
  5. The next step is to indicate the artifacts that will need to be deployed to the server. This can be done through the Deployment tab by clicking on the plus -> Artifact... Setting up a local environment for Java EE development - 14
  6. Select dukes-age:war and click OK: Setting up a local environment for Java EE development - 15
  7. Next, save our configuration by clicking on Apply and then OK.
The configuration has been created!

Launching the application

Now let's try to run the application.
  1. To do this, you can use the hotkey Alt+Shift+F10, or through the menu Run -> Run...

    And select the dukes-age configuration we created:

    Setting up a local environment for Java EE development - 16

  2. After this we see the startup logs in the Services panel:

    Setting up a local environment for Java EE development - 17
  3. Then, if everything is done according to the instructions, the browser will open and it will become clear that nothing is working:

    Setting up a local environment for Java EE development - 18
Such is the IT world. Even if everything is done correctly, something may not work. But don’t worry: you could immediately describe how to do it right, but then some of the nuances would be less obvious. For example, how is the url that IDEA directs us to when launching the application generated? Let's pay attention to it. We were redirected to http://localhost:8080/dukes-age-7.0.1/ . To understand where this came from (/dukes-age-7.0.1/), let's go to the admin panel of the GlassFish server. In the left menu, click on Applications. Next you will see deployed applications, including dukes-age. When you select the dukes-age application, you can see some of its characteristics, including the Context-Root item: Setting up a local environment for Java EE development - 19dukes-age-7.0.1 is the name and version of the application. Root point. All requests that dukes-age will process must begin with the prefix /dukes-age-7.0.1/. So we have a root point: /dukes-age-7.0.1. Also, we previously discussed that our application will handle requests to /webapi/dukesAge. Let's combine these two things and update our configuration.
  1. To do this, let's go back to IntelliJ IDEA, then to the menu: Run -> Edit Configurations...

  2. Let's select the previously saved configuration and update the URL in accordance with the screenshot below:

    Setting up a local environment for Java EE development - 20
  3. Let's save the configuration and restart our application using the Ctrl+Shift+F10 hotkey, or using a special button on the Services panel:

    Setting up a local environment for Java EE development - 21
  4. After the restart, if everything was done correctly, we will see Duke's age:

    Setting up a local environment for Java EE development - 22
Great. We finally found out that Duke is 24 years old. If you do not repeat all the steps described above within the next couple of months, starting in April 2020, then Duke will probably age a little.

Conclusion

In this article, we looked at how you can configure your local environment to work with Java EE applications using the example of the GlassFish 4.0 server, the IntelliJ IDEA 2019.3.1 (Ultimate) development environment, as well as Java EE SDK version 7. We looked at how you can:
  • download and install Java EE SDK, as well as the GlassFish server;
  • stop and start the GlassFish server via PowerShell;
  • configure IntelliJ IDEA so that you can deploy Java EE applications from the IDE to the GlassFish server and launch them immediately.

Bonus task

  1. Repeat all the described steps yourself.
  2. Take an introductory course on Java EE for beginner Java EE developers from Oracle .
  3. Item with an asterisk. Try to deploy the second first-cup demo application yourself. This application is more interactive and also interacts with the dukes-age application. To complete this step, you will need to independently understand the following questions:
    1. How to enable the database inside the glassFish server;
    2. How to integrate two applications with each other (inside first-cup indicate the path to the dukes-age endpoint, which we discussed in this article);
    3. How to create a new configuration in Intellij IDEA for first-cup deployment (similar to what was shown in this article);
    4. Possibly many other questions)
PS The barrier to entry is high, but isn’t that what motivates you?
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION