Basic Setup for Java on Mac and Windows
Basic Setup for Java on Mac
It outlines the basic software tools required to build and manage your Java Projects on Mac. Make sure the user has admin privileges before you move to below sections.
Homebrew
Homebrew, an open source, simplifies the installation of several software on your mac.
Learn more about brew on https://brew.sh/ ;no more using sudo command.
Open a terminal and run the below command to install Homebrew. Please note that it might take upto 15 minutes to install Homebrew.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
In the end, it will display the below messages:
Java
Now, you have installed homebrew, use the same terminal or open a new terminal, and
use the below command to verify which version of java is already installed.
$ brew cask info java
The command will output the details of the java currently installed on your mac, one of sample outputs is:
To install the latest java, type the below command
$ brew cask install java
The latest Oracle JDK will be installed with the output:
To install another version, say Java 11, follow the commands as written below:
$ brew tap homebrew/cask-versions
$ brew cask install java11
The sample output is:
Now you have both java14 and java11 installed on your mac.
Maven
Maven is used to build and manage java projects. You can learn more about it at https://maven.apache.org/
We will make use of brew again to install maven.
Open a new terminal or use the existing and follow the below commands.
To install maven, type:
$ brew install maven
sample output:
To check location of maven, type:
$ which mvn
sample output:
/usr/local/bin/mvn
To check maven version, type:
$ mvn -v
sample output:
Git
Github is used to manage, host, maintain and organize your code and projects. Learn more about it at https://github.com/
To install git, open a new terminal or use existing one and type:
$ brew install git
sample output:
To check the location of git, follow the command:
$ which git
sample output:
/usr/local/bin/git
To check git location, type :
$ git --version
sample output:
git version 2.28.0
Configure .bash_profile
.bash_profile is a collection of all your configuration and preferences you want to load before starting your command interface or terminal. As it is prefixed with dot(.), it makes it invisible on mac.
To view invisible files including .bash_profile, use the command:
$ ls -al
Now, let’s setup the environment configuration in your -bash_profile
Use the below command to open .bash_profile, new .bash_profile will be created if you never created before
$ vi ~/.bash_profile
This will open vi editor, we will use Basic vi command to update the .bash_profile.
Set Java in .bash_profile - type the below commands in opened .bash_profile
JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-14.0.2.jdk/Contents/Home
export JAVA_HOME
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$JAVA_HOME/bin
export PATH
Set maven in .bash_profile
M2_HOME=/usr/local/Cellar/maven/3.6.3_1/libexec
export M2_HOME
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$M2_HOME/bin
export PATH
save .bash_profile
Switch JDK
You can include the below function/script in .bash_profile, if you need to switch among jdk versions
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 11
save .bash_profile
Verify that jdk 1.11 is set to work with using the command:
$ java -version
Sample Output:
Now, switch to jdk 14 using the command
$ setjdk 11
Verify the java version using the command;
$ java -version
sample output:
Basic Java Setup on Windows
This outlines the basic software tools required to build and manage your Java Projects on Windows. Make sure the user has admin privileges before you move to below sections.
Chocolatey (choco)
Chocolatey is an open-source software management tool for windows, counterpart of homebrew on mac. To learn more, visit: https://chocolatey.org/
You can install Chocolatey using PowerShell or Command Prompt.
To install using PowerShell:
Open a new PowerShell prompt as "Run as Administrator" and run the below command:
> Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('
https://chocolatey.org/install.ps1'))
To install using command prompt:
Open a new command prompt as "Run as Administrator" and run the following command to install Chocolatey
>@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -
ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.
org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Java
Next, let’s install java using choco:
Open a new Command/PowerShell prompt as "Run as Administrator", you can use the already opened common or PoweShell.
>choco install jdk14
Sample output:
Downloading JDK from http://download.oracle.com/otn-pub/java/jdk/8u201-b09
/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-windows-x64.exe
Installing jdk8…
jdk8 has been installed.
The install of jdk8 was successful.
Software installed as 'exe', install location is likely default.
Chocolatey installed 1/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Maven
Maven is used to build and manage java projects. You can learn more about it at https://maven.apache.org/
We will make use of choco again to install maven.
Open a new Command/PowerShell prompt as "Run as Administrator '', you can use the already opened common or PoweShell.
>choco install maven
Sample output:
Downloading Maven
from 'https://archive.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.zip'
Progress: 100% - Completed download of C:\Users\ca61707\AppData\Local\Temp\chocolatey\maven\3.6.0
\apache-maven-3.6.0-bin.zip (8.68 MB).
Download of apache-maven-3.6.0-bin.zip (8.68 MB) completed.
Hashes match.
Extracting C:\Users\ca61707\AppData\Local\Temp\chocolatey\maven\3.6.0\apache-maven-3.6.0-bin.zip to
C:\ProgramData\chocolatey\lib\maven…
C:\ProgramData\chocolatey\lib\maven
C:\Users\ca61707\.m2
Added C:\ProgramData\chocolatey\bin\mvn.exe shim pointed to '..\lib\maven\apache-maven-3.6.0\bin\mvn.
Cmd'.
Added C:\ProgramData\chocolatey\bin\mvnDebug.exe shim pointed to '..\lib\maven\apache-maven-3.6.0
\bin\mvndebug.cmd'.
Environment Vars (like PATH) have changed. Close/reopen your shell to
see the changes (or in powershell/cmd.exe just type `refreshenv`).
The install of maven was successful.
Software installed to 'C:\ProgramData\chocolatey\lib\maven'
Chocolatey installed 1/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Git
Github is used to manage, host, maintain and organize your code and projects. Learn more about it at https://github.com/
Open a new Command/PowerShell prompt as "Run as Administrator '', you can use the already opened common or PowerShell.
>choco install git.install
Sample output:
Installing 64-bit git.install…
git.install has been installed.
git.install installed to 'C:\Users\ca61707\AppData\Local\Programs\Git'
git.install can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
see the changes (or in powershell/cmd.exe just type `refreshenv`).
The install of git.install was successful.
Software installed to 'C:\Users\ca61707\AppData\Local\Programs\Git\'
Chocolatey installed 2/2 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Configure Environment Variables
Similar to .bash_profile on Mac, windows has environment variables used in various programs.
Now, let’s check how to setup the environment variables.
1. If you have Used Chocolatey (choco):
JAVA_HOME should have already been set by Chocolatey.
To verify JAVA_HOME, type the below command using PowerShell or command prompt
>echo %JAVA_HOME%
sample output:
C:\Program Files\Java\jdk1.8.0_ddd
JDK should have already been set in PATH by Chocolatey.
To verify PATH, type the below commands:
>echo %PATH%
sample output:
C:\Program Files\Java\jdk1.8.0_ddd\bin
To check java version, follow the command:
>java -version
sample output:
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
maven
M2_HOME too should have already been set by Chocolatey.
To verify M2_HOME, type on command prompt:
>echo %M2_HOME%
sample output:
C:\ProgramData\chocolatey\lib\maven\apache-maven-d.d.d
To check maven version, type
>mvn -version
sample output:
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T11:41:47-07:00)
Maven home: C:\ProgramData\chocolatey\lib\maven\apache-maven-3.6.0\bin\..
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_201\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
GIT should have already been set in PATH by Chocolatey.
To verify PATH, use the command:
>echo %PATH%
sample output:
C:\Users\ca61707\AppData\Local\Programs\Git\cmd
2. If you did not use Chocolatey (choco)
Follow the below based on windows version:
Windows 10 – Search for Environment Variables then select Edit the system environment
Windows 8 – Go to Control Panel > System > Advanced System Settings
Windows 7 – Right click My Computer and select Properties > Advanced
Next, click the Environment Variables button and under System Variables, click New.
Set JAVA_HOME:
In the Variable Name field, enter : JAVA_HOME
In the Variable Value field, enter : C:\Progra~1\Java\jdk1.8.0_ddd
Set M2_HOME:
In the Variable Name field, enter : M2_HOME
In the Variable Value field, enter : path to maven installation/extracted (example: C:\Users\caddddd\apachemaven-3.6.0)
Update PATH:
In System variables, find PATH/Path, click edit... button
It will prompt you to a dialog box, clicks on New button,
Add %JAVA_HOME%\bin
Add Git path (example: C:\Users\ca61707\AppData\Local\Programs\Git\cmd)
Comments
Post a Comment