Sebastien
Sebastien

Reputation: 55

How to install Java on a Mac

this is the current image in terminal

I have been doing some research on how to install Java jre and jdk on correct PATH. I lack the comprehension when coming to understand what others would instruct on to change path in the terminal. even after several trial. I would still get an error.

How can I properly install Java and have it run so I can run my Java IDE.

Upvotes: 2

Views: 14755

Answers (4)

freedev
freedev

Reputation: 30027

In 2022, you can use just brew

brew install openjdk

and maybe you need to update PATH env:

export PATH="/usr/local/opt/openjdk/bin:$PATH"

…and for the future give a try to sdkman, is better than brew

curl -s "https://get.sdkman.io" | bash

then open a new shell and try list to see what you could install ;-)

sdk list java 

At time of writing you could use:

sdk install java 17.0.1-tem

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 338201

Installer updates paths automatically

I have always found that after using an installer to install a Java implementation on macOS, the correct paths are already set for me. No additional configuration needed.

After the installer is done, in a terminal app run:

java --version

…to verify your newly installed Java implementation.

IDE uses its own JVM

You said:

have it run so I can run my Java IDE.

Be aware that some IDEs such as IntelliJ come bundled with their own Java implementation. So your downloaded JVM would not actually be running your IDE. This internal JVM is used to run the IDE itself.

This JVM could be used to run your app in development, as I recall. But you likely will want to download and install a JVM of a version (and possibly vendor) akin to what will be used in production.

Within your IDE settings, project settings, and build-tool settings (Maven, Gradle, etc.), you can tell the IDE what external JVM to use to run your app. Meanwhile the IDE continues to run itself using its own internally-bundled JVM.


Here is a flowchart I made to help explain the various implementations. All but the last two of these vendors likely provide an installer app for your Mac.

If you have no reason to choose a particular vendor, I suggest using AdoptOpenJDK, a cooperative effort supported by many in the Java community including most (if not all) of these vendors listed here.

For more info, read the important posting: Java Is Still Free.

Flowchart guiding you in choosing a vendor for a Java 11 implementation

Upvotes: 1

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78985

I have been doing some research on how to install Java jre and jdk on correct PATH.

JRE has been discontinued since Java 11. Since you are talking about Java 14, there is no JRE for it. You just need to install (and configure, if required) JDK.

Use the following command and then check java -version:

export JAVA_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home"
export PATH="$PATH:$JAVA_HOME/bin"

Note that it will work only for your current session i.e. java -version won't work in another terminal window.

To set JAVA_HOME permanently, do as follows:

$ cd ~
$ vi .bash_profile  

Write the following line into .bash_profile file, save and quit:

export JAVA_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home"
export PATH="$PATH:$JAVA_HOME/bin"

Then refresh (read and execute .bash_profile)

$ source .bash_profile

And finally test

$ echo $JAVA_HOME
$ java -version

Note: Also, This thread may be useful to you.

Upvotes: 3

antong
antong

Reputation: 387

The easiest way to determine what java installations you currently have installed is by running:

/usr/libexec/java_home

This will return the default Java jdk you currently have installed. You could also run:

java -v

Which will show you the current version of the first JDK that your path encounters.

If you would like to view all Java JDK versions that you have installed, you can run the first command mentioned with the -V (verbose) flag, and it will list the paths to all you have installed.

If you have multiple versions installed, you can amend your $PATH variable inside either .bashrc or .bash_profile. (Note these are the files for my system, macOS, but the profile files may be different for Linux installations, I simply do not know). There is a convention you can read about regarding where you should do it.

Quite simply, if the version you do NOT want is the one that appears when you run java -v or which java then you must add the path to the desired JDK to your $PATH as mentioned.

In order to view your current path, you can run echo $PATH

In order to amend your path you will add a command similar to the following to one of the bash profile files for your system:

export PATH=$PATH:/[NEW_PATH]

or

export PATH=[DESIRED_PATH]:$PATH

The first command adds to the end of your path, the second adds to the beginning.

Most likely you may want to add the path to your desired JDK in the beginning of your existing $PATH variable.

Obviously, be very careful with changing your path, you don't want other programs to break, so make sure you simply add the desired JDK path in front of your original variable, so that the only related program, java, has a change.

Upvotes: 0

Related Questions