Set PATH and JAVA_HOME in Fedora 25 - java

Please excuse what appears to be a question answered before, but if I read 10 different posts I find 20 different responses.
I just installed Fedora 25. I am going to be learning Java development and need to set up my environment. I will be using openjdk and have installed java-1.8.0-openjdk-devel. I will also be using Maven.
Now I need to set my $PATH and $JAVA_HOME variables.
I tried the answer found here (Fedora OpenJDK Set JAVA_HOME)
but after adding to my .bashrc
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
and then testing, I get what appears to me to be the wrong answer because I do not think there should be a /jre/ on the end
> echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-1.b14.fc25.x86_64/jre/
So can I please ask what I should set JAVA_HOME to so that I do not need to update it with every openjdk update?
After that I think PATH is just
export PATH=$JAVA_HOME/bin:$PATH
this seems like such a straightforward thing to do yet it seems to me there is much confusion. thx
PS
also, is .bashrc even the correct place? because I see here (https://askubuntu.com/questions/175514/how-to-set-java-home-for-java) that /etc/environment would be more appropriate) thx

I suggest create an alias command, modifying bashrc and set JAVA_HOME
into bash_profile, this:
Create command in the bashrc:
alias set-java='sudo alternatives --config java;export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::");source ~/.bash_profile'
Save and execute: source ~/.bashrc
Create generic JAVA_HOME in bash_profile:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
PATH=$JAVA_HOME/bin:$PATH
Execute the alias command

Set JAVA_HOME :
echo "JAVA_HOME=/etc/alternatives/jre" >> ~/.profile
source ~/.profile
echo $JAVA_HOME

I put the following in ~/.profile, not ~/.bashrc and I think it's the same on Fedora (I used CentOS at work and do the same):
export JDK_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export JAVA_HOME=$JDK_HOME/jre
I have to either sign out or reboot after changing .profile.
On Ubuntu, java only changed from 7 to 8 a few years ago. I don't know why Fedora would want to put an exact version number on the java folder. You might complain?
If Java moves around too much for you on Fedora, use a symlink. The symlink takes affect right away, without logging out or rebooting. On my laptop, I encrypt my home folder, so for extra speed, I put most third party tools in a /tools/ folder and give myself account access to it (sudo chown -R myself.myself /tools/ where myself is my user ID). That's where I put my maven install. Tomcat uses a different directory name for each release, so I make a symlink like so:
cd /tools
ln -s apache-tomcat-8.0.38 latest-tomcat
Then in ~/.profile (CATALINA means TOMCAT in this example - don't ask me why):
# My un-encrypted Tools folder
export TOOLS=/tools
# Tomcat
export CATALINA_HOME=$TOOLS/latest-tomcat
# Maven
export M2_HOME=$TOOLS/apache-maven-3.3.9/
export M2="$M2_HOME"bin
export PATH=$PATH:$M2
When I upgrade tomcat:
cd /tools
rm -f latest-tomcat
ln -s apache-tomcat-8.0.39 latest-tomcat
You can use the same technique to make a /usr/lib/jvm/latest-java. In fact, if you install Oracle Java (not necessarily recommended), it does exactly that, for this reason.
Yes, what you said about the path looks correct to me. I don't put java in my path, preferring to use $JDK_HOME, $JAVA_HOME, or just calling maven. Part of that is for security. Part of it is to feel like I always know which version of Java I'm using.
P.S. To me, the ln -s syntax always looks as if the arguments are reversed. The actual folder name goes first, followed by your desired alias/link.

OpenJDK bundled with Fedora dosen't work for me. For example IntelliJ can't use this version. In my opinion, best option is to install Oracle JDK RPM from Oracle.
JDK location:
/usr/java/latest/
To manipilate JRE use command below:
sudo alternatives --config java

So far, this gets Android Studio running using the Fedora provided OpenJDK and respects the distribution provided materials without a bunch of otherwise unmanaged symlinking:
vim ~/.bash_profile
Add the following, or replace any existing examples that look similar to it:
export JAVA_HOME=$(readlink -f /usr/bin/java | cut -d/ -f1-6)
The command basically asks for the physical path that the symlink /usr/bin/java points to (configured by alternatives in Fedora), and then says "take the first 6 fields given to you by splitting on the / character."
Note that your $PATH will already be set up correctly for use with the Fedora provided OpenJDK.
In practice, this ends up looking like:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-8.fc28.x86_64/jre
This assumes you have at least one of the OpenJDK packages Fedora ships. 25 probably only shipped 1.8, 28 ships 1.8, 10.0.2, and 11.0.1. (if you're reading this and still using 25, it's probably time to upgrade.)
Install one of the java-*-openjdk-headless packages available to you (or one of the Oracle provided ones), and configure which one to use using:
sudo alternatives --config java
The default on my Fedora 28 install was to use OpenJDK 1.8.

Sadly I don't have Fedora available for me, so I can't investigate how Java is usually installed, but why you don't try to replace /usr/bin/java in your expression with something like /usr/bin/javac? The actual path to javac link you can get with
which javac
And read the real path with readlink. Obviously, it works if JDK is installed (not JRE).
About updating, most of Linux distros create a symbolic link like /usr/lib/jvm which is set to the correct directory and update in case of new version. For me better solution it to put this path in your bashrc.
And yes, bashrc is usually enough if you don't need JAVA_HOME to be visible to other OS users.

Maybe adding a tr could work? export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::" | tr -d "/jre/") ?
Also, setting it in your env var will make the var independent from the sh your using. That's a great way to store It.

Related

Error: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-8-oracle/jre/bin/java when i run gradle command in terminal

I am working in Ubuntu 16.04. I need to install gradle and the gradle is installed when i checked with sudo apt list --installed command but when i use gradle -version command it shows the following error,
JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-8-oracle/jre/bin/java
In sudo vim /etc/environment file,
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
http_proxy="http://username:password#IP:port no/"
https_proxy="https://IP:port no/"
ftp_proxy="ftp://IP:port no/"
I don't know where i made mistakes. Please help me.
Thanks.
On a 64bit openSuse 64 42.1 box;
readlink -f $(which java)
provided;
/usr/lib64/jvm/java-1.8.0-openjdk-1.8.0/jre/bin/java
But;
export JAVA_HOME=/usr/lib64/jvm/jre-1.8.0-openjdk
is the path that worked and allowed java emulator to run.
So i think we have to manually browse our file system and see what path to choose.
Today I faced this problem. I am using the default java that comes with your linux distro (so in my case, linux mint).
$ whereis java
This command gave me
java: /usr/bin/java /usr/share/java
So, I opened /user/bin. There was a link to Java. I right clicked it and selected follow original link. This lead me to /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
So now that I know where this java is, I opened my .bashrc file, and edited the JAVA_HOME.
So for my case,
## My Custom variables
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
This solved the problem.
Now if you are using some other java (say you downloaded from oracle and extracted the zip file ...), then you have to add that location. So for example, if your java is in /home/user/.sdkman/candidates/java/current, then
export JAVA_HOME=/home/user/.sdkman/candidates/java/current
export PATH=$JAVA_HOME/bin:$PATH
I see a mismatch. In your enviornment file the JAVA_HOME is set to "/usr/lib/jvm/java-8-openjdk-amd64/" and your mentioned that the error that you got relates to the JAVA_HOME as "/usr/lib/jvm/java-8-oracle/jre/bin/java"
If you JAVA is really installed in /usr/lib/jvm/java-8-oracle directory, then you need to ensure that the JAVA_HOME is set to that directory. And also your PATH reflects $JAVA_HOME/bin in it.
I typically install Oracle JDK/JRE separately in a separate directory such as /usr/local/jdk1.8.0 etc.
check the jvm installtion folder from Files
eg : /usr/lib/jvm/java-12-oracle
then in terminal run sudo nano /etc/environment and add the line
JAVA_HOME="/usr/lib/jvm/java-12-oracle"
Then open terminal and run
export JAVA_HOME="/usr/lib/jvm/java-12-oracle"

How to install java locally - no root - on linux, if possible?

I need java 1.7 and server has only got 1.6. I have no root privileges. I tried to google out something but it seems like nobody was doing it. Can I somehow compile it or get ready binaries so I could put those into my PATH. Could you help? System is Redhat.
It is quite easy...
Download the JDK as a tarball from Oracle (a simple google search will yield the link).
Unzip it somewhere in your $HOME (for instance, $HOME/jdk).
Set JAVA_HOME to the path of the root JDK install; then prepend $JAVA_HOME/bin to your PATH.
And off you go.
Here I have a particular setting insofar as I run three different major versions of the JDK: 6, 7, 8. For instance, here is my source file for setting the current shell to use Java 8:
$ cat ~/.jdk/8
export JAVA_HOME=/opt/sunjdk/1.8/current
export PATH="$JAVA_HOME/bin:$PATH"
And in /opt/sunjdk/1.8 (given that /opt/sunjdk is writable by my user hence I don't need to be root):
$ ls -l /opt/sunjdk/1.8/* -d
lrwxrwxrwx 1 fge fge 11 Oct 30 10:09 /opt/sunjdk/1.8/current -> jdk1.8.0_25
drwxr-xr-x 1 fge fge 274 Mar 18 2014 /opt/sunjdk/1.8/jdk1.8.0_05
drwxr-xr-x 1 fge fge 274 Sep 18 02:44 /opt/sunjdk/1.8/jdk1.8.0_25
(and yes, I was root to begin with so as to grant write permissions for /opt/sunjdk to "my" user; if you have no such liberty, just create another directory in your home)
Oracle offers JRE and JDK also as *.tar.gz for Linux. I usually had success just downloading such a package, untarring/unzipping it (tar -xzvf jdk-8u25.tar.gz) and then running it, using the absolute path.
I was able to accomplish this using conda.
Conda is an open-source package-manager by Anaconda, that according to the website:
You do not need administrative or root permissions to install Anaconda if you select a user-writable install location.
You can search the package repo from a browser at anaconda.org or command line, for example here are the results for JDK.
For Linux, you would download this installer. Here is a command line that will start the installer for convenience:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && bash Miniconda3-latest-Linux-x86_64.sh
Once conda is installed, you can install packages. For example, to install the JetBrains Runtime OpenJDK build:
conda install -c anaconda openjdk
Other builds may be available from other channels in the repository.
The instructions above should give a working install, but the Getting started guide is a good place to get started. Conda uses the concept of environments to help manage versions and paths in a fairly simple and straightforward manner.
I hope this helps someone.
export JAVA_HOME=/opt/sunjdk/1.8/current
export PATH="$JAVA_HOME/bin:$PATH"
For me this option only worked when I changed linux to use bash instead ksh. I don't know if this is some kind of configuration in my company, but when I tried to run via ksh using "set" command instead "export" to define path, It was set correctly with the path of my new Java installation, but when I typed which java the old version was showed. But, when I executed bash, and typed the "export", it worked. So, if someone have the same problem to configure it using set command, try to use bash with export command. I am using Redhat 6.2.

How to set JAVA_HOME environment variable on Mac OS X 10.9?

I just purchased a brand new MacBook Pro.
This is my first MAC ever and I'm still trying to get the hang of navigating my way around.
Anyway, I'm also new to Java and I've been practicing on my Windows PC before it permanently died.
Now that I'm on this MAC, I installed my JDK and now I need to set the JAVA_HOME environment variable.
I have no idea what to do.
I tried following some of these guides and didn't get very far.
Mkyong.com : How to set JAVA_HOME variable in Mac OSX
YouTube : How to set environment variables on mac, linux, solaris, rhel
YouTube : How to Set Environment Variables in Mac
I was able to locate the terminal and I think I created some multiple files. I'm getting messages like this:
(1) Another program may be editing the same file.
If this is the case, be careful not to end up with two
different instances of the same file when making changes.
Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r /Users/Erwin/.bash_profile"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/Users/Erwin/.bash_profile.sw p"
to avoid this message.
Can somebody tell how to set Java in Mac OSX environment step by step?
If you're using bash, all you have to do is:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
If you're using zsh (which probably means you're running macOS Catalina or newer), then it should instead be:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
In either case, restart your shell.
If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v flag to java_home like so:
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
I just spent 2 hours setting this variable. The other answers did not work properly for me. I'm using macOS Catalina 10.15.4.
First, find your actual Java SDK Home directory:
/usr/libexec/java_home
Manually navigate there to make sure you don't have any mistakes due to incorrect versions, etc. For me, this was:
/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Next, edit your terminal's profile. If you're using zsh, this will be:
vim ~/.zshrc
If you're not using zsh, this will be:
vim ~/.bash_profile
Inside, add the following new line anywhere in the file:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Restart your terminal app (or source ~/.bash_profile), and it should work properly.
I did it by putting
export JAVA_HOME=`/usr/libexec/java_home`
(backtics) in my .bashrc. See my comment on Adrian's answer.
Set $JAVA_HOME environment variable on latest or older Mac OSX.
Download & Install install JDK
First, install JDK
Open terminal check java version
$ java -version
Set JAVA_HOME environment variable
Open .zprofile file
$ open -t .zprofile
Or create . zprofile file
$ open -t .zprofile
write in .zprofile
export JAVA_HOME=$(/usr/libexec/java_home)
Save .zprofile and close the bash file & then write in the terminal for work perfectly.
$ source .zprofile
Setup test in terminal
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home, just export $JAVA_HOME in file ~/. bash_profile or ~/.profile.
Open the terminal and run the below command.
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
save and exit from vim editor, then run the source command on .bash_profile
$ source .bash_profile
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
It is recommended to check default terminal shell before set JAVA_HOME environment variable, via following commands:
$ echo $SHELL
/bin/bash
If your default terminal is /bin/bash (Bash), then you should use #Adrian Petrescu method.
If your default terminal is /bin/zsh (Z Shell), then you should set these environment variable in ~/.zshenv file with following contents:
export JAVA_HOME="$(/usr/libexec/java_home)"
Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.
If you are using Zsh, then try to add this line in ~/.zshrc file & restart terminal.
export JAVA_HOME=$(/usr/libexec/java_home)
I got it working by adding to ~/.profile. Somehow after updating to El Capitan beta, it didnt work even though JAVA_HOME was defined in .bash_profile.
If there are any El Capitan beta users, try adding to .profile
Since I'm using openjdk managed with sdkman, I added
sudo ln -sfn /path/to/my/installed/jdk/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
Adding this to your system lets java_home recognize your installed version of Java even when its not installed via standard packages
I checked my /Library/Java/JavaVirtualMachines/
directory and found the version to be jdk1.8.0_321.jdk/Contents/Home
and added this directly to my .bash_profile:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home
but it's still complaining after sourcing the .bash_profile:The operation couldn’t be completed. Unable to locate a Java Runtime that supports apt.
In the latest Mac, you have to add the Set $JAVA_HOME environment variable in .zprofile. Here, we simple way to open it. Press ⌘ + Shift + . from keyboard. Just open it and add the $JAVA_HOME environment variable as explained here:
JAVA 11 via Homebrew - tested on macos Ventura 2022
.zshrc
export JAVA_HOME=/opt/homebrew/opt/openjdk#11/libexec/openjdk.jdk/Contents/Home
For Mac M1
Download & Install install JDK
Open terminal check java version
java -version
Now create a file
touch .zprofile
Open the file
open -t .zprofile
Add the below line
export JAVA_HOME=$(/usr/libexec/java_home)
I resolved it on macOS Monterey by using the option provided by Google
Under Gradle JDK, choose the Embedded JDK option.
https://developer.android.com/studio/intro/studio-config#:~:text=A%20copy%20of%20the%20latest,use%20for%20your%20Android%20projects.
I'm able to solve this issue by setting JAVA_HOME in .bash_profile file
export JAVA_HOME=/usr/local/opt/openjdk#17
Note:
I installed openjdk version 17 using 'brew'. I got this location from brew console. I'm using 'bash' instead of 'zsh' in my mac.
Open Terminal.
Confirm you have JDK by typing “which java”. ...
Check you have the needed version of Java, by typing
“java -version”.
Set JAVA_HOME using this command in Terminal: export
JAVA_HOME=/Library/Java/Home.
echo $JAVA_HOME on Terminal to confirm
the path.
More simply on a mac terminal with a modern OSX
$ vim ~/.zshrc
Type "a" to being editing, and then paste (ctrl + v):
$ JAVA_HOME=/usr/libexec/java_home
then hit "escape" and type exactly ":wq" in order to write to the file and quit vim mode.
Finally, when out of vim mode and back in your terminal, type
$ source ~/.zshrc
This will refresh so that your terminal is aware of the changes.
IMPORTANT * If you don't "source" the file, you won't see the changes in this terminal session.
Check the changes by typing
$ echo $JAVA_HOME
and you should see /usr/libexec/java_home
Quick Guide for M1
Add java sdk into your m1
check version
java --version
Get all java versions installed in ur mac
/usr/libexec/java_home -V
Execute for Java path from library
/usr/libexec/java_home
(specify java version if you have multiple version, In my case -v17.0.5
/usr/libexec/java_home -v17.0.5
Mac>User>'YourUserName/Home'>.zshrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.5.jdk/Contents/Home

Ant unable to launch, giving a main class error

I'm running Elementary OS (Ubuntu 12 based), and I'm having issues running apache ant. It was working earlier before a restart, so I'm not sure what would've changed.
I've defined environment variables in /etc/environment as follows:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$ANT_HOME/bin"
JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-i386"
ANT_HOME="/opt/ant"
ANT_OPTS="-Xms256M -Xmx512M"
So my Java and Ant environment variables should be set. I'm trying to deploy with ant, with 'ant clean deploy', but I get an error in my terminal:
Error: Could not find or load main class org.apache.tools.ant.launch.Launcher
I've tried "source /etc/environment". Running 'echo $ANT_HOME' shows the correct path. I've tried moving ant to a different location and resetting the variables. Nothing. I'm kind of lost. Please help!
you should define $ANT_HOME before using it in your $PATH
JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-i386"
ANT_HOME="/opt/ant"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$ANT_HOME/bin"
double check that you have ant-launcher.jar under $ANT_HOME/lib
As others have stated, it looks like ANT_HOME isn't being set before you declare it in your path.
I take a slightly different approach to these things. Instead of constantly updating my PATH, I usually set my path to something fairly simple:
PATH="/usr/local/bin:/bin:/usr/bin/:$HOME/bin"
Then, I make hard links to various binaries into my /usr/local/bin directory.
$ cd $ANT_HOME/bin # All the Ant binaries
> for binary in *
> do
> sudo ln -s $PWD/$binary /usr/local/bin
> done
Now, I don't have to add $ANT_HOME/bin to my $PATH. Even better, if I include /usr/local/bin before /usr/bin and /bin, I am picking the binary in /usr/local/bin first. This way, I can ensure I run the version I installed over the default. For example, my machine comes with Ant 1.7 in /usr/bin/ant but I want to use Ant 1.9.1. I can install Ant 1.9.1, and that will be my default version.
I had same error when install ant with npm install. When I tried install from official repository throw pacman -S apache-ant(apt-get install apache-ant -- for Debian/Ubuntu) it's start working proper for me.
I'm experiencing this bug with jdk 1.8. But, I came across this RHEL 6.5 bug for OpenJDK 1.8 which may be related:
https://bugzilla.redhat.com/show_bug.cgi?id=1149605
Essentially, it's fixed in jpackage-utils-1.7.5-3.13 which is included with RHEL 5.7.
The problem, as Javier Ramirez said in the bug comments:
Your script /usr/share/java-utils/java-functions has problems with
"openjdk version" because it expects "java version" as Java 7 does.
$ mkdir /usr/share/java-1.8.0
$ mkdir /usr/lib/java-1.8.0
$ diff /usr/share/java-utils/java-functions.orig /usr/share/java-utils/java-functions
149,150c149,150
< -e '/java \(full \)*version "/s/'$re'/<<<\1>>>/' \
< -e '/java \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*/\1/p')
---
> -e '/[java|openjdk] \(full \)*version "/s/'$re'/<<<\1>>>/' \
> -e '/[java|openjdk] \(full \)*version "/s/.*<<<\([^>]\{1,\}\)>>>.*/\1/p')
------
I am using RHEL which comes with ant (in /usr/bin/ant) and the ant libraries in
/usr/share/ant. As suggest above, ant -version gave the "could not find" error.
I installed my own version of ant (in /home/Ant since I have a lot of space
in /home) and put ANT_HOME in my .bash_profile and $ANT_HOME/bin in my PATH
and $ANT_HOME and $ANT_HOME/bin (for good measure) in my CLASSPATH. Then ant
worked (when used as myself, not as root).

What is the correct target for the JAVA_HOME environment variable for a Linux OpenJDK Debian-based distribution?

In Windows, JAVA_HOME must point to the JDK installation folder (so that JAVA_HOME/bin contains all executables and JAVA_HOME/libs contains all default jar libraries).
If I download Sun's JDK bundle and installs it in Linux, it is the same procedure.
However, I need to use Kubuntu's default OpenJDK package. The problem is that all executables are placed in /usr/bin. But the jars are placed in /usr/share/java. Since they are not under the same JAVA_HOME folder I'm having trouble with Grails and maybe there will be trouble with other applications that expect the standard Java structure.
If I use:
JAVA_HOME=/usr
All applications and scripts that want to use any Java executable can use the standard procedure call $JAVA_HOME/bin/executable. However, since the jars are in a different place, they are not always found (example: in grails I'm getting ClassDefNotFound for native2ascii).
On the other hand, if I use:
JAVA_HOME=/usr/share/java
None of the Java executables (java, javac, etc.) can be found.
So, what is the correct way of handling the JAVA_HOME variable in a Debian-based Linux?
Thanks for your help,
Luis
What finally worked for me (Grails now works smoothly) is doing almost like Steve B. has pointed out:
JAVA_HOME=/usr/lib/jvm/default-java
This way if the user changes the default JDK for the system, JAVA_HOME still works.
default-java is a symlink to the current JVM.
If you use alternatives to manage multiple java versions, you can set the JAVA_HOME based on the symlinked java (or javac) like this:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
The standard Ubuntu install seems to put the various Java versions in /usr/lib/jvm. The javac, java you find in your path will softlink to this.
There's no issue with installing your own Java version anywhere you like, as long as you set the JAVA_HOME environment variable and make sure to have the new Java bin on your path.
A simple way to do this is to have the Java home exist as a softlink, so that if you want to upgrade or switch versions you only have to change the directory that this points to - e.g.:
/usr/bin/java --> /opt/jdk/bin/java,
/opt/jdk --> /opt/jdk1.6.011
I modified one of the solution above and this seems to work no matter where java is installed, as long it's in your PATH.
JAVA_HOME=$( j=$( readlink -f $(which java) ) ; echo ${j%%/bin/java} )
I usually don't have any JAVA_HOME environment variable. Java can set it up itself. Inside java java.home system property should be available.
Try setting the JAVA_LIB variable also.
If you have issues with JAR files not being found I would also ensure your CLASSPATH is set to include the location of those files. I do find however that the CLASSPATH often needs to be set differently for different programs and often ends up being something to set uniquely for individual programs.
Updated answer that will solve your problem and also just a general good how-to for installing Oracle Java 7 on Ubuntu can be found here: http://www.wikihow.com/Install-Oracle-Java-on-Ubuntu-Linux
I've discovered similar problems with the openjdk-6-jre and openjdk-6-jre-headless packages in Ubuntu.
My problem was solved by purging the openjdk-6-jre and openjdk-6-jre-headless packages and re-installing. The alternatives are only updated on a fresh install of the openjdk-6-jre and openjdk-6-jre-headless packages.
Below is a sample of installing after purging:
aptitude purge openjdk-6-jre openjdk-6-jre-headless # to ensure no configuration exists
aptitude install --without-recommends openjdk-6-jre # Installing without some extras
Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading extended state information
Initializing package states... Done
The following NEW packages will be installed:
ca-certificates-java{a} java-common{a} libavahi-client3{a} libavahi-common-data{a} libavahi-common3{a} libcups2{a} libflac8{a} libgif4{a} libnspr4-0d{a} libnss3-1d{a} libogg0{a} libpulse0{a} libsndfile1{a} libvorbis0a{a} libvorbisenc2{a} libxi6{a} libxtst6{a}
openjdk-6-jre openjdk-6-jre-headless{a} openjdk-6-jre-lib{a} tzdata-java{a}
The following packages are RECOMMENDED but will NOT be installed:
icedtea-6-jre-cacao icedtea-netx ttf-dejavu-extra
0 packages upgraded, 21 newly installed, 0 to remove and 119 not upgraded.
Need to get 0B/34.5MB of archives. After unpacking 97.6MB will be used.
Do you want to continue? [Y/n/?]
Writing extended state information... Done
Selecting previously deselected package openjdk-6-jre-lib.
(Reading database ... 62267 files and directories currently installed.)
Unpacking openjdk-6-jre-lib (from .../openjdk-6-jre-lib_6b24-1.11.5-0ubuntu1~10.04.2_all.deb) ...
...
Processing triggers for man-db ...
Setting up tzdata-java (2012e-0ubuntu0.10.04) ...
...
Setting up openjdk-6-jre-headless (6b24-1.11.5-0ubuntu1~10.04.2) ...
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/java to provide /usr/bin/java (java) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/keytool to provide /usr/bin/keytool (keytool) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/pack200 to provide /usr/bin/pack200 (pack200) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/rmid to provide /usr/bin/rmid (rmid) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/rmiregistry to provide /usr/bin/rmiregistry (rmiregistry) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/unpack200 to provide /usr/bin/unpack200 (unpack200) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/orbd to provide /usr/bin/orbd (orbd) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/servertool to provide /usr/bin/servertool (servertool) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/tnameserv to provide /usr/bin/tnameserv (tnameserv) in auto mode.
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/lib/jexec to provide /usr/bin/jexec (jexec) in auto mode.
Setting up openjdk-6-jre (6b24-1.11.5-0ubuntu1~10.04.2) ...
update-alternatives: using /usr/lib/jvm/java-6-openjdk/jre/bin/policytool to provide /usr/bin/policytool (policytool) in auto mode.
...
You can see above that update-alternatives is run to set up links for the various Java binaries.
After this install, there are also links in /usr/bin, links in /etc/alternatives, and files for each binary in /var/lib/dpkg/alternatives.
ls -l /usr/bin/java /etc/alternatives/java /var/lib/dpkg/alternatives/java
lrwxrwxrwx 1 root root 40 2013-01-16 14:44 /etc/alternatives/java -> /usr/lib/jvm/java-6-openjdk/jre/bin/java
lrwxrwxrwx 1 root root 22 2013-01-16 14:44 /usr/bin/java -> /etc/alternatives/java
-rw-r--r-- 1 root root 158 2013-01-16 14:44 /var/lib/dpkg/alternatives/java
Let's contast this with installing without purging.
aptitude remove openjdk-6-jre
aptitude install --without-recommends openjdk-6-jre
Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading extended state information
Initializing package states... Done
The following NEW packages will be installed:
ca-certificates-java{a} java-common{a} libavahi-client3{a} libavahi-common-data{a} libavahi-common3{a} libcups2{a} libflac8{a} libgif4{a} libnspr4-0d{a} libnss3-1d{a} libogg0{a} libpulse0{a} libsndfile1{a} libvorbis0a{a} libvorbisenc2{a} libxi6{a} libxtst6{a}
openjdk-6-jre openjdk-6-jre-headless{a} openjdk-6-jre-lib{a} tzdata-java{a}
The following packages are RECOMMENDED but will NOT be installed:
icedtea-6-jre-cacao icedtea-netx ttf-dejavu-extra
0 packages upgraded, 21 newly installed, 0 to remove and 119 not upgraded.
Need to get 0B/34.5MB of archives. After unpacking 97.6MB will be used.
Do you want to continue? [Y/n/?]
Writing extended state information... Done
Selecting previously deselected package openjdk-6-jre-lib.
(Reading database ... 62293 files and directories currently installed.)
Unpacking openjdk-6-jre-lib (from .../openjdk-6-jre-lib_6b24-1.11.5-0ubuntu1~10.04.2_all.deb) ...
...
Processing triggers for man-db ...
...
Setting up openjdk-6-jre-headless (6b24-1.11.5-0ubuntu1~10.04.2) ...
Setting up openjdk-6-jre (6b24-1.11.5-0ubuntu1~10.04.2) ...
...
As you see, update-alternatives is not triggered.
After this install, there are no files for the Java binaries in /var/lib/dpkg/alternatives, no links in /etc/alternatives, and no links in /usr/bin.
The removal of the files in /var/lib/dpkg/alternatives also breaks update-java-alternatives.
Ubuntu 12.04 this works...
JAVA_HOME=/usr/lib/jvm/java-6-openjdk-i386/jre
As an update for fedora user , alternatives set current java directory to
/usr/java/default
so you have to set your JAVA_HOME to /usr/java/default to always have alternatives curent selection in your classpath
HTH !
I always tend to set the JAVA_HOME according to the /usr/bin/java.
JAVA_HOME="$(dirname -- "$(dirname -- "$(readlink -f /usr/bin/java)")")"
This way, both alternatives point to the same location
As far as I remember, I used the update-java-alternatives script instead of the update-alternatives. And it did set the JAVA_HOME for me correctly.
Please see what the update-alternatives command does (it has a nice man...).
Shortly - what happens when you have java-sun-1.4 and java-opensouce-1.0 ... which one takes "java"? It debian "/usr/bin/java" is symbolic link and "/usr/bin/java-sun-1.4" is an alternative to "/usr/bin/java"
Edit:
As Richard said, update-alternatives is not enough. You actually need to use update-java-alternatives. More info at:
https://help.ubuntu.com/community/Java
My correct target has always been to download it from Sun and just install it that way. Then you know exactly what directory everything goes in.
But if you'd prefer to stick with the odd way that Debian installs it, my best guess would be the parent directory just above where the java and javac binaries are located.
(since when you specify it in your path it's $JAVA_HOME/bin)
(So in your case it would be ... $JAVA_HOME/share and $JAVA_HOME would be /usr ?)
Eh, that doesn't sound right...
I'm interested to hear the answer to this too!

Categories

Resources