Chapter Two - Install the Necessary Software
I suggest you first, read this whole chapter, and then work through the chapter from the beginning and follow the steps listed.
Introduction
Programming requires you to setup a bunch of tools to allow you to work.
For Java, this means you need to install:
- JDK - Java Development Kit
- IDE - Integrated Development Environment
For this book we are also going to install:
- Maven - a dependency management and build tool
Installing Maven adds an additional degree of complexity to the setup process, but trust me. It will make the whole process of building projects and taking your Java to the next level a lot easier.
I have created a support page for installation, with videos and links to troubleshooting guides.
If you experience any problems that are not covered in this chapter, or on the support pages, then please let me know so I can try to help, or amend this chapter, and possibly add new resources to the support page.
Do you already have JDK or Maven installed?
Some of you may already have these tools installed with your machine. The first thing we should do is learn how to check if they are installed or not.
Java JDK
Many of you will already have a JRE installed (Java Runtime Environment), but when developing with Java we need to use a JDK.
If you type javac -version at your command line and get an error saying that javac can not be found (or something similar). Then you need to install and configure a JDK.
If you see something similar to:
> javac -version
javac 1.7.0_10
Then you have a JDK installed. It is worth following the instructions below to check if your installed JDK is up to date, but if you have a 1.7.x JDK (or higher) installed then you have a good enough version to work through this book without amendment. If your JDK is version 1.6 then some of the code examples will not work.
Install Maven
Maven requires a version of Java installed, so if you checked for Java and it wasn’t there, you will need to install Maven.
If you type mvn -version at your command line, and receive an error that mvn can not be found (or something similar). Then you need to install and configure Maven before you follow the text in this book.
If you see something similar to:
> mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 08:44:56+0000)
Maven home: C:\mvn\apache-maven-3.0.4
Java version: 1.7.0_10, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_10\jre
Default locale: en_GB, platform encoding: Cp1252
OS name: "windows 8", version: "6.2", arch: "amd64", family: "windows"
Then you have Maven installed. This book doesn’t require a specific version of Maven, but having a version of 3.x.x or above should be fine.
Install The Java JDK
The Java JDK can be downloaded from oracle.com. If you mistakenly download from java.com then you will be downloading the JRE, and for development work we need the JDK.
From the above site you should follow the installation instructions for your specific platform.
You can check the JDK is installed by opening a new command line and running the command:
javac -version
This should show you the version number which you downloaded and installed from oracle.com
Install Maven
Maven is a dependency management and build tool. We will use it to add JUnit to our project and write our code based on Maven folder conventions to make it easier for others to review and work with our code base.
The official Maven web site is maven.apache.org. You can download Maven and find installation instructions on the official web site.
Download Maven by visiting the download page:
The installation instructions can also be found on the download page:
I summarize the instructions below:
For Windows:
- Unzip the distribution archive where you want to install Maven
- Create an
M2_HOMEuser/environment variable that points to the above directory - Create an
M2user/environment variable that points toM2_HOME\bin- on Windows
%M2_HOME%\bin- sometimes on Windows, I find I have to avoid re-using the
M2_HOMEvariable and instead copy the path in again
- sometimes on Windows, I find I have to avoid re-using the
- on Unix
$M2_HOME/bin
- on Windows
- Add the
M2user/environment variable to your path - Make sure you have a
JAVA_HOMEuser/environment variable that points to your JDK root directory - Add
JAVA_HOMEto your path
For Mac:
- Unzip the distribution archive
- if you don’t have a
/usr/localfolder then create one withsudo mkdir /usr/localfrom a terminal - extract the contents into an
/usr/local/apache-maven - edit
~/.bash_profile - add the following lines to your
.bash_profilefileexport M2_HOME=/usr/local/apache-mavenexport M2=$M2_HOME/binexport PATH=$M2:$PATHexport JAVA_HOME="$(/usr/libexec/java_home)"
- save your
.bash_profilefile - from a terminal enter
source ~/.bash_profile
You can check it is installed by opening up a new command line and running the command:
mvn -version
This should show you the version number that you just installed and the path for your JDK.
I recommend you take the time to read the “Maven in 5 Minutes” guide on the official Maven web site:
Install The IDE
While the code in this book will work with any IDE, I recommend you install IntelliJ. I find that IntelliJ works well for beginners since it tends to pick up paths and default locations better than Eclipse.
For this book, I will use IntelliJ and any supporting videos I create for this book, or any short cut keys I mention relating to the IDE will assume you are using IntelliJ.
The official IntelliJ web site is jetbrains.com/idea
IntelliJ comes in two versions a ‘Community’ edition which is free, and an ‘Ultimate’ edition which you have to pay for.
For the purposes of this book, and most of your automation development work, the ‘Community’ edition will meet your needs.
Download the Community Edition IDE from:
The installation should use the standard installation approach for your platform.
When you are comfortable with the concepts in this book, you can experiment with other IDEs e.g. Eclipse or Netbeans.
I suggest you stick with IntelliJ until you are more familiar with Java because then you minimize the risk of issues with the IDE confusing you into believing that you have a problem with your Java.
Create a Project using the IDE
To create your first project, use IntelliJ to do the hard work. The instructions below are for IntelliJ 14, but should be very similar for future versions of IntelliJ. Remember to check JavaForTesters.com/install for updates and additional videos.
- Start your installed IntelliJ
- Either use the “Create New Project” wizard that starts when you first run the application or,
File \ New Project - choose
Maven- If maven hasn’t filled in the Project SDK automatically then select
[New]and choose the location of your JDK
- If maven hasn’t filled in the Project SDK automatically then select
- Press
[Next] - For GroupId and ArtifactId enter the name of your project, I used ‘javaForTesters’
- Leave the version as the default ‘1.0-SNAPSHOT’, and press [Next]
- Enter a project name, I used ‘javaForTesters’
- Select a location to save the project source files
- select
Finish - select
OK
You should be able to use all the default settings for the wizard.
About your new project
The New Project wizard should create a new folder with a structure something like the following:
+ javaForTesters
+ .idea
+ src
+ main
+ java
+ resources
+ test
+ java
javaForTesters.iml
pom.xml
In the above hierarchy,
- the
.ideafolder is where most of the IntelliJ configuration files will be stored, - the
.imlfile has other IntelliJ configuration details, - the
pom.xmlfile is your Maven project configuration file.
If the wizard created any .java files in any of the directories then you can delete them as they are not important. You will be starting this project from scratch.
The above directory structure is a standard Maven structure. Maven expects certain files to be in certain directories to use the default Maven configuration. Since you are just starting you can leave the directory structure as it is.
Certain conventions that you will follow to make your life as a beginning developer easier:
- Add your JUnit Test Classes into the
src\test\javafolder hierarchy - When you create a JUnit Test Class, make sure you append
Testto the Class name
The src\main\java folder hierarchy is for Java code that is not used for asserting behaviour. Typically this is application code. We will use this for our abstraction layer code. We could add all the code we create in this book in the src\test\java hierarchy but where possible I split the abstraction code into a separate folder.
The above convention description may not make sense at the moment, but hopefully it will become clear as you work through the book. Don’t worry about it now.
The pom.xml file will probably look like the following:
<?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>javaForTesters</groupId>
<artifactId>javaForTesters</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
This is the basics for a blank project file and defines the name of the project.
You can find information about the pom.xml file on the official Maven site.
Add JUnit to the pom.xml file
We will use a library called JUnit to help us run our code.
You can find installation instructions for using JUnit with Maven on the JUnit web site.
We basically edit the pom.xml file to include a dependency on JUnit. We do this by creating a dependencies XML element and a dependency XML element which defines the version of JUnit we want to use. At the time of writing it was version 4.11
The pom.xml file that we will use for this book, only requires a dependency on JUnit, so it looks like this:
<?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>javaForTesters</groupId>
<artifactId>javaForTesters</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
You can see I also added a build section with a maven-compiler-plugin. This was mainly to cut down on warnings in the Maven output. If you really want to make the pom.xml file small you could get away with adding the <dependencies> XML element and all its containing information about JUnit.
Amend your pom.xml file to contain the dependencies and build elements above. IntelliJ should download the JUnit dependency ready for you to write your first JUnit Test, in the next chapter.
You can find more information about this plugin on the Maven site:
Summary
If you followed the instructions in this chapter then you should now have:
- Maven installed -
mvn -version - JDK installed -
javac -version - IntelliJ IDE installed
- Created your first project
- A
pom.xmlfile with JUnit as a dependency
I can’t anticipate all the problems you might have installing the three tools listed in this chapter (JDK, Maven, IDE).
The installation should be simple, but things can go wrong.
I have created a few videos on the JavaForTesters.com/install site which show how to install the various tools.
I added some Maven Troubleshooting Hints and Tips to the “Java For Testers” blog:
If you do get stuck then try and use your favourite search engine and copy and paste the exact error message you receive into the search engine and you’ll probably find someone else has already managed to resolve your exact issue.