Change JAVA_HOME value in Linux Mint 19x - java

I have a linux mint 19x and if I do:
echo $JAVA_HOME >> /usr/lib/jvm/java-8-oracle
I want to change the path to put Java 11, but not in the .bashrc, .profile, /etc/enviroment file, I see my JAVA_HOME defined
I tried to put the JAVA_HOME path in my /etc/environment
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Can someone tell me where my JAVA_HOME can be defined, so I can change it well.

Via the command line, you can simply change your JAVA_HOME variable path:
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Then redefine the current path prefixing the jdk binaries:
export PATH=$JAVA_HOME/bin:$PATH
NB: This will only take effect within the current terminal session.

Firstly, check what JDKs you have installed :
sudo update-alternatives --config java
# And select the usr/lib/jvm/java-11-openjdk-amd64/bin/java version.
# Also, set the JAVA_HOME variable for Maven to pick the correct JDK:
Then :
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Another great option is to use SDKMan to manage SDKs like Java and so on.
I use all the time on all my Linux machines and it's very easy to switch between Java version.
You can find the link here:
https://sdkman.io

Related

Setting JAVA_HOME in macos

When I do
echo "export JAVA_HOME=`/usr/libexec/java_home`"
I get
export JAVA_HOME=/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Which is a JRE and not the one I want - it's under /Library/Java/JavaVirtualMachines/...
Going into the .bash_profile file I see JAVA_HOME is set to
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
following the link - java_home is a file.
So not sure how the above works or how it got changed to the JRE instead of the JDK.
I suppose I can change the JAVA_HOME in the profile file to the one I want - but is there a preferred way of making sure the JDK is used by all apps on the mac.
Thanks.

How do I set Path Variables in Linux just like in windows?

I downloaded JDK and have many softwares which use requires it. For eg: for eclipse I'll copy the jdk in java folder in eclipse root directory. As a result I have many copies of JDK all over my system. Also dont know how to configure it for Sublime Text. Is is possible to install java just like in windows. I mean that I just open terminal and type java and it executes instead of changing directory everytime.
You could set your path variable but you really shouldn't be doing it like that.
Since you don't specify which version of Linux I'll be explaining it for Ubuntu and Arch Linux.
You should install openjdk packages with your package manager like so:
Ubuntu:
sudo apt-get install openjdk-8-jdk
Arch linux:
sudo pacman -S jdk8-openjdk
You can then use the following commands to change between those versions:
Ubuntu
sudo update-alternatives --config java
Arch Linux
archlinux-java set java-8-openjdk
You can even launch specific apps in specific versions but this would be too much to cover for now.
Setting the path variable regardless
To change your $PATH you have to either edit ~/.profile (or ~/.bash_profile) for each user or global $PATH setting in /etc/profile.
Simply append another path to it like so:
/usr/local/bin:/usr/local/sbin:/custom/path
You can also do:
PATH = $PATH:/custom/path
Multiple installation is never a good idea. You can have windows like setup but in *nix OS you have to follow different steps.
The preferred location for JAVA_HOME or /etc/environment. So Open /etc/environment in any text editor and add the following line:
JAVA_HOME="/usr/lib/your jdk path" (Use your java path)
If needed run else skip to next step source /etc/environment
See if done right?
echo $JAVA_HOME
Manual setup
Yes, just set PATH and JAVA_HOME suitably. For example, see this guide. This should give something like the following:
~/.bash_profile (or /etc/profile)
...
export JAVA_HOME=PATH_TO_JDK_ROOT/bin/java
export PATH=$PATH:PATH_TO_JDK_ROOT/bin
Linux distribution setup
Depending on your needs, you may otherwise opt to install your linux distribution JDK (using apt-get, yum, ...), thus avoiding manual steps to set the env vars.
Example: Ubuntu 16.04
To install the default JDK:
sudo apt-get install default-jdk
To install the Oracle JDK:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
To select the active Java version when there is more than one installed:
sudo update-alternatives --config java
If you want the appended value to be local to the script, do not prepend the export. Otherwise follow the example below which uses a search ability. I have appended to the PATH statement and want to export it so the PATH is known globally. If I invoke a second terminal, the if statement prevent the PATH environment variable from being double appended.
if [ ! "/home/mylogon/bin" == $PATH ];
then
export PATH=$PATH:/home/mylogon/bin:/home/mylogon/.local/bin"
fi

Java is installed but `echo $JAVA_HOME` doesn't seem to produce any output

I needed Maven for a Java project, so I installed it directly in a fresh Ubuntu installation using sudo apt install maven. It automatically downloaded Java 1.8_091 and installed it too. I ran echo $JAVA_HOME in the terminal but it doesn't produce any output. How do I rectify this? Is there any problem with environment variables.
When you download jdk and wants to install in ubuntu, then you need to create an env variable JAVA_HOME in .bashrc file and add this variable to $PATH variable. so that when you echo $JAVA_HOME, you will get the path to your java installation
As u have installed it using sudo apt, it doesn't set JAVA_HOME variable rather installs the java inside /usr/bin folder, and also set alternatives in ubuntu so that ubuntu would be able to identify java
To set JAVA_HOME only in actual shell session, invoke command:
export JAVA_HOME=/usr/lib/jvm/java-<version>
To persist this environment variable edit vi ~/.bash_profile file, and append it to it
To create the variable $JAVA_HOME, you've just to execute these lines :
JAVA_HOME=/pathToJavaJDKorJRE/java
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
Note : If you don't know the path of your JDK or JRE (pathToJavaJDKorJRE), use this command to find them.
find / -name 'javac'

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.

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