Setting Java path using bash script CentOs 7 - java

I'm trying to change the path of java_home to java-1.7.0 in a CentOS 7 machine which already has java-1.8.0 installed.
I uninstalled java-1.8.0, and used 
yum -y install java-1.7.0
to install java 1.7. However,
which java
command still points to java-1.8. How do I find the path of java 1.7 JRE in this case in order to set java_home using a bash script?
Thank you,

Try setting JAVA_HOME with the new java path(eg. '/usr/local/java1.7/jdk').
export JAVA_HOME=/usr/local/java1.7/jdk
Then add the JAVA_HOME/bin to PATH variable.
export PATH=$JAVA_HOME/bin:$PATH
By default java will get installed in the ' /usr/lib/jvm ' location.
So, try ls command in this path to find the required java version.
cd /usr/lib/jvm
ls
Use this path to set the JAVA_HOME.

Related

WSL Bash isn't finding java in PATH

I have an export command in my .bashrc to add the path of the java.exe file to PATH. Right now, running echo $PATH gives me this at the end
/mnt/c/Program Files/Java/jdk-14.0.2/bin
This is exactly where the java.exe and javac.exe files are stored, but when I run something like
java -version I'm getting the Command 'java' not found error. What am I doing wrong here?
You have added the Windows version of the Java binaries to the Path. You use WSL to run Linux binaries. (WSL1 is a compatibility layer, WSL2 uses the Linux kernel).
You have two options:
Install Java in your WSL environment. For example on Ubuntu with following commands:
sudo apt update
sudo apt install openjdk-14-jdk
After installing Java it will be available in the search path.
You could also run the Windows version by calling java.exe (note the .exe) on the name. This way WSL would call the Windows version. (https://learn.microsoft.com/en-us/windows/wsl/interop#run-windows-tools-from-linux)
If you want to use the windows version of java for some reason you where almost there. Try this please :)
java.exe -version
Install java in wsl:
sudo apt-get -y install openjdk-14-jdk
check the version.
java –version
check the path to config JAVA_HOME.
sudo update-alternatives --config java
eg
/usr/lib/jvm/java-14-openjdk-amd64/bin/java.
edit environment file to add the path:
sudo nano /etc/environment
add declare and add the JAVA_HOME:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$JAVA_HOME/bin"
JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
save and check the path.
source /etc/environment
echo $JAVA_HOME
Output
/usr/lib/jvm/java-14-openjdk-amd64

Change JAVA_HOME value in Linux Mint 19x

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

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'

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