Logo

dev-resources.site

for different kinds of informations.

JDK Setup On Linux, Windows, Mac

Published at
9/16/2024
Categories
java
jdk
javaprogramming
webdev
Author
thekalderon
Categories
4 categories in total
java
open
jdk
open
javaprogramming
open
webdev
open
Author
11 person written this
thekalderon
open
JDK Setup On Linux, Windows, Mac

Setting Up JDK on Windows, macOS, and Linux
To start programming in Java, the first essential step is setting up the Java Development Kit (JDK). The JDK provides tools required to compile, debug, and run Java programs. This guide will walk you through the process of installing the JDK on three major operating systems: Windows, macOS, and Linux, with accompanying command-line commands where applicable.

1.** Setting Up JDK on Windows**
Step 1: Download the JDK
Visit the Oracle JDK website or OpenJDK website to download the latest JDK version for Windows.
Choose the appropriate installer based on your system architecture (e.g., jdk-11.0.x_windows-x64_bin.exe).

Step 2: Install the JDK
Run the installer and follow the installation wizard.
Accept the license agreement and choose the installation directory (default is usually C:\Program Files\Java\jdk-).
Finish the installation.
Step 3: Set Up Environment Variables
You need to add the JDK bin directory to your system's PATH variable so you can compile and run Java programs from any directory.

Open the System Properties window:
Press Win + R, type sysdm.cpl, and hit Enter.
Navigate to the Advanced tab and click on Environment Variables.
Under System variables, find the Path variable, select it, and click Edit.

In the Edit Environment Variable window, click New and add the path to your JDK’s bin directory (e.g., C:\Program Files\Java\jdk-\bin).
Click OK to close all windows.

Step 4: Verify the Installation
Open a command prompt and type the following:

java -version

You should see output indicating the installed Java version.


javac -version
Enter fullscreen mode Exit fullscreen mode

This command checks if the Java compiler (javac) is correctly installed.

  1. Setting Up JDK on macOS Step 1: Download the JDK Go to the Oracle JDK website or OpenJDK website. Download the .dmg file for macOS. Step 2: Install the JDK Open the downloaded .dmg file and follow the instructions to install it. The JDK will be installed in /Library/Java/JavaVirtualMachines/. Step 3: Set Up Environment Variables To set up the JDK path, you can modify your shell configuration file (~/.bash_profile, ~/.zshrc, etc.), depending on your shell.

Open the terminal.
Open the configuration file using an editor (e.g., for bash or zsh):

nano ~/.bash_profile # For bash users

nano ~/.zshrc # For zsh users

Add the following lines to set JAVA_HOME and add Java binaries to the PATH:

export JAVA_HOME=$(/usr/libexec/java_home)

export PATH=$JAVA_HOME/bin:$PATH

Save and close the file.
Reload the shell configuration:

source ~/.bash_profile # For bash users

source ~/.zshrc # For zsh users
Step 4: Verify the Installation
To confirm the JDK is correctly installed, run the following commands in your terminal:

java -version

javac -version

  1. Setting Up JDK on Linux Step 1: Install the JDK On Linux, you can use package managers to install OpenJDK. Here are the commands for different distributions: For Ubuntu/Debian: sudo apt update

sudo apt install openjdk-21-jdk
For CentOS/RHEL/Fedora:
sudo dnf install java-21-openjdk-devel
For Arch Linux:
sudo pacman -S jdk21-openjdk
Step 2: Set Up Environment Variables
Once the installation is complete, set up the JAVA_HOME and PATH variables by editing the .bashrc or .zshrc file:

nano ~/.bashrc # For bash users

nano ~/.zshrc # For zsh users

Add the following lines:

export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))

export PATH=$JAVA_HOME/bin:$PATH

Save and close the file, then reload it:

source ~/.bashrc # For bash users

source ~/.zshrc # For zsh users
Step 3: Verify the Installation
Check the JDK installation with the following commands:

java -version

javac -version
Enter fullscreen mode Exit fullscreen mode

Writing a Simple Java Program
Once the JDK is installed, you can write and run a simple Java program.
Step 1: Create a Java File
Use any text editor to create a Java file. Here's an example program:

// HelloWorld.java

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}
Enter fullscreen mode Exit fullscreen mode

Save the file as HelloWorld.java.

Step 2: Compile the Java Program
Open your terminal or command prompt and navigate to the directory where the HelloWorld.java file is located. Then, run the following command to compile the program:


javac HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

This will generate a HelloWorld.class file in the same directory.
Step 3: Run the Java Program
After compilation, run the program using the following command:

java HelloWorld
Enter fullscreen mode Exit fullscreen mode

You should see the output:

Hello, World!

Introduction to Data Structures and Algorithms (DSA)
What is DSA?
Data Structures and Algorithms (DSA) are the building blocks of software development. A data structure is a way of organizing data in memory so it can be used efficiently, while an algorithm is a step-by-step procedure for performing a task.
Basic Data Structures:
Arrays: A collection of elements stored in contiguous memory locations.
Linked Lists: A sequence of nodes, where each node contains data and a pointer to the next node.
Stacks: A LIFO (Last In, First Out) structure where elements are added and removed from one end.
Queues: A FIFO (First In, First Out) structure where elements are added at one end and removed from the other.
Hash Tables: A data structure that maps keys to values using a hash function.
Trees: A hierarchical structure with nodes, where each node has a value and references to child nodes.
Graphs: A collection of nodes (vertices) and edges that connect pairs of nodes.
Basic Algorithms:
Sorting: Organizing data in a specific order (e.g., Bubble Sort, Merge Sort, Quick Sort).
Searching: Finding an element in a data structure (e.g., Binary Search).
Traversal: Visiting all elements in a data structure (e.g., Tree Traversal using DFS or BFS).

Understanding DSA is crucial for solving problems efficiently and writing optimized code.

With the JDK set up, you can now explore Java programming and dive into implementing various data structures and algorithms. Happy coding!

jdk Article's
30 articles in total
Favicon
Garbage Collection in Java: Progress Since JDK 8
Favicon
Day 5 - sudo,apt,jdk
Favicon
1.What is APT in Linux?2.what is JDK in Linux?3.what happened behind the screen, after we entering JavaC?
Favicon
Preparando o ambiente de desenvolvimento da melhor API de tabelas de campeonato que você já viu!
Favicon
Java Compilation Process : From Source Code to Bytecode Execution
Favicon
Java JVM, JRE and JDK
Favicon
Exploring the New Features of Java 23
Favicon
# Must-Know Features in JDK 23: What’s New for Java Developers? 🚀
Favicon
Enhancing Performance with Static Analysis, Image Initialization and Heap Snapshotting
Favicon
JDK Setup On Linux, Windows, Mac
Favicon
Memory Management in GraalVM Native Image
Favicon
Fundamental Questions of Java
Favicon
Tổng quan về công nghệ java
Favicon
El Anuncio de Microsoft: Microsoft Build for OpenJDK 21
Favicon
Using JDK21 Preview Features and/or incubator classes
Favicon
How to Install Java JDK 17 on Windows 10
Favicon
VSCode Java Extension Pack Auto Config
Favicon
How to install Java in Pop!_Os
Favicon
Getting Started with Java Programming: Downloading and Installing JDK and IntelliJ
Favicon
Como intalar o Java JDK 17 no Ubuntu 22.04
Favicon
Instalando o JDK 17 ou 21 no Windows 11
Favicon
Spring boot 2.7.7 application deployment on Glassfish 6.2.3
Favicon
Step-by-Step Guide to Migrating from Java 8 to Java 17 (or Later): Tips, Checklist and Best Practices
Favicon
Switching between multiple versions of various tools
Favicon
Encryption raised an exception
Favicon
Install Java JDK on Arch Linux
Favicon
JDK vs JRE vs JVM: Asosiy Farqlari
Favicon
JVM, JDK,JRE ve ClassLoader Nedir ?
Favicon
Change the default Java Version on macOS
Favicon
The state of pattern matching in Java 17

Featured ones: