Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Transitioning to Java

You're reading from   Transitioning to Java Kickstart your polyglot programming journey by getting a clear understanding of Java

Arrow left icon
Product type Paperback
Published in Apr 2023
Publisher Packt
ISBN-13 9781804614013
Length 354 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ken Fogel Ken Fogel
Author Profile Icon Ken Fogel
Ken Fogel
Arrow right icon
View More author details
Toc

Table of Contents (23) Chapters Close

Preface 1. Part 1:The Java Development Environment
2. Chapter 1: Understanding Java Distributions FREE CHAPTER 3. Chapter 2: Code, Compile, and Execute 4. Chapter 3: The Maven Build Tool 5. Part 2:Language Fundamentals
6. Chapter 4: Language Fundamentals – Data Types and Variables 7. Chapter 5: Language Fundamentals – Classes 8. Chapter 6: Methods, Interfaces, Records, and Their Relationships 9. Chapter 7: Java Syntax and Exceptions 10. Chapter 8: Arrays, Collections, Generics, Functions, and Streams 11. Chapter 9: Using Threads in Java 12. Chapter 10: Implementing Software Design Principles and Patterns in Java 13. Chapter 11: Documentation and Logging 14. Chapter 12: BigDecimal and Unit Testing 15. Part 3:GUI and Web Coding in Java
16. Chapter 13: Desktop Graphical User Interface Coding with Swing and JavaFX 17. Chapter 14: Server-Side Coding with Jakarta 18. Chapter 15: Jakarta Faces Application 19. Part 4:Packaging Java Code
20. Chapter 16: Deploying Java in Standalone Packages and Containers 21. Index 22. Other Books You May Enjoy

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.

You have been reading a chapter from
Transitioning to Java
Published in: Apr 2023
Publisher: Packt
ISBN-13: 9781804614013
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image