How to correctly set the JAVA_HOME environment variable? - java

on a tutorial (related to a product) it is shown hot to set the JAVA_HOME environment variable on a Linux Ubuntu system, in this way:
echo "JAVA_HOME=\"/usr/lib/jvm/default-java\"" | sudo tee -a /etc/environment
I have some trivial doubts:
1) What exactly is the JAVA_HOME environment variable and for what is it used?
From what I have understand it is something like a link to the JVM setted into the operating system that is used from the servlet container\application server to know where the JVM is and so it can be used. Is this assertion true or am I missing something? Exist some other purpose of this environment variable?
2) In the tutorial it is shown that the path of this environment variable is:
/usr/lib/jvm/default-java
but into my system I have something different, infact into the /usr/lib/jvm/ path I have not the default-java directory but I have a path like this:
/usr/lib/jvm/java-8-oracle
that contains the following directories:
bin
db
include
jre
lib
man
and some other files.
What is the correct path to use for the settings of my JAVA_HOME environment variable?
3) What exactly does this section of the previous statment:
sudo tee -a /etc/environment
Tnx

1: You are correct
2: It should point to the actual root folder of the jvm/jdk install, in this case /usr/lib/jvm/java-8-oracle
3: tee is a command to output the input both to a file and the command line, that -a option appends it to the file. So essentially it, as a super-user, appends the output of the previous command echo "JAVA_HOME=\"/usr/lib/jvm/default-java\"" to the file /etc/environment but also outputs it to the command line. Output should be, for you, exactly: JAVA_HOME="/usr/lib/jvm/java-8-oracle"

Related

How to upgrade JDK from Oracle to Zulu in redhat linux

I want to upgrade my Linux JDK from Oracle to Azul-Zulu.can someone helps me to do this.
What to do after downloading the tar.gz Zulu package?
I see that after extracting the downloaded package we need to put it in the path where we need to access java. Is that correct or what we can do more like setting environment variables etc.
the answer to your question depends on what you actually want to start with the JDK. Sometimes is is sufficient to change the path to java binary in a start script, e.g. Eclipse or Tomcat, sometimes you also need to setup the variable JAVA_HOME.
To setup the java for yourself only:
Check your shell by running echo $SHELL
-> /bin/bash
-> /bin/zsh
-> /bin/tcsh
or something like that Locate your shell's config file:
bash: ~/.bashrc
zsh: ~/.zshrc
tcsh: ~/.cshrc or ~/.tcshrc
and optionally most of the shells: ~/.profile or ~/.login check those file(s) if you already have variable declarations for: JAVA_HOME JRE_HOME (and rarely seen) SDK_HOME JDK_HOME
check whether any of those files contain a setup for the variable PATH (probably has)
If you found any of the *_HOME variables from above you can change them to reflect your new java home directory: e.g. zulu11.37.17-ca-jdk11.0.6-linux_x64 located under /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64 would mean to set the variables as follows: zsh and bash:
export JAVA_HOME=/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
export JRE_HOME=/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
tcsh
setenv JAVA_HOME /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
setenv JRE_HOME /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
SDK_HOME and JDK_HOME may point to the above locations as well but are normally unused.
If you located your PATH somewhere it may read something like this: zsh and bash:
export PATH=~/bin:$PATH
tcsh:
setenv PATH ~/bin:$PATH
In order for your new java to appear "before" the "other" java you need to update (or create if missing) this to e.g.: zsh and bash:
export PATH=~/bin:/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64/bin:$PATH
tcsh:
setenv PATH ~/bin:/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64/bin:$PATH
After completely logging out and in again your JAVA_HOME and PATH can be controlled in a shell like this:
echo $JAVA_HOME -> should point to *your* java folder
echo $PATH -> should contain *your* java folder's bin directroy
which java -> should list the new java binary instead of /bin/java or /usr/bin/java
BTW: the variables JAVA_HOME, JRE_HOME and PATH may also be set in a startscript, e.g. catalina.sh for tomcat or the eclipse.ini for eclipse if you do not want to modify your environment.
If you download the RPM/DEB instead of the tar.gz and install the package you (may) not need configure anything since the packages might re-configure your system for you
Cheers, Holger

Although I have JAVA_Home set, the system can not find it

I set my JAVA_HOME variable the follow path: /usr/lib/jvm/jdk11 and when i put the following comand: java -version , it shows me the current version and echo $JAVA_HOME indicates the above path.
When I start tomcat the following error message comes up:
neither JAVA_HOME nor JRE_HOME variable defined.
I setting up everything right, but the tomcat cant find it.
Tomcat calls a script, setenv.sh, from catalina.sh.
Create a script called setenv.sh in the directory CATALINA_BASE/bin, and add the line: "export JAVA_HOME=/usr/lib/jvm/jdk11". Make the script executable, i.e. chmod +x setenv.sh
You will need to add the following line in your .bashrc file:
export PATH=$PATH:$JAVA_HOME/bin
The .bashrc file is located in the /home directory.

Not able to set JAVA_HOME variable in ubuntu 14.04 on manual installation

I've downloaded java jdk1.8.0.7.tar.gz file from the official website and unzipped it into my home directory. Now to set the $JAVA_HOME variable I used the follwing commands command nano .bashrc and then appending export $JAVA_HOME=/home/shivam/Java/jdk1.8.0.7 at the end of the file . But whenever I run the command sudo $CATALINA_HOME/bin/startup.sh I get an error message saying
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
I don't understand why it is unable to find the java path though I've installed tomcat and set its home variable the this way, which worked.
I wan't to install Java manually without using apt-get. Kindly guide .
You want to append the following to .bashrc:
JAVA_HOME=/home/shivam/Java/jdk1.8.0.7
Note the missing $ at the start.
Also remember that the file won't effect your current shell without sourceing it first.
Also, when running a command with sudo, you are running it as the root user, not as yourself. So the environment variable needs to be set for the root user, not yourself.
You can run sudo env | grep JAVA_HOME to see whether it is set for root.
Have you run .bashrc ?
source .bashrc
Try add the following 2 lines in your .bashrc file:
JAVA_HOME=/home/shivam/Java/jdk1.8.0.7/
export JAVA_HOME

/usr/local/java/jdk1.8.0_40/jre/bin/bin/java: No such file or directory

I enter this command and the it gives back this error
`bin/hadoop jar
share/hadoop/mapreduce/hadoop-mapreduce-examples-2.6.0.jar grep input
output 'dfs[a-z.]+'
bin/hadoop: line 144: /usr/local/java/jdk1.8.0_40/jre/bin/bin/java: No
such file or directory`
please help.
Looks like you're trying to set Hadoop. You should find where Java is installed in your machine, then go to
/etc/<hadoop>/<conf>/hadoop-env.sh
and change JAVA_HOME to where Java is installed.
In order to know where Java is installed, try to echo $JAVA_HOME (if it's set it'll show the path), if not, try to search for it:
sudo find /usr/ -name *jdk
Now I've noticed that you have a double /bin in the path, change the JAVA_HOME and remove the redundant /bin.
Path /usr/local/java/jdk1.8.0_40/jre/bin/bin/java has /bin/bin twice. It shows that PATH environment or JAVA_HOME variable is wrongly configured. Check that path specified in PATH variable should be /usr/local/java/jdk1.8.0_40/jre/bin or JAVA_HOME = /usr/local/java/jdk1.8.0_40/jre

Why can't Git Follow my Java JDK installation path?

I'm trying to compile a project in using Git Bash, but i'm having a problem with java.
I have the JDK version 1.8.0.25 installed under the path T:\Program Files\Java\jdk1.8.0_25.
Initially, I tried to set the JAVA_HOME variable via the command in Git like so:
$ JAVA_HOME="T:\Program Files\Java\jdk1.8.0_25" Java -jar BuildTools.jar
This didn't work, I get an unable to locate directory error.
So I tried manually setting the environment variable via the command:
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_25"
Then I ran the compile command without the first part and I get an error saying that the compiler tools were not found in my (JRE) directory. Almost as if its ignoring the environment variable I set and searching the JRE instead of the JDK anyway. Why won't it follow the path I specify?
Are you sure you need to set the actual JAVA_HOME variable? If I remember correctly, for git bash it looks at the $PATH variable for the java version. Maybe try appending your java location here.
also, to double check (sorry if you already know this), you can run the env command to list all your environment variables.
$ env
Try setting the path and run the java -v command to verify the installation is correct. If not, what is the exact error?
$ java -v
Edit
Some more info, to set the $PATH in git windows I think you have to update your $HOME. See this or some other posts (google) for an explanation
Git for Windows: .bashrc or equivalent config files for Git Bash shell

Categories

Resources