Expanation for the inconsitency regarding java installtion paths in unix? - java

I installed java in my system around 6 months. I blindly followed this document's instruction here. I downloaded the jdk, unpacked it, set the paths Java_Home, Path according to the instructions in the doc.
Today I wanted to find out, which java is installed in my system and where. So, I looked it up online and ran this command
readlink -f $(which java)
It says
/usr/local/java/jre1.7.0_40/bin/java
But when I check Java_Home variable, it says
/usr/lib/jvm/jdk1.7.0_40
Here both paths refer to jdk 1.7, but they are different. One is in /usr/lib/jvm and other is in /user/local/java.
I think, due to some confusion, I set the java_home incorrectly. I must have unpakced jdk at couple of places. And, while setting up the java_home, I took incorrect path. Either that, or it is some linkage between two locations, which occurred due to some command I ran which I don't know of.
Anyway, I can run java programs correctly and run eclipse etc, so everything is fine and because of that I never noticed that.
But, I would like to know whether I need to fix the java_home variable to ensure that both of above commands return the same value. And, if it is not necessary, why is this set up working fine when 'readlink -f $(which java)' and java_home return different path.

readlink -f $(which java) just tells you which file is executed when you type java in your shell. JAVA_HOME is an environment variable used by some programs to locate java. Since both variables point to a 1.7 jre/jdk, it should not matter much that they differ. Programs using JAVA_HOME will use the JDK java and programs just using java command line will use the JRE version.
If you really need to change them to point to the same java, then you should NOT change your JAVA_HOME but instead your java symlink, because pointing to a JDK is usually better than pointing to a JRE, since some programs mandate that JAVA_HOME must point to a JDK (for example, because those programs need javac which is not included in the JRE).

Your JAVA_HOME should match your chosen version of Java. I set my JAVA_HOME with the following:
export JAVA_HOME=`readlink -f /usr/bin/javac | sed 's|/bin/javac||g'`
I use the Debain/Ubuntu update-java-alternatives command to choose which version of Java I use. It makes sure that it doesn't just update /usr/bin/java, but also all the other Java commands like javac and javadoc.
sudo update-java-alternatives --set java-7-oracle
You can get a list of the Java distributions installed on your system:
update-java-alternatives -l
Most programs don't rely on both JAVA_HOME and which java is in your path. Typically they rely on one or the other, but not both. So for any given application, it will probably run, but different applications may run using different versions of Java. In my experience, most installed java applications will use /usr/bin/java and most development environments will use JAVA_HOME.
Specifying both JAVA_HOME and setting /usr/bin/java correctly ensure that almost all Java application run with your preferred (and the latest) version of Java.
There are some notable exceptions to that rule, however. Some programs that use run under Java have their own configuration to choose which version of Java they are using.
For example I run the tomcat7 server, and I have to have the configuration file /usr/share/tomcat7/bin/setenv.sh that sets the JAVA_HOME directory for just that application:
JAVA_HOME=`readlink -f /usr/bin/javac | sed 's|/bin/javac||g'`
JAVA_OPTS="-Xms256m -Xmx2048m -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:MaxPermSize=512M -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
Other programs like Eclipse completely ignore JAVA_HOME. It has its own algorithm for figuring out which version of Java it will run under and has a -vm command line option that allows you to specify your preference.

It's two different things.
The first tells you where the JRE (Java Runtime Environment), the java binary is located. You need this to run JAR files.
JAVA_HOME tells you where the JDK (Java Development Kit), the javac binary is located. You need this to compile and package JAR files.

Related

What is the recommended approach to switch between java 7 and java 8 in Linux (Ubuntu)?

I have a linux box (Ubuntu server 14.04). I installed jdk7 via apt-get and Oracles Java 8 manually by extracting the tarball.
How can I switch between the Java versions from a bash session?
I suppose it should be done via "alternatives", but the details are not clear to me.
Switching java is more than calling one of the two java executables. There are other binaries (e.g. javac) and some tools refer to different files within the java installation directories (think of cacerts for example).
An optimal solution would simulate the effects of having only one of the two versions installed at any time.
Example: Using maven it is possible to set JAVA_HOME, but if some process started by maven calls java, JAVA_HOME is ignored.
I think Debian has Java 8 meanwhile. Does anybody know how they deal with this issue?
Is the alternatives mechanism only usable for individual binaries or can it be used for a complete "suite", too?
You can use this command to get a list of installed jdk's and easily choose one you would like to use:
sudo update-alternatives --config javac
I'm not sure that I fully understand the question, but you could either use an environment variable in your bash session that holds the path to your java executable or you could put a symbolic link somewhere for the same purpose.
For example
export JAVA_EXEC=/usr/lib/jvm/java-8-oracle/jre/bin/java
$JAVA_EXEC -version
$JAVA_EXEC -jar cooljar.jar
Or with symlink, like the "alternatives" you mentioned
ln -s /usr/lib/jvm/java-8-oracle/jre/bin/java /usr/local/bin/java
/usr/local/bin/java -version
ln -s "${SOME_JAVA_PATH}" /usr/local/bin/java
/usr/local/bin/java -version

Change java version (Mac)

I have 2 java versions on my computer:
/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
The 1.6.0 is set to default. How can I make my java programs to run 1.7?
Tried to add:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home
to my .zshrc file. But this seems to only change the path for my terminals java command.
Also tried to change the HOME symlink like this:
cd /Library/Java
mv Home Home-1.6
ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/ Home
This had no effect at all.
Also tried java changer software:
http://www.guigarage.com/2013/02/change-java-version-on-mac-os/
But no effect.
Any idea how to start java programs like .app and .jar files with the 1.7 version by just clicking on them?
I believe OS X (at least 10.8) uses the following paths:
JRE: /System/Library/Frameworks/JavaVM.framework/Versions/Current
JDK: /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
Those are symlinks, which you can update to point to your 1.7 installation.
You can verify this fairly easily:
a) run which java to check which java executable is being executed. In theory, that should be /usr/bin/java.
b) run ls -la on your java executable, which should tell you where it points (/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
on my machine).
I think this should sort your .jar execution issue. If your Java application is wrapped in a .app, I believe it's a bit more complex: if memory serves, the version of java used will depend on the JavaApplicationStub being used by the .app.
$ edit ~/.profile
#Java 1.8
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
export PATH=${PATH}:${JAVA_HOME}
$ java -version
java version "1.8.0_20-ea"
here are the steps:
http://ukitech.blogspot.com/2014/04/switching-version-of-java-on-mac.html
You can always add into your profile both on Mac or Linux. Just create if doesn't exist ~/.profile file and there this line:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home
This should work .zshrc as well as .bash_profile are loaded only when terminal window is openned and profile when your graphical environment starts up.

JAVA_HOME and java -version

I'm using a Windows .bat script and I set JAVA_HOME as C:/Program Files/Java/jdk1.6.0_32
when I do a java -version, it still shows the 1.3
How can I fix this? What am I doing wrong?
For me the issue was in my PATH variable, C:\ProgramData\Oracle\Java\javapath; was added by java windows install before my %JAVA_HOME%\bin;.
So I'd echo %JAVA_HOME% pointing to a JDK7 and java -version showing jdk8.
I'd to put %JAVA_HOME%\bin; before C:\ProgramData\Oracle\Java\javapath; so that java -version displays jdk7.
Try %JAVA_HOME%\bin\java -version
If you modify JAVA_HOME, it's usually better to invoke java with an absolute path (using JAVA_HOME) because the new binary is probably not in the path (and then Windows will load the wrong binary).
Make sure that the PATH environment variable is pointing to %JAVA_HOME%\bin.
Be sure not to mix the system variable path and the user variable system path. I feel OK in calling java without the absolute path (when I know how JAVA_HOME and PATH are configured).
Calling java -version from command line, causes cmd.exe to do the lookup on the "known" directories. "Known" means PATH environment variable. It seems that your PATH contains a java 1.3 bin folder, and not 1.6.
JAVA_HOME is another variable, that is used (for example, and not only) by java wrappers, or by scripts executing some java stuff.
Try doing this:
SET JAVA_HOME=C:/Program Files/Java/jdk1.6.0_32
%JAVA_HOME%/bin/java -version
Add quotes where needed.
I had similar issue,in my case , I had two versions java installed. it can be fixed by uninstalling one version of java completely from system.
Had a similar scenario today - two Windows 10 devices - both have JRE 1.6 & 1.7.
When typing
Java -version
One device shows 1.6 the other 1.7.
This was preventing me running a third party JAR to install some software on the device showing 1.6 (which worked fine on the device showing 1.7 when running java -version), using:
java -jar ThirdParty.jar
As the JAR needed to be run by 1.7.
Cause of this was in the PATH environment variable - one device had the location of 1.6 first in the PATH list, moving the 1.7 location above the 1.6 location resulted in consistency using Java -version and allowed me to install the software.
java -version will consult the paths in the special environment variable Path. You need to select the java version you want and move it upwards the latter (click "Move Up"). You probably have that reference to JDK 1.3 in Path above your addition of JDK 1.6. Since that's the first thing the OS finds, that's what it chooses to run.
Executing the command again with the same window opened after changing the environment variables will not work. Re-open it
I know this question is old but this was my case and I wanted to re-explain further, similar to #DanBot 's case

Why can't I set JAVA_HOME and JRE_HOME to something else?

I'm running ubuntu and djatoka (which runs in tomcat) won't work with OpenJDK. So I set JAVA_HOME and JRE_HOME to the Sun java that I downloaded and exported the variables. When I start tomcat, it reports JRE_HOME to what I set.
However, when I actually look at the variables, they're pointing to OpenJDK and djatoka isn't working. I can't find anyplace where it's being hardcoded, and even when I drop the variable assignments in catalina.sh and export from there, the same thing happens.
What the heck is going on? Thanks,
kyle
Did you start tomcat in the same session where you exported the value?
Did you manually install tomcat or did you apt-get it?
You can set the JAVA_HOME that tomcat must use in tomcat/bin/catalina.sh
You can also set it in /etc/environment. eg:
JAVA_HOME=/usr/lib/jvm/java-6-sun
https://help.ubuntu.com/community/EnvironmentVariables#System-wide environment variables
What operating system are you using? Try making sure you are setting those environment variables for the entire system and not the user.
Ubuntu uses a somewhat wired alternatives system. Program in /usr/bin point to /etc/alternatives and from there it goes to /usr/share/jdk-something.
There is an easy way: Uninstall the OpenJDK and GCJ. But you might run into dependency-issues.
Or read about the alternatives-system.
man update-alternatives
should help. It's not that hard. A little complicated - that's all.
I should add, that the benefit of the alternatives system is, that you can have multiple installations of java in parallel (1.5, 1.6, 1.7 alpha, OpenJDK) and switch with one command all the links - to java, javac, appletviewer, javap and so on. Not to forget the CLASSPATH, afaik. And Updates from 1.6.23 to 1.6.24 are handled by Ubuntu flawlessly. But it is annoying to trace the links down to their root.
There is even a Java-shortcut for the alternatives:
update-java-alternatives --help

Running Ant with JDK 1.6 on Mac OS X

I am having a problem running Ant with JDK 1.6 on Mac OS X. Even though Java application versions is set to Java SE 6 in OS X's Java Preference, executing java -version in Terminal also shows java version "1.6.0_07", Ant still seems to use JDK 1.5 to be using JDK 1.5 as it does not see JDK 1.6 classes when compiling my code.
I understand that Ant relies on JAVA_HOME environment variable to specify which JDK to use. However, I do not quite understand how this variable can be set on Mac OS X.
Hence, my question is how to make Ant runs with JDK 1.6 on Mac OS X. If the correct way is still to set JAVA_HOME environment variable, how to set the variable on OS X.
The JAVA_HOME environment variable is set in your home directory's .profile file. (/Users/ejel/.profile ?) Edit it and set it to what you want it to be. E.g.:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
From this point onward, every time you open a new terminal window it will have JAVA_HOME set to this new value. It will not have changed with any existing open windows.
If you are truly aghast to putting this in the profile, or if it conflicts with other software, the export statement could always be run in the terminal manually or go into a script (eg: setj6ev.sh) that is run once before you start running ant tasks.
I've added the line
export JAVA_HOME=`/usr/libexec/java_home`
To my .zshrc file, it seems to do the trick (.bash_profile or whatever if you use bash).
Ted, using the Java Preferences app doesn't change the CurrentJDK symlink in /System/Library/Frameworks/JavaVM.framework/Versions, which is what Ant will use if the JAVA_HOME environment variable isn't set. Thus, you can either change that symlink manually or set the JAVA_HOME environment variable, but if you do neither, then Ant won't use the correct JDK.
You can see the version of the jdk that Ant is using by issuing an <echo message="${ant.java.version}"/> in your build.xml file.
Explicitly setting the JAVA_HOME variable in your .profile/.bashrc/.zshrc isn't actually the recommended way to do it on the mac. There are programs that I've seen get hosed up with an explicitly set JAVA_HOME to a particular version (grails 1.1 with some spring resources for example).
The correct way to set the version of Java that you want to use is to use the /Application/Utilities/Java Preferences.app application.
In there, you drag the version of java that you want to use to the top. This will enable that version for all applications (both those run from the command line and those launched through GUI processes).
You can test the current version by running this from the command line:
java -version
I don't actually like the way that the mac handles the entire set of java symlinked directories and files. It's not obvious and people often screw it up.
See the apple developer page on this for more details.
I try everything, and only one thing works for me : unlink CurrentJDK, and link to 1.6 :
cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo unlink CurrentJDK
sudo ln -sF "1.6" CurrentJDK
Finally I get :
java -version
java version "1.6.0_22"
I hope this help.
You may need to open a new command prompt instance so that the shell can pick up any changes to the environment variables.

Categories

Resources