Book Image

Transitioning to Java

By : Ken Fogel
Book Image

Transitioning to Java

By: Ken Fogel

Overview of this book

This comprehensive guide will help non-Java developers already using different languages transition from their current language to all things Java. The chapters are designed in a way that re-enforces a developer’s existing knowledge of object-oriented methodologies as they apply to Java. This book has been divided into four sections, with each section touching upon different aspects that’ll enable your effective transition. The first section helps you get to grips with the Java development environment and the Maven build tool for modern Java applications. In the second section, you’ll learn about Java language fundamentals, along with exploring object-oriented programming (OOP) methodologies and functional programming and discovering how to implement software design patterns in Java. The third section shows you how to code in Java on different platforms and helps you get familiar with the challenges faced on these platforms. In the fourth section, you’ll find out how you can manage and package your Java code. By the end of this Java programming book, you’ll have learned the core concepts of Java that’ll help you successfully transition from a different language to Java.
Table of Contents (23 chapters)
1
Part 1:The Java Development Environment
5
Part 2:Language Fundamentals
15
Part 3:GUI and Web Coding in Java
19
Part 4:Packaging Java Code

Installing Java

Installing Java is a simple process. As a developer, you will install the JDK from any of the distributors. Most Java distributors have packaged Java with an installer and as a compressed file without an installer that you can download. The choice depends on your OS, CPU, and whether you are the administrator or superuser and can use an installer. Or, you are a client and can only install the compressed file.

With your distribution and version decided, you are ready to install Java as an admin and non-admin.

As an admin

As an admin, you can install Java for all users of the computer in the following ways.

Windows

Download the appropriate (32- or 64-bit) .msi file for Java from https://adoptium.net/. This type of file contains an installer that will place Java in the folder of your choice and configure the appropriate environment variables. Just double-click on the .msi file after it is downloaded. The Windows installer will lead you through the installation.

macOS

You have two options for installing Java for macOS. The first is to download the .pkg file for Mac that includes an installer. Just double-click on the .pkg file after it is downloaded. The Apple installer will lead you through the installation.

The second is to use HomeBrew, a command-line utility for managing new software and updates, which will download and install Java.

With HomeBrew installed, you can install the OpenJDK version with the following:

brew install openjdk@17 

To install the Eclipse Temurin version of Java 17, use the following:

brew tap homebrew/cask-versions
brew install --cask temurin17

Linux

On Linux, you use the apt install command-line tool. You must be a superuser/admin to use this tool. You also include the distribution and version you require. You install the OpenJDK Java at the command line with the following:

sudo apt install openjdk-17-jdk

To install the Eclipse Temurin version of Java, use the following:

sudo apt install temurin-17-jdk

Verifying installation

Once the installation is complete, verify that Java works by issuing the following command:

java -version

If it shows you the version and distribution name of Java that you just installed, you are done and ready to code Java. The version number may be different depending on when you download Java or use apt install. Here is what you should see:

Windows

>java -version
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)
OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, 
    sharing)

Linux and macOS

$ java -version
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)
OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, 
    sharing)

If it tells you that it cannot find Java, then follow the instructions given in the coming Configuring environment variables section for setting up the environment variables.

As a non-admin

If you are not an admin, then you can still install Java but only you will be able to use it.

Windows

Windows users can download the appropriate .zip file version and unzip it in the desired folder.

Linux and macOS

Download the appropriate .tar.gz file version for either Linux or macOS. Once downloaded, use the following command line. The only difference between Linux and macOS is the name of the file.

For Linux, use the following:

tar xzf OpenJDK17U-jdk_x64_linux_hotspot_17.0.3_7.tar.gz

For macOS, use the following:

 tar xzf OpenJDK17U-jdk_x64_mac_hotspot_17.0.3_7.tar.gz 

Configuring environment variables

There are two environment variables that need to be set. While the environment variables are the same on Windows, Linux, and macOS, the process of setting them differs.

The first environment variable is JAVA_HOME. Certain Java processes, such as web servers, need to know where Java is installed to be able to access specific components in the JDK. It must be assigned the full path to the folder in which you have installed Java.

The second environment variable is PATH. When a program is run from the command line, the OS will look for an executable file in the current directory. If it is not found, then it will go through every directory in the path to look for it.

You will have to enter these commands every time you open a console. Adjust the command based on your login name and the version of Java you are installing. While you can install multiple versions of Java, only one can be used for JAVA_HOME and PATH:

Windows

set JAVA_HOME= C:\devapp\jdk-17.0.2+8
set PATH=%JAVA_HOME%\bin;%PATH%

Adjust the path to the folder created when you unzipped the Java file. You can also place these two lines in a batch file that you can run every time you open a console to code in Java:

Linux

export JAVA_HOME=/home/javadev/java/jdk-17.0.2+8
export PATH=$JAVA_HOME/bin:$PATH

This assumes that you are logged in as javadev and you are placing Java in a directory called java. These two lines can be added to your .profile file in your home directory so that they execute every time you log in.

macOS

export JAVA_HOME=/Users/javadev/java/jdk-17.03+7/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

This assumes that you are logged in as javadev and you are placing Java in a directory called java. These two lines can be added to your .bash.profile file in your home directory so that they execute every time you log in.

Verifying installation

You can quickly determine whether your installation of Java is correct. Open a command or console window on whatever system you are using. If you performed the non-admin installation, then ensure that JAVA_HOME and PATH have been updated and set. In the command window, enter the following:

java -version

If the installation was successful, the output, if you installed the OpenJDK, will be as follows:

openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+7-Ubuntu-0ubuntu0.20.04.1)
OpenJDK 64-Bit Server VM (build 17.0.3+7-Ubuntu-0ubuntu0.20.04.1, mixed mode, sharing)

The output, if you installed the Temurin JDK, will be as follows:

openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)
OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, 
    sharing)

Your installation is now complete and verified. Let's now examine some of the files that you just installed.