Unable to change the JAVA path to the Oracle JDK - java

I'm using RHEL8 which has default OpenJDK installed.
which java command points to /usr/bin/java.
java -version gives openjdk version "1.8.0_252"
Installed java in /u01/app/java/ location. Modified the .bashrc like below & sourced it.
export JAVA_HOME=/u01/app/java/jdk1.8.0_241/
PATH="$JAVA_HOME/bin/java:$HOME/.local/bin:$HOME/bin:$PATH"
export PATH
Now JAVA_HOME points to /u01/app/java/jdk1.8.0_241/ But which java or java -version still points to the OpenJDK.
Added the same in .bash_profile file & removed it from the .bashrc : Results are same
Deleted OpenJDK & made changes in .bash_profile. But by doing so ended up with "Command not found" error.
How can i fix it?

It was Path issue.
I have given PATH="$JAVA_HOME/bin/java:$HOME/.local/bin:$HOME/bin:$PATH" for PATH. which should not be the case.
Modified my PATH to below. It worked.
PATH="$JAVA_HOME/bin:$HOME/.local/bin:$HOME/bin:$PATH"
export PATH

That happens because when you invoke java it is actually invoked /usr/bin/java, which, issuing ls -l /usr/bin/java you will probably see is linked to the openjdk installation (probably through a double link: the first one being /usr/bin/java to /etc/alternatives/java and the second one being the link to the "real" java executable). For example, on my debian:
gianluca#asus-debian:~$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 mag 20 2018 /usr/bin/java -> /etc/alternatives/java
gianluca#asus-debian:~$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 43 nov 22 2019 /etc/alternatives/java -> /usr/lib/jvm/java-11-openjdk-amd64/bin/java
You can fix in two ways:
Dirty way, you manually update all the symbolic links in /usr/bin that lead to the unwanted java version and make them point to the desired one
Clean way, you use alternatives that does the job for you.
You can read more about alternatives for RHEL here (mid-page... "The longer version").
Good link

Related

how to set JAVA_HOME on my docker-ubuntu environment?

I'm trying ubuntu inside my mac docker environment for development. javac/gcc works fine, but when I try to use JNI to connect java and c program, I found a problem. I need to find the include file and libjvm.so file on my ubuntu docker image. But unfortunately I found the javac is actually:
root#4968756a1edd:~/mynet/mytest/build/jni# javac -version
javac 1.8.0_191
root#4968756a1edd:~/mynet/mytest/build/jni# ls -lrt `which javac`
lrwxrwxrwx 1 root root 23 Feb 6 07:38 /usr/bin/javac ->
/etc/alternatives/javac
Well, in an ubuntu/centos VM installation, usually there's a directory for a jdk and everything (bin/lib/include) is under this directory. But in docker image, how can I find this jdk installation location and all those headers/libraries?
In bash you can set env variables like this:
sudo -H gedit /etc/environment
JAVA_HOME=/path/to/java/folder

why is java binary in two locations in the JDK?

On Windows I noticed that the java binary is in two different locations (I have the JDK installed):
C:\Program Files\Java\jdk1.8.0_121\bin\java.exe
C:\Program Files\Java\jdk1.8.0_121\jre\bin\java.exe
Similarly on a Unix box that I have access to, there are two copies, one in jre/bin and one just in bin. I am not sure if this other one is the JDK or just a JRE installation.
In both cases, they are the same size as each other. I confirmed they aren't symlinked on the Linux side although I suppose they could be hard linked.
The contents of jdk/bin/ is the Java binaries that are included with the Java Developers Kit. The Java binaries inside of jdk/jre/bin are the Java Runtime Environment's binaries.
If you have the JDK version of Java then use the one in jdk/bin/.
But they are the same
If you look at the executables there is actually no difference in them, so it really doesn't matter.
$ pwd
/usr/java/jdk1.7.0_45
$ ll jre/bin/java
-rwxr-xr-x. 1 root root 7718 Oct 8 2013 jre/bin/java
[saml#greeneggs jdk1.7.0_45]$ ls -l bin/java
-rwxr-xr-x. 1 root root 7718 Oct 8 2013 bin/java
$ cmp jre/bin/java bin/java
$

JDK 9 + Mac OS + jlink?

I installed the release version of JDK 9 on Mac OS.
jshell works great, Jigsaw module support works, but there is no jlink:
➜ java --version
java 9
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
This comes up empty:
find /System/Library/Frameworks/JavaVM.framework/Versions/Current/ -iname jlink\*
FYI:
➜ ls -l $(which java)
lrwxr-xr-x 1 root wheel 74 Nov 7 2016 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
You can verify your JAVA_HOME using which java and make sure it points to the default installation path which ideally should be
/Library/Java/JavaVirtualMachines...
[for e.g. I use it as export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/]
and further you can find the jlink in the bin folder of Contents
find /Library/Java/JavaVirtualMachines/jdk-9.jdk -iname jlink\*
which should return
/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/bin
Attaching a screenshot for reference of the location its installed:-
Note: Though in the screenshot, the command doesn't run successfully but its recognized.
To add the JDK 9 tools to your path, add the following to the file .bashrc of your home directory:
export JAVA_HOME=$(/usr/libexec/java_home -v 9)
export PATH="$JAVA_HOME/bin:$PATH"
Did you notice the -v 9? you can change that to 1.8 if you ever want to switch back to JDK 1.8. For any newbie who can’t locate .bashrc in the Finder: press ⌘⇧. (command shift dot) to reveal hidden files.
There's some important context here: Understanding Oracle's Java on Mac
The standard JDK utilities (java, javac, etc) are actually installed at /Library/Java/JavaVirtualMachines/(JDK_VERSION)/Contents/Home/bin.
/usr/bin is in the path, and in that directory you'll find all the usual JDK utilities. When you type java (or any other command), those are the ones it finds, but they are actually symlinks to executables with the same names in /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands. Those executables are just wrappers that interrogate your JAVA_HOME environment variable (which should point to the actual install location) and invoke the real binaries found there. I'm unclear on why anyone thought these two layers of abstraction were necessary, but it is what it is.
At some point after the release of Java 9, symlinks and wrappers for jshell were added to macOS, but it appears that no such thing was done for jlink.
To keep things working consistently going forward, I'd recommend writing an equivalent wrapper, putting it in the right location, and added a symlink to it in /usr/bin.
Unfortunately, since the wrapper scripts are under /System, you cannot create or modify anything, even as root, until you disable System Integrity Protection. This takes a few minutes and involves a few reboots, but it's easy to do:
Reboot your machine. While it reboots, hold down ⌘R. This will cause the machine to start up in Recovery Mode. You can release the keys when you see the progress bar.
Once in Recovery Mode, choose Terminal from the Utilities menu.
At the prompt, type csr disable. You'll be prompted to reboot to cause the changes to take place. Do that as well.
Once you're back in regular mode, open Terminal and do the following:
% sudo vi /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink
Password:
Enter your password at the prompt.
In vi, switch to Insert mode by pressing I, then enter the following text:
#!/bin/bash
$JAVA_HOME/bin/jlink $#
Then exit Insert mode by pressing esc, and save and exit by typing :wq.
Issue the following commands to make the script executable, create the symlink, make the symlink executable, and check your work:
% sudo chmod +x /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink
% sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink /usr/bin/jlink
% sudo chmod +x /usr/bin/jlink
% ls -la $(which jlink)
lrwxr-xr-x 1 root wheel 75B Jun 19 10:33 /usr/bin/jlink# -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink
% ls -la $(which java)
lrwxr-xr-x 1 root wheel 74B Sep 25 2017 /usr/bin/java# -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
% jlink --version
10.0.1
%
Now it's time to re-enable System Integrity Protection. Reboot back into Recovery Mode (instructions in step 1).
Once in Recovery Mode, choose Terminal from the Utilities menu.
At the prompt, type csr enable. You'll be prompted to reboot once more.
If you'd rather not muck with disabling/re-enabling SIP, you can just create the script at /usr/bin/jlink.
Hopefully some future version of macOS will include these by default.

Maven with Ant wrapper error - main class path

I'm attempting to compile my companies codebase using Maven which is wrapped in an Ant launcher for backwards compatibility reasons. I am running Oracle Linux 6.6, Java 1.7 u79-b14, Ant 1.7.1 and Maven 3.0.5 which are versions dictated to me by the enterprise infrastructure team.
When I set JAVA_HOME I get the following error when executing Ant Error: Could not find or load main class org.apache.tools.ant.launch.Launcher but if JAVA_HOME is commented out then Ant seems to run just fine.
#~/.bashrc
export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
export MAVEN_HOME=/usr/share/maven
export M2_HOME=/usr/share/maven
export MAVEN_OPTS="-Xmx1024m"
export BUILD_CHROOT="/home/builduser/builds"
Included to help is the location at which Java is installed.
[builduser#iaas-a-jen03 ~]$ which java
/usr/bin/java
[builduser#iaas-a-jen03 ~]$ ls -la /usr/bin/java
lrwxrwxrwx 1 root root 22 Jun 30 11:58 /usr/bin/java -> /etc/alternatives/java
[builduser#iaas-a-jen03 ~]$ ls -la /etc/alternatives/java
lrwxrwxrwx 1 root root 46 Jun 30 11:58 /etc/alternatives/java -> /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
The issue was resolved by following the steps found in this blog post... http://pkolomeitsev.blogspot.co.uk/2015/01/apache-ant-error-could-not-find-or-load.html
ant --execdebug was the key to creating the right folders. Then I just had to make sure that java-1.7.0-openjdk-devel.x86_46 was installed and voila!

Where is the Java SDK folder in my computer? Ubuntu 12.04

I know it's installed because when I type:
$java -version
I get:
OpenJDK Runtime Environment (IcedTea6 1.12.5) (6b27-1.12.5-0ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
And when I type:
$locate jdk
I get:
/home/arturo/Documents/2012a/sys/java/jre/glnxa64/jre/lib/servicetag/jdk_header.png
/usr/share/app-install/desktop/openjdk-6-jre:openjdk-6-java.desktop
/usr/share/app-install/desktop/openjdk-7-jre:openjdk-7-java.desktop
/usr/share/app-install/icons/openjdk-6.png
/usr/share/app-install/icons/openjdk-7.png
What worries me about the first item in the list is that the 2012a folder is my MATLAB folder and not a standard 'usr/lib' folder. I'm really confused on where the JDK and JRE got installed, because I need to set the $JAVA_HOME path pointing to the folder. Where am I missing something?
WAY-1 : Updated for the shortest and easy way
Below command will give you the path, But it will only work if java command is working in other words if java path is configured.
readlink -f $(which java)
Read more at Where can I find the Java SDK in Linux?
WAY-2 (Better than WAY-1) : Below answer is still working and try it if above command is not working
for you.
You need to dig into symbolic links. Below is steps to get Java directory
Step 1:
$ whereis java
java: /usr/bin/java /etc/java /usr/share/java
That tells the command java resides in /usr/bin/java.
Dig again:
Step 2:
$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 2009-01-15 18:34 /usr/bin/java -> /etc/alternatives/java
So, now we know that /usr/bin/java is actually a symbolic link to /etc/alternatives/java.
Dig deeper using the same method above:
Step 3:
$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 31 2009-01-15 18:34 /etc/alternatives/java -> /usr/local/jre1.6.0_07/bin/java
So, thats the actual location of java: /usr/local/jre.....
You could still dig deeper to find other symbolic links.
Reference : where is java's home dir?
Please use this command:
readlink -f $(which java)
It works for me with Ubuntu gnome.
On my computer the result is:
/usr/lib/jvm/java-7-oracle/jre/bin/java
Regards.
In generally, java gets installed at /usr/lib/jvm . That is where my sun jdk is installed. check if it is same for open jdk also.
$whereis java
java: /usr/bin/java /usr/bin/X11/java /usr/share/java /usr/share/man/man1/java.1.gz
$cd /usr/bin
$ls -l java
lrwxrwxrwx 1 root root 22 Apr 15 2014 java -> /etc/alternatives/java
$ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 39 Apr 15 2014 /etc/alternatives/java -> /usr/lib/jvm/java-7-oracle/jre/bin/java
So,JDK's real location is /usr/lib/jvm/java-7-oracle/
Location of JRE in Ubuntu:
/usr/lib/jvm/java-7-oracle/jre
$ whereis java
java: /usr/bin/java /usr/lib/java /usr/bin/X11/java /usr/share/java /usr/share/man/man1/java.1.gz
On Ubuntu 14.04, it is in /usr/lib/jvm/default-java.
For me, on Ubuntu, the various versions of JDK were in /usr/lib/jvm.
I found the solution to this with path name:
/usr/lib/jvm/java-8-oracle
I'm on mint 18.1
I am using Ubuntu 18.04.1 LTS. In my case I had to open the file:
/home/[username]/netbeans-8.2/etc/netbeans.conf
And change the jdk location to:
netbeans_jdkhome="/opt/jdk/jdk1.8.0_152"
Then saved the file and re-run Netbeans. It worked for me.
you can simply write the following command in the terminal of your linux system and get the java path :- echo $JAVA_HOME
The easiest way to do so is by typing echo $JAVA_HOME on your terminal.

Categories