Installing multiple java files to be used simultaneously - java

After installing two versions of java in rhel how do I point my program to one of those specific java file?
The java files that were installed were:
java-1.7.0-oracle-src-1.7.0.79-1jpp.1.el7_1.x86_64
java-1.8.0-oracle-javafx-1.8.0.45-1jpp.2.el7_1.x86_64
java-1.8.0-oracle-devel-1.8.0.45-1jpp.2.el7_1.x86_64
java-1.8.0-oracle-src-1.8.0.45-1jpp.2.el7_1.x86_64
java-1.7.0-oracle-javafx-1.7.0.79-1jpp.1.el7_1.x86_64
java-1.8.0-oracle-jdbc-1.8.0.45-1jpp.2.el7_1.x86_64
java-1.7.0-oracle-devel-1.7.0.79-1jpp.1.el7_1.x86_64
java-1.7.0-oracle-jdbc-1.7.0.79-1jpp.1.el7_1.x86_64
java-1.8.0-oracle-plugin-1.8.0.45-1jpp.2.el7_1.x86_64
java-1.7.0-oracle-1.7.0.79-1jpp.1.el7_1.x86_64
java-1.8.0-oracle-1.8.0.45-1jpp.2.el7_1.x86_64
java-1.7.0-oracle-plugin-1.7.0.79-1jpp.1.el7_1.x86_64

Do you mean choosing what version of Java to use?
Some Linux distributions rely on update-alternatives tool to control what specific binary to use when multiple versions of it are installed. It works by setting symbolic links. For instance in my system, after installing openjdk, a symbolic link at:
/usr/bin/java
was created linked to the real java binary at:
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
If update-alternatives is installed in your system, and the installation of Java updated its status correctly, you can choose the version of Java interactively by doing:
sudo update-alternatives --config java
Another approach: simply set JAVA_HOME and/or PATH environment variables right before you execute your command to switch between one or other Java version.

Related

macOS - How to install Java 17

Could someone please let me know the steps to install Java on a Mac.
I did brew install java
I get this
Warning: openjdk 17.0.1 is already installed and up-to-date.
To reinstall 17.0.1, run:
brew reinstall openjdk
If I do java -version, I get this.
openjdk version "13.0.8" 2021-07-20
If I have navigate to /Library/Java, I have 2 empty directories.
Where is java 17 installed??
In 2023, even if you can use just brew..
brew install openjdk#17
Java will be installed here:
/usr/local/opt/openjdk#17/bin/java
For the system Java wrappers to find this JDK, symlink it with:
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
...give a try to sdkman, it's far 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.4.1-tem
Java will be installed here:
/Users/freedev/.sdkman/candidates/java/17.0.4.1-tem
Java doesn't mind if you install multiple versions. This is often required; java is not backwards compatible (it tries to change little, but e.g. the java8 to java9 transition broke a ton of stuff, much of it needless and much of it not reasonably expectable or fixable by libraries and apps, so a bunch of java apps and libraries only run on java8 - just an example).
So, yes, you have installed JDK17. Also, yes, if you just run java without specifying which one you want, you so happen to get java13 here.
To see all installed javas, you can run:
/usr/libexec/java_home -V
to 'override', you can use something like (depends on which shell you're using on your mac):
export JAVA_HOME=`/usr/libexec/java_home -v 17`
(the backticks mean: Run this then take the output of it and treat that as the 'value' of the expression. here, assign it to the JAVA_HOME env var. -v 17 requests a path to java 17. The -V option lists all and is meant for your eyeballs, not for scripts. The -v option is mostly for scripting, and that's how we're using it here).
JAVA_HOME decides which java is used by some things, but the java you get when you just type java is /usr/bin/java, and that executable is actually just a wrapper that picks a java to run from amongst all installed versions. It uses JAVA_HOME to decide which java to actually run. There are wrappers for all the common commands (javac, too). You can always run e.g. which javac to see what that actually runs; you probably see /usr/bin/javac. Everything in /usr/bin is one of these wrapper thingies that looks at JAVA_HOME and then runs the binary it finds there.
To specify version 17
brew install openjdk#17
Later I add create a link:
sudo ln -sfn /usr/local/opt/openjdk#17/libexec/openjdk.jdk \
/Library/Java/JavaVirtualMachines/openjdk-17.jdk
And use jenv to control which java version to use
To used the version installed by homebrew rather than the one installed by the OS you can get detailed information from homebrew by typing
brew info java
Currently it states
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides similar software and installing this software in
parallel can cause all kinds of trouble.
If you need to have openjdk first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc
For compilers to find openjdk you may need to set:
export CPPFLAGS="-I/opt/homebrew/opt/openjdk/include"
For Homebrew, the package that installs the new AdoptOpenJDK is named as temurin. So you need to use below command:
brew install --cask temurin17
If you get any error like temurin cask not available then update brew using below commands:
brew update
brew tap homebrew/cask-versions
To switch quickly between different versions of java add the entries in bashrc as per your jdk versions like:
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j17="export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version"
Steps to install
You asked:
let me know the steps to install Java on a Mac.
Download an installer free-of-cost from vendors such as Adoptium, Azul Systems, Bellsoft, Amazon, Oracle, Microsoft, SAP, and others.
Run installer app.
Quit the installer app when done.
Verify installation by typing on a command-line in Terminal.app:java --version
Delete installer app that you downloaded.
Configure your IDE to use that new Java implementation you installed.
Java location
You asked:
Where is java 17 installed??
/Library/Java/JavaVirtualMachines/
In the Finder, choose Go > Go to Folder, and paste /Library/Java/JavaVirtualMachines/.
Note that this is not the Library folder within your home folder. We are not referring to /Users/your_user_name/Library/…. We are referring to the root Library folder that applies across all the user accounts on this Mac.
You said:
I did brew install java
No need for the Homebrew package manager. If you already enjoy using the brew tool, proceed. But if new to Homebrew, skip it if your only goal is to install Java. Just use an installer for Java as you would for many Mac apps.
JavaFX
You added a tag for javafx.
Be aware that for JavaFX, you have two options:
Include the necessary OpenJFX libraries within your development project and within your final app, or …
Use a JDK that includes the JavaFX/OpenJFX libraries.
At least two vendors provide JDK installers that include the JavaFX/OpenJFX libraries:
Azul Systems (ZuluFX)
Bellsoft (LibericaFX)
This answer is specifically if you use Intellij on a Mac
Within IntelliJ, you can use the IDE to add new JDKs of selected versions from common vendors. Instructions for this are here:
Setup the project JDK
When setting up the JDK, you can either:
Select a pre-existing JDK which has been registered with the IDE OR
Use the Add JDK option to add a new JDK which you previously downloaded and installed using the method outlined in Basil's answer OR
Use the Download JDK option to choose a vendor and version of the JDK that the IDE will automatically download, install and make available for selection.
The JDKs installed by Idea will be located in the same location as outlined in Basil's answer for a manual install /Library/Java/JavaVirtualMachines/.
One common issue is that the version of the JDK registered for the project differs from the default version used in the terminal. This can sometimes mean that the app works when run in Idea, then fails when run in the terminal (or, at least that it is executed against a version of the JDK you didn't expect).
To select the version of the JDK to run in the terminal, configure the Java home setting as outlined in rzwitserloot's answer.
export JAVA_HOME=`/usr/libexec/java_home -v 17`
Also, some tools such as the openjfx maven plugin will not use the java version selected in Idea when executing a call to a JDK tool like jlink, but will instead have their own mechanism for finding a JDK to use (e.g. look at JAVA_HOME or use the Maven toolchains plugin). So it is always good to check the JAVA_HOME variable and ensure that it is set to a reasonable value, both for terminal execution and for effective use of Java development tools that may rely on it.
I think that answers here are not fully out of topic, but from my point of view, my case is exactly the same as that of the author. I had already installed java 8, 11, and 13. All of them resides at:
/Library/Java/JavaVirtualMachines/
Nevertheless when I tried to find the path of JDK 17 it resides:
/usr/local/Cellar/openjdk
I use Mac OS Big Sur and the JDK was installed with homebrew
Brew now supports searching old formulae and allowing you to install a specific version. I'm using Homebrew 3.5.2-117-gb941470
Create a local tap: brew tap-new --no-git local/openjdk
Ask Brew to find the formulae of the version you want: brew extract --version 17.0.2 openjdk local/openjdk
Install Java 17 using your new local tap: brew install openjdk#17.0.2
Link the JDK into the MacOS JVM Dir: sudo ln -sfn /usr/local/opt/openjdk#17.0.2/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk17.0.2.jdk
Check Java 17 is available: /usr/libexec/java_home -V
Set Java Home: export JAVA_HOME=$(/usr/libexec/java_home -v17)
Check current version of Java is correct...
$ java --version
openjdk 17.0.2 2022-01-18
The following method installs Java without the need for any additional tools or package managers.
Go to https://jdk.java.net/17/ and download the latest macOS archive.
Download the latest Adoptium release for the Java 17 branch by going to https://adoptium.net/temurin/releases/ and be sure to select the tar.gz version.
The archive is either for x64 (Intel CPU) or for AArch64 (Apple Silicon / M1 CPU).
Then, open a Terminal, and extract the downloaded archive to the system path for Java virtual machines:
cd Downloads
tar xzf OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.5_8.tar.gz -C /Library/Java/JavaVirtualMachines

get java installation path using shell script in rhel

I have to automate a process in which i need to install java on RHEL 7, using rpm package and then update the security jars later on the installed location. Once i execute the java rpm, how would i get the correct installed path? when i execute which java it says /bin/java. However, i need the installation location using shell script.
Here, the actual installation happened at /usr/java/jdk1.7.0_55/jre. I want to get this location. Can some one suggest me how to retrieve this?
The readlink program (part of coreutils, and available on any RHEL version) can resolve symbolic links:
foo=$(readlink -f $(which java))
echo $foo
(You may also have realpath, but perhaps not).
Java RPM packages from Oracle support the 'alternatives' system, which provides the detail you are seeking. For example, after installing a recent JRE, 'alternatives' reports like so:
[user#host ~]$ alternatives --display java
java - status is auto.
link currently points to /usr/java/jre1.8.0_60/bin/java
etc...
The output is relatively friendly for scripting.

Confusion about Java Versions

So I'm trying to get the latest version of Java. When I run:
java -version
I get:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
When I run:
javac -version
I get:
javac 1.6.0_65
Now I've just downloaded and installed JDK 8. When I go into System Preferences --> Java --> Update, I see:
Your system has the recommended vesion of Java.
Java 8 Update 05.
I guess I have a few questions:
1) Don't I want the JDK and my version of Java to match up?
2) Why does my Java Control Panel claim I have Java 8, but my work in the terminal (when checking my Java version) says otherwise?
Thanks for the help,
Mariogs
The probably "simple" answer is you have two versions of java installed. On the command line you currently use 1.6. Thus the old one is active. In system preferences you see the version of java 8.
Windows:
You can change the version of the command line to java 8. Set PATH environment variable and JAVA_HOME or via windows preferences. I recommend the first one.
Mac: /usr/libexec/java_home is the starting point for switching java versions on the command line. Check out this post to understand how to handle different java versions on the Mac. IMHO this answer is a good solution.
The Java Platform offers both the JRE and the JDK in order for users to run Java programs. The JRE stands for the Java Runtime Environment, and the JDK stands for the Java Development Kit.
The JDK is meant for Java developers - that is, those who build applications/write programs in Java. It contains tools that are needed for Java coding, including -javac to compile programs.
The JRE is meant for regular users - those who only need to run Java programs on their computer and are not interested in development.
The reason for the discrepancy in your case is because you're looking at the JRE and JDK and trying to compare the two. The current JDK that you have is Java 8, whereas the current JRE that you have is 1.6.0_65. It is problematic that your JRE version does not match your JDK version, but without your PATH variable or other information about your install, we can't help you fix your installation.
1) Yes, if you use the JDK at all, you want the JRE (runtime environment) to come from the JDK (development environment) (a JDK necessarily includes a JRE).
2) Likely your path variable is set so that you invoke Java from your Java 6 installation; you need to find the equivalent for your Java 8 installation and set the path for that. Without information about your operating system, we can't help you do that.
We should know the reason for this
Our OS comes with a predefined (built-in)set of tools and utilities. When we try to execute the command e.g. cls in the Windows command line then it is already present in system path variable and os will refer the corresponding binary of cls to execute the command.
However, when we install any third party tool/software then path variable is not updated accordingly.
When we install different versions of java on your system then installations go to different directories. E.g. JDK installation directory for Windows will be
C:\Program Files\Java\jdk1.8.0_161
Similarly, JRE installation directory for Windows will be JDK installation directory for Windows will be
C:\Program Files\Java\jre1.8.0_161
We need to update the path variable of OS to point to the appropriate directory. If we set the path of JDK then it will execute a binary from JDK bin directory.
Solution
we need to update JDK or JRE version specific directory location into PATH Environment variable.
Let me see if I can clear it up for you.
1)Yes, arguably you nearly want this to be true.
2)It could be few things, but most likely that a previous instillation was not properly removed. So one gets called instead of the other.

I had Open JDK 1.7 on CentOS; I installed Oracle's Java rpm; Oracle Java doesn't seem to exist

I started off with CentOS and OpenJDK 1.7
# java -version
java version "1.7.0_25"
OpenJDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
In order to run a specific application, I want to use Oracle's Java 1.6, provided from an RPM.
I copied the Oracle binary to a specific new directory:
# pwd
/oracleJava/jdk-6u45-linux-x64-rpm
I extracted the binary and it gave me the following files:
# ls
jdk-6u45-linux-amd64.rpm
sun-javadb-core-10.6.2-1.1.i386.rpm sun-javadb-javadoc-10.6.2-1.1.i386.rpm
sun-javadb-client-10.6.2-1.1.i386.rpm sun-javadb-demo-10.6.2-1.1.i386.rpm
sun-javadb-common-10.6.2-1.1.i386.rpm sun-javadb-docs-10.6.2-1.1.i386.rpm
I installed the RPM and the rpm utility believes that it installed properly:
rpm -q jdk
jdk-1.6.0_45-fcs.x86_64
# rpm -Uvh ./*.rpm
Preparing... ########################################### [100%]
package jdk-2000:1.6.0_45-fcs.x86_64 is already installed
# rpm -Uvh sun-javadb-*.rpm
[I omit the feedback because it generates a formatting error]
#
However, the Java version just shows 1.7
# java -version
java version "1.7.0_25"
OpenJDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
In other words, I was expecting the Oracle files to give me some new /java directory somewhere, with a new java executable that would return a different answer for "java -version"
I need that new directory so that I can set JAVA_HOME and use the 1.6 version of Java.
Helpful suggestions would be greatly appreciated. Thanks in advance.
The Oracle JDK RPMs are horrible.
They do not register with the alternatives system.
They do not Provide (in RPM terms) "java"
They have messed up their RPM 'version string' and rely on Epoch (...)
All versions of the JDK (i.e. 1.6 vs 1.7) have the same Epoch
In order to quickly remedy your problem you can run the following:
/usr/sbin/alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000
It will register and prefer the Oracle java installation as an alternative. OpenJDK has weight 16000; here we register with 20000. Once you've run this command you can switch between java versions by using the (already mentioned) alternatives --config java command.
As for a less quick fix you can use my virtual java package. It's quite possibly not perfect (I'm open for improvements ;) ), but it Provides java (making my apache-tomcat package happy) and registers with the alternatives system. This virtual package simply depends on jdk...you can find it here: https://github.com/keystep/virtual-java-rpm
Run the following command to see if your JVM is getting listed.
sudo update-alternatives --config java
If your JVM gets listed select it.
The problem is that Oracle's RPMs are (wilfully IMO!) ignorant of the "alternatives" system.
Instead, they install stuff in a non-standard place (/usr/java) and then expect the user / system administrator to mess with the PATH variable ... by hand.
It is a nuisance!
So what has probably happened in your case is that your JDK / JRE has been installed in a subdirectory of /usr/java ... but since you haven't added the relevant bin subdirectory to the front of your PATH you are still picking up the OpenJDK tools via the java command name.
You can fix this after the fact by using "alternatives" to configure and then select the Oracle commands. But it is messy, especially if you want the JDK tools as well. (There are a lot of them!)
Please check that whether your JAVA_HOME points to JDK 6 using echo $JAVA_HOME. In order change your JAVA_VERSION to Java 6, you need to point to the Java development KIT 6. You also need to add the bin directory to the $PATH variable. Please ensure that JDK6 bin directory comes in the $PATH prior to other JDK bin,if any. You can check the version of your Java in the environment by command java -version.The other answer by learningloop is very perfect to switch between different Java configurations
Try to the following method
delete OpenJDK folder from
/usr/lib/jvm
I had same problem and got solution through this method

JVM version manager

Is there Ruby Version Manager equivalent for the Java world?
I'm looking for tool which allow me to easily download and install a new JVMs and switch between them. For example:
jvm install <version>
jvm list //will list installed JVMs on my system
jvm use jdk1.6 //will switch my env to jdk 1.6 version, etc.
http://www.jenv.be/ will allow this type of control.
SDKMAN! is a similar tool for the Java ecosystem. Supports various Java versions, Scala, Clojure, Kotlin, Groovy, and build tools like Maven and Gradle.
Works on Mac and Linux, with some mentions of support for Windows depending on how hard you are willing to try :)
If you use Ubuntu you can specify which JVM you want to use via command (works only for JVM installed from apt-get or aptitude)
sudo update-alternatives --config java
Or by setting JAVA_HOME. Here is good tutorial:
http://vietpad.sourceforge.net/javaonlinux.html
For the sake of completeness, there are two more - jabba (of which I am the author; written in Go and designed after nvm/gvm/rvm) and jenv (not to confuse with jenv.be; doesn't support installation from oracle but can install from a custom zip).
With JVMs, if you need to switch between them you just need to use a batch file (or powershell script) to manage the classpath and JVM path. You don't need to rely on the system default JVM path and instead just allow your app to point to whatever JVM you like by changing classpath and JVM path environment in the shell that runs the JVM.
For programs that are getting Java location from the Registry, in theory you could use a batch script to update that also.
In this respect Java is way easier than "Ruby version manager".
As it is not (yet) in the list of possibilities, there's also asdf.
asdf does not only provide version management for java, it has plugins for ~400 different languages and tools by default, you can find more on github, or create your own.
Here is an example how to setup a new install (you can also install completion so you don't have to list the versions first). The java plugin is added, a specific version (there are versions for adoptopenjdk, corretto, dragonwell, graalvm, liberica, mandrel, microsoft, openjdk, oracle, sapmachine, semeru, temurin, trava, zulu) is installed and configured to be the global (or local version) to use:
asdf plugin-add java # Add java Plugin
asdf list-all java # List all available java versions
asdf install java openjdk-18 # Install specific jdk version
asdf install java openjdk-17 # Install another jdk version
asdf global java openjdk-18 # Set the global jdk version
asdf local java openjdk-17 # Set the local version for calls from the current directory
asdf uses a file in $HOME/.tool-versions to configure the global selected version. If you call any tool in a directory that has a .tool-versions file with a different version, that one is used (defined with asdf local …).
The trick is to use update-java-alternatives (from the java-common package). The update-alternatives command will not update every one of the symbolic links for various java /bin executables, which is why update-java-alternatives is better.
So to go for OpenJDK 6 to 7, use update-java-alternatives -l to get a list of Java alternatives and then used sudo update-java-alternatives -s java-1.7.0-openjdk-amd64 to switch the JDK.
CAVEAT: The command above might throw the following errors,
update-alternatives: error: no alternatives for mozilla-javaplugin.so.
update-java-alternatives: plugin alternative does not exist:
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so
This is because the openjdk plugin is not installed by default. To fix run sudo apt-get install icedtea-7-plugin and rerun update-java-alternatives.

Categories