Preparing Your
Development Environment
To start learning Java, you need a few things installed on your computer. The following are the requirements:
- Java support on your computer (kinda’ mandatory).
- An integrated development environment, also known as IDE, which is basically an application in which you write your code and that you use to compile and execute it.
- The recommended IDE is Eclipse. You can go to their website to get the free community edition; for the purposes of the book, it will do.
- Or, you can choose the most popular free IDE for Java development: Eclipse.
- Or, you can try NetBeans,1 which is the default choice for most beginners because it was bundled with the JDK until version 8.
- Gradle is a build tool used to organize projects, to easily handle dependencies, and make your work easier as your projects get bigger. (It is mandatory because the projects in this book are organized and built on a Gradle setup.)
- Git is a versioning system that you can use to get the sources for the book, and you can experiment with it and create your own version. It is optional because GitHub, which is where the sources for this chapter are hosted, supports direct download.4
To write and execute Java programs/applications, you only need the Java Development Kit installed. All other tools that I’ve listed here are only needed to make your job easier and to familiarize you with a real development job.
Installing Java
Here you are with your computer and you can’t wait to start writing Java applications. But first, you need to get yourself a JDK and install it. For this, you need an Internet connection to open https://developer.oracle.com/java.
Scroll down until you see the Downloads section. Click the Java SE link. The two links and their contents are depicted in Figure
Figure:-Navigating the Oracle site to find the desired product, JDK in this case
On the Oracle site, you find the latest stable Java version. Click the Download JDK button. You should be redirected to the page depicted in Figure
Figure:-The Oracle page where you can download the desired JDK
As you can see, JDK is available for a few operating systems. You should download the one matching yours. For writing this book and the source code, I used a macOS computer, which means I download the JDK with the .dmg extension. You need to accept the license agreement before being allowed to download the desired JDK. You can read it if you are curious, but basically, it tells you that you are allowed to use Java as long as you do not modify its original components. It also tells you that you are responsible for how you use it, so if you use it to write or execute dangerous applications, you are legally responsible.
After you download the JDK, the next step is to install it. Just double-click it and click Next until finished. This works for Windows and macOS. The JDK is installed in a specific location.
In Windows, it is
C:\ProgramFiles\Java\jdk-10.
In macOS, it is
/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home.
On Linux systems, depending on the distribution, the JDK install location varies. My preferred way is to get the *.tar.gz from the Oracle site that contains the full content of the JDK, unpack it, and copy it to a specific location. Also, my preferred location on Linux is /home/iuliana.cosmina/tools/jdk-10.jdk.
The JAVA_HOME Environment Variable
The most important directory in the JDK is the bin directory, because that directory has to be added to the path of your system so you can call the Java executables from anywhere. This allows other applications to call them as well, without extra configurations steps needed. Most IDEs used for handling8 Java code are written in Java, and they require knowing where the JDK is installed so that they can be run. This is done by declaring an environment variable named JAVA_HOME that points to the location of the JDK directory. To make the Java executables callable from any location within a system, you must add the bin directory to the system path. The next three sections explain how to do this on the three most common operating systems
JAVA_HOME on Windows
To declare the JAVA_HOME environment variable on a Windows system, you need to open the dialog window for setting up system variables. On Windows systems, click the Start button; in the menu, there is a search box (or right-click the Start button for a context- menu and select Search). Enter the word environment in there (the first three letters should suffice) and the option should become available for clicking. These steps are depicted in Figure
Figure:-Windows menu item to configure environment variables
After clicking that menu item, a window like the one shown in Figures should open.
Figure:- First dialog window to set environment variables on Windows
Click the Environment Variables button. Another dialog window opens, which is split into two sections: user variables and system variables. You are interested in system variables because that is where we declare JAVA_HOME. Just click the New... button and a small dialog window appears with two text fields; one requires you to enter the variable name—JAVA_HOME in this case, and one requires you to enter the path—to the JDK in this case. The second window and the variable information pop-up dialog window are depicted in Figure
Figure:- Declaring JAVA_HOME as a system variable on Windows
After defining the JAVA_HOME variable, you need to add the executables to the system path. This can be done by editing the Path variable. Just select it from the System Variables list and click the Edit... button. Starting in Windows 10, each part of the Path variable is shown on a different line, so you can add a different line and add %JAVA_ HOME%\bin on it. This syntax is practical because it takes the location of the bin directory from whatever location the JAVA_HOME variable contains. The dialog window is depicted in Figure
Figure:-Declaring the JDK executables directory as part of the system Path variable on Windows 10
On older Windows systems, the contents of the Path variable are depicted in the dialog box shown in Figure 2-7, so you must add the %JAVA_HOME%\bin text in the Variable value text field, and separate it from the existing content by using a semicolon (;).
No matter which Windows system you have, you can check that you set everything correctly by opening Command Prompt and executing the set command. This lists all the system variables and their values. JAVA_HOME and Path should be there with the desired values. For the setup proposed in this section when executing set the output is depicted in Figure
Figure:-Windows system variables listed with the set command
If you execute the previous command and see the expected output and then execute java -version in the command prompt, it prints the expected result. You are all set.
...> java -version java version
"10-ea" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10-ea+42)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10-ea+42, mixed mode)
JAVA_HOME on macOS
The location in which JDK is installed is /Library/Java/JavaVirtualMachines/jdk- 10.jdk/Contents/Home. Your JAVA_HOME should point to this location. To do this for the current user, you can do the following:
1. In the /Users/your.user directory, create a file named .bash_profile.
2. In this file, write the following:
export JAVA_HOME=$(/usr/libexec/java_home -v10)
export PATH=$JAVA_HOME/bin:$PATH
On macOS, you can simultaneously install multiple Java versions. You can set which version is the one currently used on the system by obtaining the JDK location for the desired version by calling the /usr/libexec/java_home command and giving the Java version you are interested in as the argument. The result of executing the command is stored as a value for the JAVA_HOME variable. On my system, I have JDK 8, 9, 10, and 11 installed. If I execute the command, giving an argument to each of the Java versions, look at what happens:
$ /usr/libexec/java_home -v11 /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
$ /usr/libexec/java_home -v10 /Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
$ /usr/libexec/java_home -v9 /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
$ /usr/libexec/java_home -v1.8 /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
Depending of the version given as argument, a different JDK location is returned. If you want to test the value of the JAVA_HOME, the echo command can help with that.
$ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
The line export PATH=$JAVA_HOME/bin:$PATH adds the contents of the bin directory from the JDK location to the system patch. This means that I could open a terminal and execute any of the Java executables under it. For example, I could verify that the Java version set as default for my user is the expected one by executing java –version.
$ java -version java version "10-ea" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10-ea+42)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10-ea+42, mixed mode)
If you do all of this and java -version prints the expected result, you are all set.
JAVA_HOME on Linux
Linux systems are Unix-like operating systems. This is similar to macOS, which is based on Unix. Depending on your Linux distribution, installing Java can be done via the specific package manager or by directly downloading the JDK as a *.tar.gz archive from the official Oracle site.
If Java is installed using a package manager, the necessary executables are usually automatically placed in the system path at installation time. That is why in this book, we cover only the cases where you do everything manually, and choose to install Java only for the current user in a location such as
/home/your.user/tools/jdk-10.jdk,9 because covering package managers is not the object of the book after all.
So, after downloading the JDK archive from the Oracle site and unpacking it at /home/your.user/tools/jdk-10.jdk, you need to create a file named either .bashrc or .bash_profile11 in your user home directory and add the following to it.
export JAVA_HOME=/home/your.user/tools/jdk-10.jdk
export PATH=$JAVA_HOME/bin:$PATH
As you can see, the syntax is similar to macOS. To check the location of the JDK and the Java version, same commands mentioned in the macOS section can be used.
Installing Gradle
The sources attached to this book can be compiled and executed using the Gradle wrapper, which is a batch script on Windows and a shell script for other operating systems. When you start a Gradle build via the wrapper, Gradle automatically downloads and runs the build; thus you do not to really need to install Gradle. Instructions on how to do this can be found by reading the public documentation at www.gradle.org/docs/current/userguide/gradle_wrapper.html.
A good practice is to keep code and build tools separate, and for the project attached to this book this is the recommended way to go.
If you decide to use Gradle outside the editor, you can download the binaries only (or if you are curious, you can download the full package, which contains binaries, sources, and documentation) from the official site (www.gradle.org), unpack them, and copy the contents somewhere on the hard drive. Create a GRADLE_HOME environment variable and point it to the location where you have unpacked Gradle. Also, add %GRADLE_HOME%\bin for Windows, or $GRADLE_HOME/bin for Unix-based operating systems, to the general path of the system.
Gradle was chosen as a build tool for the sources of this book because of the easy setup, small configuration files, flexibility in defining execution tasks, and because it is practical to learn a build tool—because for medium-sized and large projects, they are a must-have.
You should see something similar to this:
-----------------------------------------------------------
Gradle 5.0-20180826235923+0000
-----------------------------------------------------------
Build time: 2018-08-26 23:59:23 UTC
Revision: c2edb259761ee18f9a14e271f24ef58530b1300f
Kotlin DSL: 1.0-rc-3
Kotlin: 1.2.60
Groovy: 2.4.15
Ant: Apache Ant (TM) version 1.9.11 compiled on March 23 2018
JVM: 10 (Oracle Corporation 10+46)
OS: -- whatever operating system you have -
________________________________________________________________________________
The preceding text is confirmation that Gradle commands can be executed in your terminal; thus,
Gradle was installed successfully.
Installing Git
This is an optional section, but as a developer, being familiar with a versioning system is important, so here it is. To install Git on your system, just go to the official page at https://git-scm.com/downloads and download the installer. Open the installer and click Next until done. This works for Windows and macOS.12 Yes, it is this easy. You do not need to do anything else.13 For Linux, you can use your package manager or PPA to install Git.
To test that Git installed successfully on your system, open a terminal (Command Prompt in Windows, and any type of terminal you have installed on macOS and Linux) and run git --version to see the result that it is printed. It should be the version of Git that you just installed.
$ git –version git version 2.15.1
Now that you have Git installed, you can get the sources for this book by cloning the official Git repository in a terminal or directly from the IDE. But more about this a little bit later.
Installing a Java IDE
You can use drive to download the Eclipse.
Scroll Down the menu and search for Eclipse for Java Developer.Then Click install.
1 Comments
hii
ReplyDeleteif you have any doubts.Please let me know