Even if $JRE_HOME is not set java -version is displaying properly - java

I used sudo apt-get open-jdk7 to install Java on my Ubuntu OS.
When I open a terminal and type echo $JRE_HOME it displays nothing. But then I do java -version it shows me correct version of OpenJDK. Also when I simply type java it works.
Not sure how Ubuntu set the path of java.
Similarly javac works but I don't see anything when I say echo $JAVA_HOME.
My path does not show java directory path in it.
Where is Ubuntu setting the java path? Why does Ubuntu not set JAVA_HOME and JRE_HOME during installation of OpenJDK?

It's a common misconception that Java somehow requires JAVA_HOME, JRE_HOME or similar environment variables to be set to work.
In fact the java and javac (and related) executables don't really care about those variables. They only need to be on the PATH (or can be executed via a full path if not on PATH) and will find their related install by looking "near itself" (i.e. the executable are usually installed in a known location relative to the JVM files).
There are a few third party scripts that use JAVA_HOME and/or JRE_HOME to find a Java installation (Apache Tomcat is a well-known example for this), but the Java executables themselves don't need those variables.
As to how Ubuntu finds the executables is easy to answer: use which java and which javac to find out which executables are executed when you type those commands. On Debian-based systems they are usually a series of symlinks through the alternatives systems that point to the actual executables.

"Where is ubuntu setting java path?", ubuntu does not set the open-jdk-java-path automatically.
try
find / -name "java"
to locate the javac. And write
export JRE_HOME="xxx"
(where xxx is the base of the open-jdk-jre.)
BTW: You got nothing than trouble with open-jdk. You better use the sun-jdk.

As many Linux distros, Ubuntu has /etc/alternatives in its paths. If you type:
which java
Then you'll get
/etc/alternatives/java
If you type:
ls -l /etc/alternatives/java
Then you'll get
/etc/alternatives/java -> /usr/lib/jvm/<your java version>
A few mor ls -l -s can help you to get the full picture.

Related

Windows Subsystem for Linux not recognizing JAVA_HOME Environmental Variable

I'm trying to get WSL to recognize my windows installed environmental variable of JAVA_HOME. I attached of what I have in my bashrc and what I have in my windows environmental variables along with outputs from cmd and bash.
What's at the end of my bashrc:
export JAVA_HOME="/mnt/d/Program Files/Java/jdk-11.0.1"
export PATH="/mnt/d/Program Files/Java/jdk-11.0.1/bin:$PATH"
CMD INPUT/OUTPUT:
C:\Users\jaall>javac --version
javac 11.0.1
BASH INPUT/OUTPUT:
myubuntu_name#DESKTOP-LUK3BII:~$ javac --version
Command 'javac' not found, but can be installed with:
sudo apt install default-jdk
sudo apt install openjdk-11-jdk-headless
sudo apt install ecj
sudo apt install openjdk-8-jdk-headless
I've been stuck on this for awhile and can't figure it out or find a working solution online. Thanks!
As Biswapriyo suggested, you should use WSLENV.
Open PowerShell. Then set JAVA_HOME to the path to your java installation.
In your case, run setx JAVA_HOME "D:\Program Files\Java\jdk-11.0.1"
You should see a message that says "SUCCESS: Specified value was saved".
Then run setx WSLENV "JAVA_HOME/p".
You should see the success message again.
Type 'env' into your WSL bash prompt.
You should see JAVA_HOME correctly set at this point.
Note: If step 2 doesn't work, you might want to changing the path to JAVA_HOME to include the \bin folder.
TL;DR: In WSL, you must use javac.exe since it is a Windows binary. Simply typing javac will not work, even if the path is set up correctly. If that doesn't work, try adding ../bin to the end of your JAVA_HOME variable.
Using Windows Binaries & Environment Variables in WSL
There's a much easier way to make Windows and WSL utilize the same JavaSDK binary, you just need to set up a few things first. Best of all, if you have JavaSDK installed on Windows, you do not need to install Linux binaries.
Check WSL Permissions and Directory Link (Optional, but recommended)
In WSL, list symbolic links on PC:
ls -l /mnt
If any drive is owned by root, perform your WSL dev work in /mnt/c/Users/<UserName>
Personally, I create a development directory in Windows and add a symbolic link to the directory in WSL:
ln -s /mnt/d/dev/environment/ ~/dev
cd dev now brings you to your development directory.
Ensure Java for Windows works
Open PowerShell/cmd.exe from any directory and enter: java --version
You should get a list of JRE info:
openjdk 11.0.4 2019-07-16 LTS
OpenJDK Runtime Environment Corretto-11.0.4.11.1 (build 11.0.4+11-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.4.11.1 (build 11.0.4+11-LTS, mixed mode)
Your version might be different, the important part is that the system knows where to find Java. If you get an error, ensure your Windows Environment variables are set correctly:
JAVA_HOME as an Environment Variable, and
JAVA_HOME/bin as a Path variable.
Setting Variable in WSL
The best place to put the next lines of code are in your .bashrc file, but if you have a ./bash_profile or /etc/profile structure set up, you can put it there.
# Shared environment variables
# Use 'java.exe <args>' to utilize Windows Java binaries from within WSL.
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10
While we're at it, let's add Maven too:
export MAVEN_HOME=/mnt/d/software/apache-maven-3.6.2
I have my WSL, Java, and all my other dev tools set up on my second HDD which is not a system drive, ensure that your location matches your JAVA_HOME path in Windows.
For instance, if in Windows, Java is located at: C:\Java\jdk8.0
The corresponding WSL mount point is: /mnt/c/Java/jdk8.0
Executing
Important: Use java.exe <args> in WSL instead of java <args>
Say you just wrote CompareTwoStrings.class and want to compile and run it using the Windows binaries. You can do it from a Windows shell or WSL.
Windows PowerShell/cmd:
javac GetStringLength.java
java GetStringLength
WSL:
javac.exe GetStringLength.java
java.exe GetStringLength
Using java <args> in WSL will result in a Command 'java' not found error. That is because running windows binaries from within WSL requires that the .exe extension is used in the command.
Simplicity
We don't want to install a second copy of Java specific to WSL and waste that precious disk space, so we're going to call the Windows binary from the WSL shell. This is a great benefit of WSL—WSL1 in particular—in that it can interact (almost) flawlessly with the Windows File System.
NOTE: In order to run a program, it must either be a Path variable, or be run from within it's containing folder.
Hopefully that works as easily for you as it did for me. Just remember to use the correct command depending on what OS binary you're running. This took me about 10 minutes to get set up, and has been a lifesaver for cross-compiling and general ease-of-use.
I originally had Maven working in Windows attempted to run Maven in WSL2 and tried all of the previous solutions, but would consistently get the following no matter what I set for JAVA_HOME and PATH:
$ mvn -v
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
The issue was I was trying to use a Windows version of the JDK in the WSL2 Linux kernel. To fix this I ended up having to install a Linux version of the JDK (version 11) in the WSL as follows:
$ sudo apt update
$ sudo apt install openjdk-11-jdk
$ sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
Nothing to configure.
Then take the path for your JDK and use it to create JAVA_HOME and update PATH by appending the following in .profile
export PATH="/usr/lib/jvm/java-11-openjdk-amd64/bin/java:$PATH"
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Now close and reopen WSL2 and when your try again:
$ mvn -v
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.10.16.3-microsoft-standard-wsl2", arch: "amd64", family: "unix"
Since I've never been able to share variables between the 2 systems easily, I created a simple bash function which can easily retrieve (and define, if asked to) any Windows Environment variable.
It also takes care of paths so they get converted from Win32 to Un*x-like.
I added this to /etc/bash.bashrc:
winenv()
{
if [ "$#" == "0" ] || [ "$1" == "--help" ]
then
echo $'\n'Usage:
echo $'\t'winenv [-d] WINDOWS_ENVIRONEMENT_VARIABLE_NAME
echo $'\t'-d: Defines environment variable in current shell
echo $'\t Note that paths will be translated into un*x-like\n'
return
fi
local IFS='$\n'
local PATH_TO_TRANSLATE=$1
[ "$1" == "-d" ] && PATH_TO_TRANSLATE=$2
local VAR=$(cmd.exe /c echo %${PATH_TO_TRANSLATE}% | tr -d '\r')
local NEW=$(wslpath -u "${VAR}" 2>/dev/null || echo ${VAR})
echo "${PATH_TO_TRANSLATE} = ${VAR} -> ${NEW}"
[ "$1" == "-d" ] && export "${PATH_TO_TRANSLATE}=${NEW}"
}
And all I have to do to display one is to call winenv PROGRAMFILES (for example)
Or if I expect to export it, I just have to add a -d argument before the variable name as in winenv -d WINDIR.

What is the difference between the JAVA_HOME environment variable and the output of "java -version"

What is the JAVA_HOME environment variable actually? I'm in doubt. What is done by setting it ? And I have also another question that if we have two or more JDK s in the OS, we can set one version's path to set the $JAVA_HOME variable and another version as the running java in the machine which is shown by "java -version" command.As I think in ubuntu, java --version command gives the JRE configured (running java) version of java and JAVA_HOME variable defines which version to use for compiling java programs. Am I correct? What is actually the difference between the jdks referred by these two outputs-
1. echo $JAVA_HOME
2. java -version
Lets start with what the two commands do.
echo $JAVA_HOME
This outputs the value of the JAVA_HOME environment variable. By convention, this environment variable is set (typically in a shell "rc" file) to the base directory of a JRE or JDK installation. However, none of the Oracle or OpenJDK core java tools depend on this. You set JAVA_HOME for the benefit of 3rd-party tools, to tell them which of possibly many JRE or JDK installations on the system to use.
So running echo $JAVA_HOME will typically give the directory for a JRE or JDK. But it might be unset ... or point to missing directory.
java -version
This runs the java command and tells it to output its own version information. Unlike the previous, if your shell can find a (real) java command, the information it outputs is definitive.
But which JRE / JDK does will the java command come from?
Well that is determined by the shell, and how it (in general) finds commands. For all mainstream Unix / Linux / BSD shells, this is controlled by the PATH environment variable. The PATH variable defines a command search path; i.e. a list of directories where the shell looks for commands.
So if PATH is /usr/bin:/bin:/usr/local/bin:/home/joeuser/bin:/usr/java/jre-x.y.z/bin then the shell will look for java in each of those directories in turn. The first one java command that it finds will be the one that is executed. You can find out which one it is by running this command.
which java
But that is not the end of it, because if you are using an OpenJDK Java installation on a typical Linux system, the java command in /usr/bin is actually the first in a chain of symlinks that is managed by the alternatives command. The actual executable is at the other end of the chain.
(The alternatives system is a way to implement a switchable system-wide default for a command or set of commands. Read the manual entry for more details. But note that Oracle Java installers (or RPMs) for Linux don't "grok" the Linux alternatives system ... one of my bugbears!)
Finally, it is common to see something like this in a shell "rc" file:
export JAVA_HOME=/path/to/jre # (or jdk)
export PATH=/bin:$JAVA_HOME/bin:/usr/bin:...
There is nothing magical about this. The $JAVA_HOME is going to be expanded by the shell before it sets the environment variable. Tthe actual PATH value will be:
/bin:/path/to/jre/bin:/usr/bin:...
So in summary:
echo $JAVA_HOME tells you the JRE or JDK that a typical 3rd-party tool will try to use by default.
java -version tells you the JRE or JDK that the java command comes from.
They may be different.
JAVA_HOME is an environment variable that contains the directory where Java is installed (if a few versions of Java are installed - this will point to the default)
java -version is a command that shows which version of Java is installed (and again, the one that's used by default in case a few versions are installed)
Addition: Thanks to Luiggi's comment: if the folder where java is installed is not in the PATH (another environment variable) - running java -version or any other java command will fail!
Its not related to jdk and JRE. java -version it is used by the java which is in /usr/bin/java. In some application is used $JDK_HOME. Both the jdks are same. Or we can define different jdk.
echo $JAVA_HOME
will print the value of environment variable called JAVA_HOME. If it's not set, it will print an empty message. Usually, you set JAVA_HOME to the path of the latest JDK installed in your pc.
java -version
will print the version of the java executable (Java Virtual Machine) that is set in your path, if configured. Otherwise, it will throw an exception on the command line.

Find JAVA_HOME and set it on RHEL

I had installed java a while ago on my RHEL machine. Now, I'm trying to run a program that requires the JAVA_HOME variable to be set. What is the best way to figure out the installation directory of my java installation and then set JAVA_HOME? Here are the results of running java- version:
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
I have a /usr/lib/jvm directory, but it is empty.
RHEL uses alternatives subsystem to manage java installations. You can have
multiple versions of java installed, but only one is active at a time.
This means that running which java doesn't provide useful information. The
output would be the same no matter which java installation is selected via
alternatives. Running readlink -f $(which java) (as already suggested in
other comment) or using asking alternatives alternatives --display java would
be better.
See example from RHEL 6 machine with OpenJDK installed (which is shipped with
RHEL):
[root#example ~]# which java
/usr/bin/java
[root#example ~]# readlink -f $(which java)
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/jre/bin/java
[root#example ~]# alternatives --display java | head -2
java - status is manual.
link currently points to /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
Note that enviroment variable JAVA_HOME is not defined anywhere by default,
you would need to define it yourself in .bashrc of user which requires it.
In previous example, correct value of JAVA_HOME would be
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64.
See details in Install OpenJDK
documentation, search for section "Optional: Set the JAVA_HOME environment variable".
First, try echo $JAVA_HOME from the command line. Since java is on your path already, JAVA_HOME may be set.
What is the best way to figure out the installation directory of my java installation
Running the command which java will point you to where java is installed.
and then set JAVA_HOME
You can edit ~/.bashrc, ~/.bash_profile, or /etc/profile to set JAVA_HOME. Setting it in ~/etc/profile will set it system wide, and this is probably not what you want. Say for the sake of example the output of which java is /opt/jdk_1.7.0_25, then you'd just add export JAVA_HOME=/opt/jdk_1.7.0_25 to ~/.bashrc or ~/.bash_profile and then run source ~/.bashrc (or source ~/.bash_profile if you set it there).
Note that in this case, java is on the PATH but in some cases you'd need to add export PATH=$PATH:$JAVA_HOME/bin to add the JAVA_HOME variable to the PATH.
readlink command will show you full path of symbolic link:
readlink -f `which java`
The best you can do is avoid Red Hat's java altogether.
Get your java from Oracle and put it in /opt.
Then just create symlink /opt/java -> /opt/jdk-someversion, and create /etc/profile.d/java.sh containing
#!/bin/sh
export JAVA_HOME=/opt/java
export PATH=$JAVA_HOME/bin:$PATH
Then, to change system-wide java, just change symlink in opt.
To use multiple java versions, use scripts like the above with appropriate JAVA_HOME.
Furthermore, /sbin/service script used to run /etc/init.d scripts will rip off environment variables - executes env -i explicitly. So i.e. your tomcat will not get JAVA_HOME, you'll have to create setenv.sh in $CATALINA_BASE/bin.
Drawback to this approach is you don't get java updates from Red Hat.
I found a way to combine the above so that I can programmaticly set JAVA_HOME.
export JAVA_HOME=$(dirname $(readlink -f $(which java))|sed 's^jre/bin^^')
On Ubuntu, returns /usr/lib/jvm/java-8-openjdk-amd64/.
On CentOS7, returns /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/.
This can be added to a Makefile with
JAVA_HOME = $(shell dirname $$(readlink -f $$(which java))|sed 's^jre/bin^^')
This is much safer than exporting a hardcoded path and is more portable.
At least on RHEL 7, alternatives sets up a slave link for java_sdk at:
/etc/alternatives/java_sdk/
This is a symlink to the root of the SDK installation.
For me worked to change link in
/etc/alternatives for:
java -> /app/java/jdk8u222-b10/jre/bin/java
I know this is old java, but this server was neglected in our company.

Editing the listed hudson java system info

I am aware that Hudson can use different versions of Java to compile projects. What I am looking for however, is to edit the version hudson lists on its SystemInfo page:
java.endorsed.dirs
java.ext.dirs java.home
java.io.tmpdir
java.library.path
java.runtime.name
java.runtime.version
Those things. Currently, OpenJDK is listed. I have previously installed the regular Java 6.021 JDK from sun itself. I want to make it so that is listed, and not OpenJDK. OpenJDK was installed along with Hudson, for some reason.
The problem is that I can't find any of this in the config files. Where do I set this?
Also, I have modified the PATH variables and the /etc/bash.bashrc file to have respectively the correct paths and the JAVA_HOME variable. These have been tested and confirmed in the past.
Check the following link (ls -l /usr/bin/java):
/usr/bin/java
What does it point to? My guess is it is pointing to the OpenJDK java executable.
You have to make it point to your Sun JDK java executable: $JAVA_HOME/bin/java (put full path of $JAVA_HOME here.
cd /usr/bin && ln -sf $JAVA_HOME/bin/java .
(have to do this as root, and again substitute full path instead of $JAVA_HOME)
These settings come from the JVM that Hudson is running under. If these settings are reporting OpenJDK, then somehow your environment variable changes must not have taken effect.
Make sure that that JAVA_HOME change is visible to Hudson, and that the Sun JDK appears on the PATH before any OpenJDK directory.

Set Java environment variable problem on ubuntu

I got a message "No Java virtual machine could be found from your PATH
environment variable. You must install a VM prior to
running this program." Does anyone know how set up it correctly? Thanks!
If you install Java the Ubuntu way, you won't have to add it to the PATH yourself.
First, install Java from the Ubuntu repository:
sudo apt-get install sun-java6-jre
There are several other packages: sun-java6-jdk for the JDK, sun-java6-plugin for the browser plug-in etc.
Use Ubuntu's alternatives mechanism to select Sun Java 6 as the default version of Java that you want to use:
sudo update-alternatives --config java
This will present you with a menu where you can choose which version of Java you want to use by default (you can have multiple Java versions installed at the same time on your system).
See: https://help.ubuntu.com/community/Java
As the error message says, this looks like you have not set your PATH or JAVA_HOME environment variable correctly.
can you execute java from the command line? Try:
$ java -version
or
$ dpkg -L sun-java6-jre
Provided your are using debian or some derivative of it, if the "dpkg ..." command gives you any useful output, you should set the JAVA_HOME to the location of the installation directory, for instance
export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.10
export PATH=$PATH:$JAVA_HOME
and you should be able to get java running.
First make sure you have the path to your java binaries.
locate javac
This will return a list of all locations matching "java". Look for something like "/usr/lib/jvm/java-6-sun-1.6.0.11".
Open /etc/environment with a text editor and add the following. Make sure you set JAVA_HOME to the actual path of the java installation directory you just found:
export JAVA_HOME="/usr/lib/jvm/java-6-sun-1.6.0.11"
export PATH="$PATH:$JAVA_HOME/bin"
I think that you need to install this package
sudo apt-get install sun-java5-bin
also check this
> #!/bin/bash
> # Init Script for j2re
> #This goes in /etc/profile.d
>
> JAVA_HOME=/usr/java/j2reVERSion
> PATH=$JAVA_HOME/bin:$PATH
>
> export JAVA_HOME PATH

Categories