"which java" in CentOS prints wrong java path - java

I am not sure why "which java" and "whereis java" paths are not correct. I tried to edit ~/.bash_profile and /etc/environment but did not help. The desired path is what is seen in "echo $JAVA_HOME" below but the same is not reflected in "which java"
Below is what I get in CentOS 6.4:
which java
/usr/bin/java
java -version
java version "1.7.0_45"
JAVA(TM) SE Runtime Environment (build 1.7.0_45-b18)
JAVA HotSpot (TM) 64-bit Server VM (build 24.45-b08, mixed mode)
whereis java
java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java
echo $JAVA_HOME
/usr/java/jdk1.7.0_45/jre => desired shows correct when echo $JAVA_HOME

Run alternatives --config java to pick the Java version you want to use as default. It will print out a list of installed Javas to choose from.
which java, however, will always print out /usr/bin/java. This doesn't mean it's set wrong! Observe:
$ ls -l `which java`
lrwxrwxrwx 1 root root 22 Oct 19 11:49 /usr/bin/java -> /etc/alternatives/java
$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 35 Oct 19 11:49 /etc/alternatives/java -> /usr/lib/jvm/jre-1.5.0-gcj/bin/java
If you use alternatives to change the path to IcedTea, ls -l /etc/alternatives/java will reflect that.

Your PATH (and nothing else) determines which directories to look for commands. This is the same in Linux, Solaris, and DOS.
When you do a which {command} it find the first directory you can execute the command in.
When you update your PATH in .bashrc, you have to source it again to change your current settings.

Sometimes alternatives does not work in a single command by selecting the desired version of java. I am not sure of the precise reason for this though..
I fell victim to such a scenario.
The auto-pilot failed, we must fly manual now..
In any of below two files in your unix installation add following variables and a call to a shell script (I have provided below) -
~/.bashrc
export JAVA_HOME=/opt/jdk1.8.0_141
export PATH=$JAVA_HOME/bin:$PATH
sudo bash /appl/common/toFixJava.sh
OR
/etc/profile
export JAVA_HOME=/opt/jdk1.8.0_141
export PATH=$JAVA_HOME/bin:$PATH
sudo bash /appl/common/toFixJava.sh
The script below referenced above sets the rest of java modules to utilize the java you want to use -
toFixJava.sh
altrs="java javac jre jarsigner javadoc javafxpackager javah javap java-rmi.cgi javaws jcmd jconsole jcontrol jdb jhat jinfo jmap jmc jmc.ini jps jrunscript jsadebugd jstack jstat jstatd jvisualvm keytool appletviewer apt ControlPanel extcheck idlj native2ascii orbd pack200 policytool rmic rmid rmiregistry schemagen serialver servertool tnameserv unpack200 wsgen wsimport xjc"
for each in $altrs
do
alternatives --install /usr/bin/$each $each /opt/jdk1.8.0_141/bin/$each <desired installation index number, e.g. 2>
alternatives --set $each /opt/jdk1.8.0_141/bin/$each
done
Please do let know if this doesn't work for you. I will help you solve alternatively!

Related

wsl: 'which java/whereis java' give wrong information

I have installed wsl2,and then installed jdk 17. Now I want to configure JAVA_HOME.
When I do:
$ which java
/usr/bin/java
$ whereis java
java: /usr/bin/java
But I don't see any java folder in /usr/bin.
Instead, I found java in /usr/lib/jvm:
$ pwd
/usr/lib/jvm
$ ls
java-1.17.0-openjdk-amd64 java-17-openjdk-amd64
Why so?
Because java isn't a folder, it's a binary, and probably a link to one of the binaries in jvm directory. You can check where the Java application points to by using ls -al /usr/bin/java
JAVA_HOME shouldn't point to the Java binary, but to one of the directories you've listed in the second example, so something like JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64.
I'd recommend having a look at jenv btw, it helps a lot in switching JDKs should you need it.

java -version looking at wrong location and not showing correct java version macOS

I guess I have messed up big time with my java installation and am now not able to correct this.
Ok, so first, /usr/libexec/java_home -V tells me
Matching Java Virtual Machines (1):
15, x86_64: "Java SE 15" /Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
Secondly, Java home is set up properly.
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
But when I do, java -version, it says
java -version
-bash: /opt/anaconda3/bin/java: No such file or directory
Don't know why it is going to /opt/anaconda3/ directory.
Another thing, I read that /usr/bin/java points to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
and if I want to use the new java version, I need to replace the /usr/bin/java symlink so that it points to /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java instead using,
sudo rm /usr/bin/java
sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin
But, apparently I cannot remove the symlink
rm: /usr/bin/java: Operation not permitted
What should be the approach now?

Why is "JAVA_HOME" path different from "which java" and "Whereis java"

In bash_profile the java_home is set as given below
export JAVA_HOME=$(/usr/libexec/java_home)
command "echo $JAVA_HOME" gives the below path
/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home
command "which java" gives the below path
/usr/bin/java
command Whereis java give the below path
/usr/bin/java
command "ls -la /usr/bin/java" gives the below link
lrwxr-xr-x 1 root wheel 74 Jan 15 2019 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
Why is that the java_home is different from the paths returned by "which java" and "Whereis java". Also why is /usr/bin/java linked to java in another location
Why is that the java_home is different from the paths returned by "which java" and "Whereis java". Also why is /usr/bin/java linked to java in another location
JAVA_HOME is an environment variable
Any command run on shell / command prompt would be looked up in $PATH
by default "/usr/bin/*" is in PATH
To get your java and javac in PATH you will need to use following
export PATH=$JAVA_HOME/bin:$PATH
This when put in your bashrc or bash_profile will always use java and its related executable binaries from your JAVA_HOME. Remember to put this before the existing path as shown above.
Hope this helps

Javac version doesn't update

I'm trying to compile a jar on my mac using 1.8 as a target release, but javac fails with the message "invalid target release".
When I run javac -version it gives me "javac 1.6.0_29".
I have the 1.8 jdk installed under "/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk", but even when I run /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/javac -version it gives me "javac 1.6.0_29".
Sym linking the CurrentJDK folder in "/System/Library/Frameworks/JavaVM.framework/Versions/" to the jdk folder in Library didn't change anything. Neither did sym linking the javac inside /usr/bin to the javac in my jdk folder.
Any ideas how to get javac to use my 1.8 version ?
Thank you very much!
UPDATE
My path variables output the following:
echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin:/opt/X11/bin:/usr/local/git/bin:/Developer/Tools/Panda3D
echo $JAVA_HOME (since I set it already in order to get javac to work)
/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/
Preprending /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/ to the $PATH variable didn't change the output for javac -version either.
UPDATE 2
Other question: What does javac do? Is it executing its code directly or depending on other binaries in the system? If so the link for them could be broken. Otherwise I wonder why executing the binary inside my 1.8 jdk folder gives me the version 1.6.
The proper Debian/Ubuntu way to configure which javac is pointed to by /usr/bin/javac is to use the update-alternatives command. You can do it interactively, and select from a list of available options:
sudo update-alternatives --config javac
and for java:
sudo update-alternatives --config java
In addition to what #Zakaria said, this command will show you all Java packages installed on the system, e.g.:
$ update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64
This one an overview of the Java tools and which version will be used, e.g.:
$ update-alternatives --get-selections | grep java
...
java manual /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
javac auto /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
javadoc auto /usr/lib/jvm/java-7-openjdk-amd64/bin/javadoc
...
As shown above here java would run from version 8, and javac from version 7.
If you want to update selections for all of the Java tools in one go try the following replacing the package name with one of your choice:
$ sudo update-java-alternatives -s java-1.8.0-openjdk-amd64
Verify that this has actually worked. If it didn't or partially did you have to resort to a semi-manual way, for example:
$ for i in $(update-alternatives --get-selections | grep java | awk '{system("basename "$3)}'); do sudo update-alternatives --config $i; done
where does the /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK link refer to?
Make sure it refers to the proper JDK
/Library/Java/JavaVirtualMachines/jdk{major}.{minor}.{macro[_update]}.jdk
Please check that both JAVA_HOME and PATH environment variables point to the 1.8 folder and have no link to the 1.6 java folder, especially the PATH.
Check:
echo %PATH%
Set:
export PATH="java8 folder":$PATH
or check this page: Set environment variables on Mac OS X Lion
Do the following steps
In the Windows Search bar, type "environment".
In your environment variables (account), create a variable called JAVA_HOME. Set it to C:\Program Files\Java\jdk1.8.0_65 (or wherever your JDK is).
In your system environment variables, edit PATH. Put this at the beginning of the PATH value: %JAVA_HOME%\bin;
Press OK.
Exit any command windows you are in.
Launch a command window. Type javac -version.
you should get this response: javac 1.8.0_65

JAVA_HOME directory in Linux

Is there any linux command I could use to find out JAVA_HOME directory? I've tried print out the environment variables ("env") but I can't find the directory.
On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))
On Mac you can run $(dirname $(readlink $(which javac)))/java_home
I'm not sure about windows but I imagine where javac would get you pretty close
Just another solution, this one's cross platform (uses java), and points you to the location of the jre.
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
Outputs all of java's current settings, and finds the one called java.home.
For windows, you can go with findstr instead of grep.
java -XshowSettings:properties -version 2>&1 | findstr "java.home"
echo $JAVA_HOME will print the value if it's set. However, if you didn't set it manually in your startup scripts, it probably isn't set.
If you try which java and it doesn't find anything, Java may not be installed on your machine, or at least isn't in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn't be used to determine if java is installed or not.
I know this is late, but this command searches the /usr/ directory to find java for you
sudo find /usr/ -name *jdk
Results to
/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk
FYI, if you are on a Mac, currently JAVA_HOME is located at
/System/Library/Frameworks/JavaVM.framework/Home
To show the value of an environment variable you use:
echo $VARIABLE
so in your case will be:
echo $JAVA_HOME
In case you don't have it setted, you can add in your .bashrc file:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
and it will dynamically change when you update your packages.
If $JAVA_HOME is defined in your environment...
$ echo $JAVA_HOME
$ # I am not lucky...
You can guess it from the classes that are loaded.
$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]
This method ensures you find the correct jdk/jre used in case there are multiple installations.
Or using strace:
$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
On the Terminal, type:
echo "$JAVA_HOME"
If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using "locate java" to try and discover where your installation of Java is located.
Did you set your JAVA_HOME
Korn and bash shells:export JAVA_HOME=jdk-install-dir
Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
C shell:setenv JAVA_HOME jdk-install-dir
Here's an improvement, grabbing just the directory to stdout:
java -XshowSettings:properties -version 2>&1 \
| sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'
You can check from the command line by executing this command echo $JAVA_HOME. If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives --config java which lists all installed versions with current active one marked and provides dialog to switch:
There are 3 programs which provide &apos;java&apos;.
Selection Command
-----------------------------------------------
1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java)
2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java)
*+ 3 /usr/java/jdk-17.0.2/bin/java
Enter to keep the current selection[+], or type selection number:
from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2
http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment
If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.

Categories

Resources