Install Java on WSL2
Install a default version for current Ubuntu WSL2
- Execute the following commands to install default Java:
Terminal window sudo apt updatesudo apt search jdksudo apt install default-jdk - Alternatively, if you want to install a specific version, sepecify a package as the following command:
Terminal window sudo apt install openjdk-8-jdk
Set Java home
- Edit .bashrc file to export JAVA_HOME variable:
Terminal window sudo vi ~/.bashrcexport JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 - Open a new terminal session and verify if we have set Java home correclty
Terminal window java --version
Install Gradle for building Java project
- Download Gradle to
/tmp
folder:Terminal window wget https://services.gradle.org/distributions/gradle-7.3.3-bin.zip -P /tmp - Extract zip file to
/opt/gradle
:Terminal window sudo apt install unzipsudo unzip -d /opt/gradle /tmp/gradle-*.zip- Verify Gradle files
Terminal window ls /opt/gradle/gradle-7.3.3/ - You should see some Gradle files.
- Set up environment variable by creating gradle.sh:
Terminal window sudo vi /etc/profile.d/gradle.sh - Add the follwing content to gradle.sh
/etc/profile.d/gradle.sh export GRADLE_HOME=/opt/gradle/gradle-7.3.3export PATH=${GRADLE_HOME}/bin:${PATH} - Make the script executable:
Terminal window sudo chmod +x /etc/profile.d/gradle.sh - Load the environment variable
Terminal window source /etc/profile.d/gradle.sh - Verify if Gradle has been installed successfully:
Terminal window gradle -v