in the Microsoft.PowerShell_profile.ps1 writes this line in: $Env:Path = "C:\Program Files\Microsoft\jdk-17.0.11.9-hotspot\bin;" + $Env:Path
in the .zshrc, we have to write the corrected line for our .zshrc file: export PATH="/C/Program Files/Microsoft/jdk-17.0.11.9-hotspot:$PATH"
to test the jshell after add the env: JAVA_HOME=C:\Program Files\Microsoft\jdk-17.0.11.9-hotspot
echo %JAVA_HOME%
:\Users\DELL>jshell | Welcome to JShell -- Version 17.0.3.1 | For an introduction type: /help intro
jshell> System.out.println("Hello World") Hello World
jshell> System.out.println("Navin Reddy, Telusko") Navin Reddy, Telusko
jshell> 2+4 $3 ==> 6
jshell> 9-6 $4 ==> 3
jshell>
Please generate Command line in Java language.
public class CommandLineExample {
public static void main(String[] args) {
// Check if at least one argument is provided
if (args.length > 0) {
System.out.println("Arguments provided:");
for (String arg : args) {
System.out.println(arg);
}
} else {
System.out.println("No arguments provided.");
}
}
}
The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed for Java development.
To set up the JDK on a Windows machine, follow these steps:
-
Download the JDK:
- Go to the Oracle JDK download page.
- Download the appropriate version for your operating system.
-
Install the JDK:
- Run the downloaded installer.
- Follow the installation instructions.
-
Set up the Environment Variables:
- Open the Start Menu and search for "Environment Variables".
- Click on "Edit the system environment variables".
- In the System Properties window, click on the "Environment Variables" button.
- Under System Variables, click "New" and add a new variable:
- Variable name:
JAVA_HOME
- Variable value:
C:\Program Files\Java\jdk-<version>
(replace<version>
with the actual version number).
- Variable name:
- Find the
Path
variable in the System Variables section, select it, and click "Edit". - Click "New" and add
%JAVA_HOME%\bin
.
-
Verify the Installation:
- Open Command Prompt.
- Type
java -version
and press Enter. You should see the installed JDK version. - Type
javac -version
and press Enter. You should see the installed compiler version.
This completes the JDK setup on a Windows machine.
We can install multiple versions of the Java Development Kit (JDK) on the same machine, such as Java 17 LTS and Java 21 LTS. Here’s how you can manage multiple JDK versions on a Windows machine:
-
Install Multiple JDK Versions:
- Download and install the desired JDK versions from the Oracle JDK download page or other sources like AdoptOpenJDK.
- Install each JDK in a separate directory, for example:
C:\Program Files\Java\jdk-17
C:\Program Files\Java\jdk-21
-
Set Up Environment Variables:
- Open the Start Menu and search for "Environment Variables".
- Click on "Edit the system environment variables".
- In the System Properties window, click on the "Environment Variables" button.
- Under System Variables, click "New" and add a new variable for each JDK:
- Variable name:
JAVA_HOME_17
- Variable value:
C:\Program Files\Java\jdk-17
- Variable name:
JAVA_HOME_21
- Variable value:
C:\Program Files\Java\jdk-21
- Variable name:
- Modify the
Path
variable to include the desired JDK version. For example, to use JDK 17:- Find the
Path
variable in the System Variables section, select it, and click "Edit". - Add
%JAVA_HOME_17%\bin
to the list.
- Find the
- To switch to JDK 21, you can update the
Path
variable to include%JAVA_HOME_21%\bin
instead.
-
Switch Between JDK Versions:
- To switch between JDK versions, you can update the
JAVA_HOME
environment variable and thePath
variable accordingly. - Alternatively, you can use a script to switch between versions. For example, create a batch file to set the environment variables for JDK 17:
@echo off set JAVA_HOME=C:\Program Files\Java\jdk-17 set PATH=%JAVA_HOME%\bin;%PATH% echo Switched to JDK 17
- Create another batch file for JDK 21:
@echo off set JAVA_HOME=C:\Program Files\Java\jdk-21 set PATH=%JAVA_HOME%\bin;%PATH% echo Switched to JDK 21
- To switch between JDK versions, you can update the
By following these steps, you can manage and switch between multiple JDK versions on your Windows machine.
The Java Development Kit (JDK) and the Java Runtime Environment (JRE) are both essential components for Java development and execution, but they serve different purposes:
-
JDK (Java Development Kit):
- Purpose: Used for developing Java applications.
- Components: Includes the JRE, an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed for Java development.
- Usage: Required by developers to write, compile, and debug Java applications.
-
JRE (Java Runtime Environment):
- Purpose: Used for running Java applications.
- Components: Includes the Java Virtual Machine (JVM), core libraries, and other components necessary to run Java applications.
- Usage: Required by end-users to run Java applications but does not include development tools like the compiler and debugger.
In summary, the JDK is a superset of the JRE. The JDK includes everything the JRE has, plus additional tools for developing Java applications.
To configure the JDK version for your Java project in Visual Studio Code, follow these steps:
-
Install the Java Extension Pack:
- Open Visual Studio Code.
- Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing
Ctrl+Shift+X
. - Search for "Java Extension Pack" and install it.
-
Configure the JDK in Visual Studio Code:
- Open the Command Palette by pressing
Ctrl+Shift+P
. - Type
Java: Configure Java Runtime
and select it. - In the Java Configuration window, you can add or select the JDKs you have installed.
- Click on
Add JDK
and navigate to the directory where your JDK is installed (e.g.,C:\Program Files\Java\jdk-17
orC:\Program Files\Java\jdk-21
). - Set the JDK you want to use as the default by clicking on the
Set as Default
button next to the desired JDK version.
- Open the Command Palette by pressing
-
Configure the JDK in
settings.json
:- Open the Command Palette by pressing
Ctrl+Shift+P
. - Type
Preferences: Open Settings (JSON)
and select it. - Add or update the following settings in the
settings.json
file to specify the JDK version for your project:{ "java.home": "C:\\Program Files\\Java\\jdk-17", "java.configuration.runtimes": [ { "name": "JavaSE-17", "path": "C:\\Program Files\\Java\\jdk-17" }, { "name": "JavaSE-21", "path": "C:\\Program Files\\Java\\jdk-21" } ] }
- Replace the paths with the actual paths to your JDK installations.
- Open the Command Palette by pressing
-
Configure the JDK in
pom.xml
(for Maven projects):- If you are using Maven, you can specify the JDK version in your
pom.xml
file:<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties>
- Replace
17
with21
if you want to use JDK 21.
- If you are using Maven, you can specify the JDK version in your
-
Configure the JDK in
build.gradle
(for Gradle projects):- If you are using Gradle, you can specify the JDK version in your
build.gradle
file:java { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 }
- Replace
VERSION_17
withVERSION_21
if you want to use JDK 21.
- If you are using Gradle, you can specify the JDK version in your
By following these steps, you can configure the JDK version to use in your Java project in Visual Studio Code.
To create a batch script (multi_JDK.bat
) that allows you to switch between different JDK versions, you can use the following structure. This script will prompt you to choose which JDK version to switch to:
@echo off
echo Select the JDK version to switch to:
echo 1. Microsoft JDK 17
echo 2. Oracle JDK 17
echo 3. Oracle JDK 21
set /p choice=Enter your choice (1, 2, or 3):
if "%choice%"=="1" (
set JAVA_HOME=C:\Program Files\Microsoft\jdk-17.0.11.9-hotspot
set PATH=%JAVA_HOME%\bin;%PATH%
echo Switched to Microsoft JDK 17
) else if "%choice%"=="2" (
set JAVA_HOME=C:\Program Files\Java\jdk-17
set PATH=%JAVA_HOME%\bin;%PATH%
echo Switched to Oracle JDK 17
) else if "%choice%"=="3" (
set JAVA_HOME=C:\Program Files\Java\jdk-21
set PATH=%JAVA_HOME%\bin;%PATH%
echo Switched to Oracle JDK 21
) else (
echo Invalid choice. No changes made.
)
pause
-
Prompt User for Choice:
- The script prompts the user to select which JDK version to switch to.
-
Set Environment Variables Based on Choice:
- Depending on the user's choice, the script sets the
JAVA_HOME
and updates thePATH
environment variable to point to the selected JDK version.
- Depending on the user's choice, the script sets the
-
Echo Confirmation:
- The script echoes a confirmation message indicating which JDK version has been switched to.
-
Pause:
- The script pauses at the end to allow the user to see the confirmation message before the command prompt window closes.
Save this script as multi_JDK.bat
and run it whenever you need to switch between different JDK versions.
To configure the multi_JDK.bat
script for use with CMD, PowerShell, and Zsh, you need to create equivalent scripts for each shell environment. Here are the steps for each:
The multi_JDK.bat
script provided earlier is already suitable for CMD. You can run it directly in the Command Prompt.
Create a PowerShell script (multi_JDK.ps1
) with the following content:
Write-Host "Select the JDK version to switch to:"
Write-Host "1. Microsoft JDK 17"
Write-Host "2. Oracle JDK 17"
Write-Host "3. Oracle JDK 21"
$choice = Read-Host "Enter your choice (1, 2, or 3)"
switch ($choice) {
"1" {
$env:JAVA_HOME = "C:\Program Files\Microsoft\jdk-17.0.11.9-hotspot"
$env:PATH = "$env:JAVA_HOME\bin;$env:PATH"
Write-Host "Switched to Microsoft JDK 17"
}
"2" {
$env:JAVA_HOME = "C:\Program Files\Java\jdk-17"
$env:PATH = "$env:JAVA_HOME\bin;$env:PATH"
Write-Host "Switched to Oracle JDK 17"
}
"3" {
$env:JAVA_HOME = "C:\Program Files\Java\jdk-21"
$env:PATH = "$env:JAVA_HOME\bin;$env:PATH"
Write-Host "Switched to Oracle JDK 21"
}
default {
Write-Host "Invalid choice. No changes made."
}
}
Create a Zsh script (multi_JDK.sh
) with the following content:
#!/bin/zsh
echo "Select the JDK version to switch to:"
echo "1. Microsoft JDK 17"
echo "2. Oracle JDK 17"
echo "3. Oracle JDK 21"
read "choice?Enter your choice (1, 2, or 3): "
case $choice in
1)
export JAVA_HOME="/mnt/c/Program Files/Microsoft/jdk-17.0.11.9-hotspot"
export PATH="$JAVA_HOME/bin:$PATH"
echo "Switched to Microsoft JDK 17"
;;
2)
export JAVA_HOME="/mnt/c/Program Files/Java/jdk-17"
export PATH="$JAVA_HOME/bin:$PATH"
echo "Switched to Oracle JDK 17"
;;
3)
export JAVA_HOME="/mnt/c/Program Files/Java/jdk-21"
export PATH="$JAVA_HOME/bin:$PATH"
echo "Switched to Oracle JDK 21"
;;
*)
echo "Invalid choice. No changes made."
;;
esac
- Open Command Prompt.
- Navigate to the directory containing
multi_JDK.bat
. - Run the script:
multi_JDK.bat
.
- Open PowerShell.
- Navigate to the directory containing
multi_JDK.ps1
. - Run the script:
.\multi_JDK.ps1
.
- Open a terminal with Zsh (e.g., WSL on Windows, Terminal on macOS/Linux).
- Navigate to the directory containing
multi_JDK.sh
. - Make the script executable:
chmod +x multi_JDK.sh
. - Run the script:
./multi_JDK.sh
.
By following these steps, you can configure and use the multi_JDK
script in CMD, PowerShell, and Zsh environments.
To ensure the script works correctly in Git Bash with Zsh on Windows 11, you need to adjust the paths to use the Windows-style paths directly. Here is the corrected script:
#!/bin/zsh
echo "Select the JDK version to switch to:"
echo "1. Microsoft JDK 17"
echo "2. Oracle JDK 17"
echo "3. Oracle JDK 21"
read "choice?Enter your choice (1, 2, or 3): "
case $choice in
1)
export JAVA_HOME="C:/Program Files/Microsoft/jdk-17.0.11.9-hotspot"
export PATH="$JAVA_HOME/bin:$PATH"
echo "Switched to Microsoft JDK 17"
;;
2)
export JAVA_HOME="C:/Program Files/Java/jdk-17"
export PATH="$JAVA_HOME/bin:$PATH"
echo "Switched to Oracle JDK 17"
;;
3)
export JAVA_HOME="C:/Program Files/Java/jdk-21"
export PATH="$JAVA_HOME/bin:$PATH"
echo "Switched to Oracle JDK 21"
;;
*)
echo "Invalid choice. No changes made."
;;
esac
-
Shebang Line:
#!/bin/zsh
specifies that the script should be run with Zsh.
-
Echo Statements:
- Prompts the user to select the JDK version.
-
Read User Input:
read "choice?Enter your choice (1, 2, or 3): "
reads the user's choice.
-
Case Statement:
- Based on the user's choice, sets the
JAVA_HOME
and updates thePATH
environment variable. - Uses Windows-style paths (
C:/Program Files/...
) which are compatible with Git Bash.
- Based on the user's choice, sets the
-
Open Git Bash:
- Open Git Bash from the Start menu or a shortcut.
-
Navigate to the Directory:
- Use the
cd
command to navigate to the directory where your script is located. For example:cd /c/Users/chien/PowerShell
- Use the
-
Make the Script Executable:
- Ensure the script has execute permissions:
chmod +x multi_JDK.sh
- Ensure the script has execute permissions:
-
Run the Script:
- Execute the script:
./multi_JDK.sh
- Execute the script:
This should allow you to switch between different JDK versions using the script in Git Bash with Zsh on Windows 11.