I'm pretty new on ubuntu, at some point in the terminal I'm running:
mortar local:illustrate pigscripts/retail-recsys.pig purchase_input -f params/retail.params
but I have this following error:
A suitable java installation could not be found. If you already have java installed
please set your JAVA_HOME environment variable before continuing. Otherwise, a
suitable java installation will need to be added to your local system.
Installing Java
On OSX run javac from the command line. This will intiate the installation. For
Linux systems please consult the documentation on your relevant package manager.
But I'm pretty sure I have Java, so please how can I set my JAVA_HOME environment variable?
First, you need to decide which installed version of Java to use? No fear, you can pick any you have -
update-java-alternatives -l
One "easy" solution is to add this to "$HOME/.bashrc",
export JAVA_HOME=$(update-java-alternatives -l | head -n 1 | awk -F ' ' '{print $NF}')
This picks the first installed JDK and takes it's JAVA_HOME (the third field) - on my system that's
/usr/lib/jvm/java-1.7.0-openjdk-amd64
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
in your ~/.bashrc file.
If you want this environment variable available to all users and on system start then you can add the following to /etc/profile.d/java.sh (create it if necessary):
export JDK_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
Then in a terminal run:
sudo chmod +x /etc/profile.d/java.sh
source /etc/profile.d/java.sh
For JAVA_HOME to point to the active jdk, add to your ~/.bashrc
export JAVA_HOME=$(update-alternatives --query javac | sed -n -e 's/Best: *\(.*\)\/bin\/javac/\1/p')
which will dynamically set the $JAVA_HOME to the JDK selected by update-alternatives.
The simplest method to set environment variable is with export:
$ export JAVA_HOME="/usr/bin"
This will temporarily set the desired variable. You can check if it was set with:
$ echo $JAVA_HOME
or
$ printenv
If you want a more permanent solution, append 'export JAVA_HOME="/usr/bin"' to .bashrc or .bash_profile file.
To check if java is properly installed:
$ which java
$ which javac
You should get similar output:
/usr/bin/java
put the line export JAVA_HOME=/usr/lib/jvm/java-xxx-oracle in your .profile file at home directory. Note that you have to replace xxx. You may need to logout and login again
In Debian/Ubuntu/Linux Mint, we could add to .bashrc
export JAVA_HOME=$(update-java-alternatives -l | head -n 1 | sed 's/\s//g')
Normally you can set paths in
~/.bashrc
with export JAVA_HOME=/usr/lib/jvm/java-version
However you may followe instructions from here for a comprehensive instruction.
By far, the ultimate guide to doing this is here. You don't need to set PATH as much as you just need to adjust the default 'java alternative' location.
you can type java in terminal,if it does not work means your did not install java.if it works, type javac in terminal.if javac dose not work,you should set the java environment variable,if it works ,there maybe something wrong with you program.
Related
I just installed JDK in Ubuntu with sudo apt-get install openjdk-6-jdk command,
after the installation where's the Java bin directory located? And how can I set the environment path for that directory? I have little experience with Ubuntu, can anyone give some advice or suggest any good website for reference?
set environment variables as follows
Edit the system Path file /etc/profile
sudo gedit /etc/profile
Add following lines in end
JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Then Log out and Log in ubuntu for setting up the paths...
Java is typically installed in /usr/java
locate the version you have and then do the following:
Assuming you are using bash (if you are just starting off, i recommend bash over other shells) you can simply type in bash to start it.
Edit your ~/.bashrc file and add the paths as follows:
for eg. vi ~/.bashrc
insert following lines:
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
after you save the changes, exit and restart your bash or just type in bash to start a new shell
Type in export to ensure paths are right.
Type in java -version to ensure Java is accessible.
Ubuntu installs openjdk6 to /usr/lib/jvm/java-6-openjdk path. So you will have the bin in /usr/lib/jvm/java-6-openjdk/bin. Usually the classpath is automatically set for the java & related executables.
To Set JAVA_HOME / PATH for a single user, Login to your account and open .bash_profile file
$ vi ~/.bash_profile
Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:
export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
Set PATH as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Feel free to replace /usr/java/jdk1.5.0_07 as per your setup. Save and close the file. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:
$ source ~/.bash_profile
OR
$ . ~/.bash_profile
Verify new settings:
$ echo $JAVA_HOME
$ echo $PATH
Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:
$ which java
Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.
To Set JAVA_HOME / PATH for all user, You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
# vi /etc/profile
Next setup PATH / JAVA_PATH variables as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Save and close the file. Once again you need to type the following command to activate the path settings immediately:
# source /etc/profile
OR
# . /etc/profile
You need to set the $JAVA_HOME variable.
In my case while setting up Maven, I had to set it up to where JDK is installed.
First find out where JAVA is installed:
$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
Now dig deeper:
$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 46 Aug 25 2018 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Dig deeper:
$ ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
-rwxr-xr-x 1 root root 6464 Mar 14 18:28 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
As it is not being referenced to any other directory, we'll use this.
Open /etc/environment using nano:
$ sudo nano /etc/environment
Append the following lines
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export JAVA_HOME
Reload PATH using:
$. /etc/environment
Now:
$ echo $JAVA_HOME
Here is your output:
/usr/lib/jvm/java-1.8.0-openjdk-amd64
Sources I referred to:
https://askubuntu.com/a/175519
https://stackoverflow.com/a/23427862/6297483
if you have intalled only openJDK, the you should update your links, because you can have some OpenJDK intallation.
sudo update-alternatives --config java
after this
$gedit ~/.bashrc
add the following line in the file
JAVA_HOME=/usr/lib/jvm/YOUR_JAVA_VERSION
export PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
you can get you java version with
java -version
Open terminal (Ctrl+Alt+t)
Type
sudo gedit .bashrc
Enter password of ubuntu user
Go to last line of the file
Type below code in new line
export JAVA_HOME=enter_java_path_here
export PATH=$JAVA_HOME/bin:$PATH
eg: export JAVA_HOME=/home/pranav/jdk1.8.0_131
export PATH=$JAVA_HOME/bin:$PATH
Save the file
Type
source ~/.bashrc
in terminal
Done
To set up system wide scope you need to use the
/etc/environment file sudo gedit /etc/environment
is the location where you can define any environment variable. It can be visible in the whole system scope. After variable is defined system need to be restarted.
EXAMPLE :
sudo gedit /etc/environment
Add like following :
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/opt/jdk1.6.0_45/"
Here is the site you can find more : http://peesquare.com/blogs/environment-variable-setup-on-ubuntu/
How to install java packages:
Install desired java version / versions using official ubuntu packages, which are managed using alternatives:
sudo apt install -y openjdk-8-jdk
or/and other version:
sudo apt install -y openjdk-11-jdk
Above answers are correct only when you have only one version for all software on your machine, and you can skip using update-alternatives. So one can quickly hardcode it in .bashrc or some other place:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
but it's not healthy, as later on you may change the version.
Correct way to set JAVA_HOME (and optionally JAVA_SDK, JAVA_JRE )
The correct way (and mandatory when you have more than one), is to detect what update-alternative is pointing to, and always use update-alternatives to switch active version.
Here are the suggestions for both: only specific unix account or for all accounts (machine level).
1. for a specific unix account only:
Use this if you don't have permissions to do it at machine level.
cat <<'EOF' >>~/.bashrc
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
2. To do it at machine level, and for all bourne shells, you need 2 steps:
2.a
cat <<'EOF' | sudo tee /etc/profile.d/java_home_env.sh >/dev/null
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
As your shell might not be set as interactive by default, you may want to do this also:
2.b
cat <<'EOF' | sudo tee -a /etc/bash.bashrc >/dev/null
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
EOF
PS: There should be no need to update the $PATH, as update-alternatives takes care of the link to /usr/bin/.
More on: https://manpages.ubuntu.com/manpages/trusty/man8/update-alternatives.8.html
It should put java in your path, probably in /usr/bin/java. The easiest way to find it is to open a term and type which java.
Create/Open ~/.bashrc file $vim ~/.bashrc
Add JAVA_HOME and PATH as referring to your JDK path
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Save file
Now type java -version it should display what you set in .bashrc file.
This will persist over sessions as well.
Example :
Update bashrc file to add JAVA_HOME
sudo nano ~/.bashrc
Add JAVA_HOME to bashrc file.
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Ensure Java is accessible
java -version
In Case of Manual installation of JDK, If you got an error as shown below
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Execute the following command in your JAVA_HOME/lib directory:
unpack200 -r -v -l "" tools.pack tools.jar
Execute the following commands in your JAVA_HOME/jre/lib
../../bin/unpack200 rt.pack rt.jar
../../bin/unpack200 jsse.pack jsse.rar
../../bin/unpack200 charsets.pack charsets.jar
Ensure Java is accessible
java -version
I have a Linux Lite 3.8 (It bases on Ubuntu 16.04 LTS) and a path change in the following file (with root privileges) with restart has helped.
/etc/profile.d/jdk.sh
Step1:
sudo gedit ~/.bash_profile
Step2:
JAVA_HOME=/home/user/tool/jdk-8u201-linux-x64/jdk1.8.0_201
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Step3:
source ~/.bash_profile
Let me simplify:
download JDK from Oracle Website : Link
Extract it
Create a folder (jvm) in /usr/lib/ i.e /usr/lib/jvm
move the extracted folder from the jdk to /usr/lib/jvm/
*Note : use terminal, sudo, mv command i.e. sudo mv
Create a .sh file at /etc/profile.d/ eg: /etc/profile.d/myenvvar.sh
In the .sh file type
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin
*Note : use terminal, gedit and sudo eg: sudo gedit myenvvar.sh
Turn Off the Computer, after all these steps and Restart it
Open Terminal , and type
java -version
Check the output , then type
echo $JAVA_HOME
Once I've installed the openjdk version of the Java Development Kit on an Ubuntu machine, I use this procedure to create a JAVA_HOME environment variable that doesn't need to be changed after every version upgrade of the openjdk installation.
Firstly, I issue a command to discover the directory in which the java executables are located for this java installation.
echo $(readlink -e `which java` | xargs -0 dirname)
If I'm happy with the output from that, everything else can be derived from it.
Configuring the JAVA_HOME and PATH environment variables
Rather than adding more and more configurations to the ~/.bashrc file I've found it a cleaner practice to create a separate small file that ~/.bashrc can "include" when it runs.
Let's call that file ~/.java_env_vars (but you could name it whatever you wish).
Add an "include condition" to the ~/.bashrc file
Open ~/.bashrc in any text editor and these lines to the end of the file:
# include the java environment configuration file here (if it exists)
if [ -f "$HOME/.java_env_vars" ]; then
. $HOME/.java_env_vars
fi
Create the Java environment variable configuration file
Open any text editor, create the Java environment configuration file ~/.java_env_vars and add this content to it:
#1. set a java_bin variable to the directory containing the actual Java executables.
java_bin=$(readlink -e `which java` | xargs -0 dirname)
#2. append "$java_bin" to the PATH environment variable
export PATH=$PATH:"$java_bin"
#3. assign the directory of the current Java installation to the JAVA_HOME environment variable.
export JAVA_HOME=$(dirname "$java_bin")
NOTE: exporting the PATH and JAVA_HOME variables just ensures that they're always available wherever they're needed.
Source your ~/.bashrc file (so that the changes you made to it are reflected) using the following command:
source ~/.bashrc
Test the Java environment variable configuration
Open a new terminal console, and test the new Java environment variables by issuing this command:
echo "$PATH" && echo "$JAVA_HOME"
Output should be two lines, something like this:
/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin
/usr/lib/jvm/java-11-openjdk-amd64
All you have to do now is to set the “JAVA_HOME” and “PATH” environment variables and then you are done. Enter the following commands to set your environment variables. Make sure that your environment variables point to a valid installation of JDK on your machine. For Ubuntu 18.04, the path is /usr/lib/jvm/java-8-openjdk-amd64/
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
To check whether your JAVA_HOME path has been successfully saved, enter the following command to check.
echo $JAVA_HOME
Open file /etc/environment with a text editor
Add the line JAVA_HOME="[path to your java]"
Save and close then run source /etc/environment
Set java version from the list of installed. For see the list of the installed version run following command:
update-java-alternatives --list
Then set your java version according to the following command:
sudo update-java-alternatives --set /usr/lib/jvm/java-1.8.0-openjdk-amd64
open jdk once installed resides generally in your /usr/lib/java-6-openjdk
As usual you would need to set the JAVA_HOME, classpath and Path.
In ubuntu 11.04 there is a environment file available in /etc where you need to set all the three paths. And then you would need to restart your system for the changes to take effect..
Here is a site to help you around
http://aliolci.blogspot.com/2011/05/ubuntu-1104-set-new-environment.html
Once JDK installed set the JAVA_HOME in environment
sudo nano /etc/environment and add the line JAVA_HOME="/usr/lib/jvm/jdk-11.0.1/"
Add the configuration in .bashrc
sudo nano ~/.bashrc and add following lines
JAVA_HOME=/usr/lib/jvm/jdk-11.0.11/
PATH=$JAVA_HOME/bin:$PATH
refresh the new configuration with source ~/.bashrc
enter the command java -version and you can see the version installed on your machine
You can install the default Ubuntu(17.10) java from apt:
sudo apt install openjdk-8-jdk-headless
And it will set the PATH for you, if instead you need to install specific version of Java you can follow this YouTube
I installed java 11 in my Ubuntu 20.04. Setting up a JAVA_HOME for the same.
enter the this command to find out your ubuntu version --
swapnil#swapnil-vm:~$ lsb_release -d
Description: Ubuntu 20.04.3 LTS
enter this command to find out the location of your jvm --
swapnil#swapnil-vm:~$ whereis jvm
jvm: /usr/lib/jvm
open .bashrc in any editor of your choice --
nano .bashrc
add the following lines --
## setting JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
now you are good to go!!
open a new terminal and enter the command --
ehco $JAVA_HOME
Use the following lines to set the path variables in the /etc/environment
echo export JAVA_HOME=/path/to/java | sudo tee -a /etc/environment
echo export JRE_HOME=/path/to/jre | sudo tee -a /etc/environment
It should work.
Note:
You should reboot the system for changes to take effect.
Installation of Oracle Java:
Download the tarball (.tar file) from Oracle website
unzip it by sudo tar -xvpzf fileName -C /installation_folder_name
change the files permission and ownership
add the following two lines in /etc/profile
export JAVA_HOME=/home/abu/Java/jdk1.8.0_45/
export PATH=$JAVA_HOME/bin:$PATH
restart the machine and check by java -version and javac -version
First, check whether env var exists or not
echo $JAVA_HOME
if an env var exists with that name then the above command will return the env var Path. if it's return nothing then copy the env path first then execute below command. such as my java env path is /usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Beating my head on the wall with this. Using Mac 10.10 and my java -version works but mvn -version gives me
error: JAVA_HOME is not defined correctly. We cannot execute /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java
and that's not even my correct jdk. Here is what my bash_profile looks like:export
export JAVA_HOME=/Library/Java/JavaVirtualMachine/jdk1.8.0_25.jdk/Contents/Home
export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.3
export M2=$M2_HOME/bin
export MAVEN_OPTS=-Xms256m-Xmx512m
export PATH=$M2:$PATH
When I ran into this. I needed to restart the Terminal app. For it to pick up the new bash_profile changes.
That is (maybe) because iOS has a default Java installed and you have another Java VM that you downloaded yourself.
Try this, instead of defining the java home, let the OS use the one installed. In your profile, or on a shell script do this
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
Or whatever version of java you are using. For example, if you have java 6 and 7 installed, then running
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
will select java 7, whereas
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
will let you use Java 6.
NOTE the back ticks.
Please check the mvn.sh script, make sure you have not set the JAVA_HOME in it. Or redownload a maven package and unpack.
And check /etc/launchd.conf, have you set a JAVA_HOME in it?
install maven in mac/linux (mac in your case), should following this http://maven.apache.org/download.cgi section:Unix-based Operating Systems (Linux, Solaris and Mac OS X)
if your default terminal is bash (echo $SHELL will output something like: /bin/bash)
run echo $JAVA_HOME first before run mvn.
isaacdong-imac:~ isaac$ echo $SHELL /bin/bash
isaacdong-imac:~ isaac$
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
isaacdong-imac:~ isaac$
if your JAVA_HOME is empty, please note this:
export command ONLY current env of shell process. when you open a new shell or restart mac. export effect is gone.
see this: Set environment variables on Mac OS X Lion
I'm trying to find the location where JAVA_HOME env variale is set up and I tried to find in ~/.bash_profile , ~/.bashrc and /etc/profile but could not find the env variable.
But when I run echo $JAVA_HOME its gives out the value /usr/local/java.
Where else would the JAVA_HOME env variable.
BTW its a Red Hat Linux Server.
Try the file /etc/bash.bashrc, sometimes it is also used to initialise bash. If not, then try to find the word JAVA_HOME inside the files in /etc with grep -r BASH_HOME /etc
UPDATE
From man bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes
commands from the first one that exists and is readable.
and also:
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.
So the only file that you haven't had a look at is ~/bash_login.
Probably JAVA_HOME is not in any of these files and what happens is that one of these files call another script, in that case, you'll have to read line by line if one of these scripts is "loading" more scripts from other places
What about:
grep JAVA_HOME /etc/*
and:
grep JAVA_HOME ~/.*
Please also check variables been sourced in all startup scripts in /etc/ and also under home directory,
it will look like . path/filename or source path/filename
EX: . /etc/var
or
source /etc/var
There are few standard locations which environment variables can be set.
/etc/profile
shell scripts under /etc/profile.d
/etc/bashrc
~/.bashrc
~/.bash_profile
Recommended way is to put environment variables in a script under /etc/profile.d. You can grep this directory, if other locations failed.
grep -R "JAVA_HOME" /etc/profile.d/
This post can give more details on the topic.
I am using Linux Mint Cinnamon 14. I have set the $JAVA_HOME and $PATH environment variables in ~/.profile as follows:
export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=/home/aqeel/development/jdk/jdk1.6.0_35/bin:$PATH
I then did source ~/.profile to make the proper changes.
When I execute java -version command to check the active java version, it shows the default (already installed open-jdk) java version. How can I override the default open-jdk with the one I downloaded?
UPDATE:
which java says /usr/bin/java
$JAVA_HOME/bin/java -version says 'Permission Denied'
sudo $JAVA_HOME/bin/java -version (asks for password, then) says Command not found
but cd $JAVA_HOME/bin, and ls shows that it is right directory.
While it looks like your setup is correct, there are a few things to check:
The output of env - specifically PATH.
command -v java tells you what?
Is there a java executable in $JAVA_HOME\bin and does it have the execute bit set? If not chmod a+x java it.
I trust you have source'd your .profile after adding/changing the JAVA_HOME and PATH?
Also, you can help yourself in future maintenance of your JDK installation by writing this instead:
export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=$JAVA_HOME/bin:$PATH
Then you only need to update one env variable when you setup the JDK installation.
Finally, you may need to run hash -r to clear the Bash program cache. Other shells may need a similar command.
Cheers,
update-java-alternatives
The java executable is not found with your JAVA_HOME, it only depends on your PATH.
update-java-alternatives is a good way to manage it for the entire system is through:
update-java-alternatives -l
Sample output:
java-7-oracle 1 /usr/lib/jvm/java-7-oracle
java-8-oracle 2 /usr/lib/jvm/java-8-oracle
Choose one of the alternatives:
sudo update-java-alternatives -s java-7-oracle
Like update-alternatives, it works through symlink management. The advantage is that is manages symlinks to all the Java utilities at once: javac, java, javap, etc.
I am yet to see a JAVA_HOME effect on the JDK. So far, I have only seen it used in third-party tools, e.g. Maven.
If you want to use JDKs downloaded from Oracle's site, what worked for me (using Mint) is using update-alternatives:
I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
I ran:
sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1
Now you can execute sudo update-alternatives --config java and choose your java version.
This doesn't set the JAVA_HOME variable, which I wanted configured, so I just added it to my ~/.bashrc, including an export JAVA_HOME="/home/aqeel/development/jdk/jdk1.6.0_35" statement
Now, I had two JDKs downloaded (let's say the second has been extracted to /home/aqeel/development/jdk/jdk-10.0.1).
How can we change the JAVA_HOME dynamically based on the current java being used?
My solution is not very elegant, I'm pretty sure there are better options out there, but anyway:
To change the JAVA_HOME dynamically based on the chosen java alternative, I added this snippet to the ~/.bashrc:
export JAVA_HOME=$(update-alternatives --query java | grep Value: | awk -F'Value: ' '{print $2}' | awk -F'/bin/java' '{print $1}')
Finally (this is out of the scope) if you have to change the java version constantly, you might want to consider:
Adding an alias to your ~./bash_aliases:
alias change-java="sudo update-alternatives --config java"
(You might have to create the file and maybe uncomment the section related to this in ~/.bashrc)
$JAVA_HOME/bin/java -version says 'Permission Denied'
If you cannot access or run code, it which be ignored if added to your path. You need to make it accessible and runnable or get a copy of your own.
Do an
ls -ld $JAVA_HOME $JAVA_HOME/bin $JAVA_HOME/bin/java
to see why you cannot access or run this program,.
When it searches for java it looks from left to right in path entries which are separated by : so you need to add the path of latest jdk/bin directory before /usr/bin, so when it searches it'll find the latest one and stop searching further.
i.e. PATH=/usr/java/jdk_1.8/bin:/usr/bin:..... and so on.
then initialize user profile using command: source ~/.bash_profile
and check with: [which java]
you'll get the right one.
check available Java versions on your Linux system by using update-alternatives command:
$ sudo update-alternatives --display java
Now that there are suitable candidates to change to, you can switch the default Java version among available Java JREs by running the following command:
$ sudo update-alternatives --config java
When prompted, select the Java version you would like to use.1 or 2 or 3 or etc..
Now you can verify the default Java version changed as follows.
$ java -version
Try this:
export JAVA_HOME=put_here_your_java_home_path
type export PATH=$JAVA_HOME/bin:$PATH (ensure that $JAVA_HOME is the first element in PATH)
try java -version
Reason: there could be other PATH elements point to alternative java home. If you put first your preferred JAVA_HOME, the system will use this one.
There is an easy way, just remove the symbolic link from "/usr/bin". It will work.
Updating the ~/.profile or ~/.bash_profile does not work sometimes. I just deleted JDK 6 and sourced .bash_profile.
Try running this:
sudo rm -rd jdk1.6.0_* #it may not let you delete without sudo
Then, modify/add your JAVA_HOME and PATH variables.
source ~/.bash_profile #assuming you've updated $JAVA_HOME and $PATH
In Linux Mint 18 Cinnamon be sure to check /etc/profile.d/jdk_home.sh I renamed this file to jdk_home.sh.old and now my path does not keep getting overridden and I can call java -version and see Java 9 as expected. Even though I correctly selected Java 9 in update-aternatives --config java this jdk_home.sh file kept overriding the $PATH on boot-up.
I have a bash script on a Linux box that runs a Jar file. When logged in as a regular user I don't have permission to run the script, but it prints the following log:
*INFO * Using JVM found at /opt/jdk6/bin/java
When I try to use the script with Sudo though, it gives:
*ERROR* Unable to locate java, please make sure java is installed and JAVA_HOME set
I've set JAVA_HOME to the same path above — can see it with echo $JAVA_HOME & it's also set as an option within the script. I'm happy that the script isn't the issue — it's a default CQ5 control script & I'm using it on dozens of other boxes without issue. Just unsure what I'm doing wrong above & presume it's something I'm missing re Linux set-up?
When I run the sudo command, does it have access to the JAVA_HOME that I set up as myself?
By default, sudo will cleanup the environment of the spawned commands. Pass -E to keep it:
sudo -E env
Compare to:
sudo env
"sudo -E " didn't solve the problem when JAVA_HOME was not exported. And when it was exported, "sudo " without -E works the same.
So you can add export JAVA_HOME=.../jdk<version> in your .bash_profile and .bashrc file.
In case you wondered what's the difference of .bash_profile and .bashrc, .bash_profile is executed upon login (e.g., show some diagnostic/welcome information). .bash_rc is executed when you open a new terminal (e.g., shift-ctrl-T).
In order to run some commands for both cases, you can put it in .bashrc file, and let .bash_profile source .bashrc:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
You could always just pass it to java explicitly like this:
sudo java -Djava.home=$JAVA_HOME Test