This is Step B, the final setup stage in our Building a Microservices Project series before we actually start building our application.
By the end of this post we will have a working Java development environment, installed Activator, will have created a test application, and learned how to compile and run the application locally.
Activator is a build tool that makes it much easier to pull in required libraries, run a Java web server, and create skeleton projects. A skeleton project gives us the bones needed to build new applications more quickly.
Before we get started, please make sure you already have the latest Java runtime environment installed.
Please also install the IntelliJ IDE (Integrated Development Environment):
The free community version of IntelliJ is sufficient. If you prefer to use another IDE, go for it! Unfortunately, you’ll have to figure out how to configure your project in that IDE, though I am sure there are plenty of helpful tutorials out there. I will be mentioning configuration steps specific to IntelliJ.
Newb Tip: If this is your first time installing IntelliJ, there is a first-time setup for plugins, etc. You can simply leave all the defaults and click Next a few times.
Next, install the latest Java JDK:
Make a note of the JDK install location. You can customize this however you wish, just remember it.
Windows users extra step
If you do not use Windows, you can skip this short section.
In order for java and javac to work properly in our Cygwin terminal, we need to add the JDK bin to our system PATH.
- Go to Control Panel -> System -> Advanced system settings
- Select the Advanced tab
- Click Environment Variables
- Underneath System variables, select Path and click Edit
- Click Browse
- Navigate to where you installed the JDK, and select the bin directory within it
- Click OK to exit all the windows
- Open up your terminal and run:
- $ javac -help
- Make sure you get a lengthy usage description and not an error
When you visit the link below, look for the section at the top, “Alternative downloads”, and click Offline distribution.
After the download completes, extract its contents. Move activator into your home directory, and add it to your PATH.
Mac OS X / Linux
- Edit ~/.profile
- Add or change the PATH line to include the activator location:
- Additionally, ensure activator is executable:
- $ chmod u+x /path/to/activator-x.x.x/activator
- Move the extracted activator directory into your Cygwin home directory. For me this looked like: D:\cygwin64\home\Elliot\activator-dist-1.3.10
- Add the activator bin to your system PATH:
- Control Panel -> System -> Advanced system settings
- Advanced tab, Environment variables
- Underneath System variables, click Path and then click Edit
- Click Browse, and locate the new activator home as described above, but be sure to select the bin directory. For me, this was D:\cygwin64\home\Elliot\activator-dist-1.3.10\bin
- Click OK to close all windows
Now, ensure that activator is ready. Open a new terminal, and type:
$ activator -h
If you don’t see any errors, you are good to proceed.
Create your first Play app
From the terminal, cd to your home directory. Feel free to create a subdirectory within here, called “code” or “projects”.
- $ mkdir ~/projects
- $ cd ~/projects
Now let’s create a new Play applcation, called little-app:
- $ activator new little-app play-java
This tells activator to create a new project using the Play framework and Java, called little-app. It creates a new directory for you, little-app, which you can now cd to.
- $ cd little-app
Now let’s launch the web service and make sure we have our first demo page:
- $ activator run
Upon first run, activator will download many dependencies. This gets quicker the next time you call run for the project.
At some point, you will see the downloading come to a conclusion, and some text saying “Running the application … Listening for HTTP on … :9000”. Let’s see if this is really true. Open your browser, and go to: http://127.0.0.1:9000/
There may be a few second delay before you see the demo Play page. Your sources were compiling.
So, if you do see your first demo page, then excellent! We have a working development environment and are ready to begin building the first microservice Java Play application.
To stop the Play application from running, simply type Ctrl+D (Ctrl+C also works).