Find JAVA_HOME and set it on RHEL - java

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.

Related

Hadoop Installation 2.6.0 on Ubuntu 14 - Java Error

EDIT
I am trying to install Hadoop 2.6.0 on my Ubuntu 14 machine. I am coming across an error though.
When I am trying to set the HOME variable for Java it does not seem to be doing as expected.
I am on my machine as hduser setup specifically for running and using Hadoop. This user is a sudoer.
Some information:
java -version' gives the following
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)
This is the only version installed on my machine, which can be seen by running the following command:
update-alternatives --display java
Which gives the following message:
java - auto mode
link currently points to /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java - priority 1071
slave java.1.gz: /usr/lib/jvm/java-7-openjdk-amd64/jre/man/man1/java.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java'.
I then go to the following path:
cd /usr/lib/jvm
and the I list out the contents ls
default-java java-1.7.0-openjdk-amd64 java-7-openjdk-amd64
I then type cd java* and pwd which brings up the following path:
/usr/lib/jvm/java-1.7.0-openjdk-amd64
Ok, so with that information, I then copy that directory into the .bashrc file as follows:
# The java implementation to use.
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
The hadoop-env.sh file I fill out as follows:
#Hadoop variables
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
I then at the terminal type source ~/.bashrc and then restart the terminal in order for it to set to the new Java path. When typing Hadoop -version I get the following output:
/usr/bin/hadoop: line 350: /usr/lib/jvm/java-6-sun/bin/java: No such file or directory
/usr/bin/hadoop: line 434: /usr/lib/jvm/java-6-sun/bin/java: No such file or directory
I do not know where to go from here.
Thank you,
Add JAVA_HOME to point to your openjdk in hadoop-env.sh. Add this line in hadoop-env.sh:
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
NOTE: Change JAVA_HOME path in .bashrc too
UPDATE I:
Run these commands in terminal. (This will set java & javac in /bin to use your jdk)
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java 1
sudo update-alternatives --config java
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/javac" 1
sudo update-alternatives --config javac
NOTE: If you dont have java and javac in the specified path, it will be inside /jre folder. Change it respectively.
It seems that you have installed java (I mean JRE/JDK here) 1.6, 1.7 and 1.8 at the same time. Which is comlpetelly ok since java installations are usually managed via alternatives subsystem.
The problem is that (contrary to your expectations) setting JAVA_HOME doesn't select which java is used. This is done via mentioned alternatives subsystem instead. The JAVA_HOME variable itself is only addtional configuration and doesn't have power to override what is being executed when one asks to start java process.
Moreover running cd java* isn't really a good idea unless you would like to go into first directory which starts with given string. Try to run ls java* to see my point. And again, it's ok to have multiple different versions of java here thanks to alternatives subsystem.
Another problem is that you are mixing different flavours of java which you probably don't have installed (java-8-oracle vs java-6-sun vs java-1.6.0-openjdk.
To check which java is installed, run:
alternatives --display java
And then based on the results, swich to one version of java and set JAVA_HOME accordingly.

What is the difference between the JAVA_HOME environment variable and the output of "java -version"

What is the JAVA_HOME environment variable actually? I'm in doubt. What is done by setting it ? And I have also another question that if we have two or more JDK s in the OS, we can set one version's path to set the $JAVA_HOME variable and another version as the running java in the machine which is shown by "java -version" command.As I think in ubuntu, java --version command gives the JRE configured (running java) version of java and JAVA_HOME variable defines which version to use for compiling java programs. Am I correct? What is actually the difference between the jdks referred by these two outputs-
1. echo $JAVA_HOME
2. java -version
Lets start with what the two commands do.
echo $JAVA_HOME
This outputs the value of the JAVA_HOME environment variable. By convention, this environment variable is set (typically in a shell "rc" file) to the base directory of a JRE or JDK installation. However, none of the Oracle or OpenJDK core java tools depend on this. You set JAVA_HOME for the benefit of 3rd-party tools, to tell them which of possibly many JRE or JDK installations on the system to use.
So running echo $JAVA_HOME will typically give the directory for a JRE or JDK. But it might be unset ... or point to missing directory.
java -version
This runs the java command and tells it to output its own version information. Unlike the previous, if your shell can find a (real) java command, the information it outputs is definitive.
But which JRE / JDK does will the java command come from?
Well that is determined by the shell, and how it (in general) finds commands. For all mainstream Unix / Linux / BSD shells, this is controlled by the PATH environment variable. The PATH variable defines a command search path; i.e. a list of directories where the shell looks for commands.
So if PATH is /usr/bin:/bin:/usr/local/bin:/home/joeuser/bin:/usr/java/jre-x.y.z/bin then the shell will look for java in each of those directories in turn. The first one java command that it finds will be the one that is executed. You can find out which one it is by running this command.
which java
But that is not the end of it, because if you are using an OpenJDK Java installation on a typical Linux system, the java command in /usr/bin is actually the first in a chain of symlinks that is managed by the alternatives command. The actual executable is at the other end of the chain.
(The alternatives system is a way to implement a switchable system-wide default for a command or set of commands. Read the manual entry for more details. But note that Oracle Java installers (or RPMs) for Linux don't "grok" the Linux alternatives system ... one of my bugbears!)
Finally, it is common to see something like this in a shell "rc" file:
export JAVA_HOME=/path/to/jre # (or jdk)
export PATH=/bin:$JAVA_HOME/bin:/usr/bin:...
There is nothing magical about this. The $JAVA_HOME is going to be expanded by the shell before it sets the environment variable. Tthe actual PATH value will be:
/bin:/path/to/jre/bin:/usr/bin:...
So in summary:
echo $JAVA_HOME tells you the JRE or JDK that a typical 3rd-party tool will try to use by default.
java -version tells you the JRE or JDK that the java command comes from.
They may be different.
JAVA_HOME is an environment variable that contains the directory where Java is installed (if a few versions of Java are installed - this will point to the default)
java -version is a command that shows which version of Java is installed (and again, the one that's used by default in case a few versions are installed)
Addition: Thanks to Luiggi's comment: if the folder where java is installed is not in the PATH (another environment variable) - running java -version or any other java command will fail!
Its not related to jdk and JRE. java -version it is used by the java which is in /usr/bin/java. In some application is used $JDK_HOME. Both the jdks are same. Or we can define different jdk.
echo $JAVA_HOME
will print the value of environment variable called JAVA_HOME. If it's not set, it will print an empty message. Usually, you set JAVA_HOME to the path of the latest JDK installed in your pc.
java -version
will print the version of the java executable (Java Virtual Machine) that is set in your path, if configured. Otherwise, it will throw an exception on the command line.

How can I change Mac OS's default Java VM returned from /usr/libexec/java_home

(Wasn't sure if this should go on SU... migration is certainly an option, but more programmers read questions here, so here goes).
I am running Mac OS X 10.8.4, and I have Apple's JDK 1.6.0_51 installed as well as Oracle's JDK 1.7.0_25. I recently installed Oracle's 1.8 preview JDK for some pre-release software that requires it. Now, when I run /usr/libexec/java_home, I get this:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
1.7.0_25, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home
1.6.0_51-b11-457, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_51-b11-457, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Great.
However, running:
$ java -version
Returns:
java version "1.8.0-ea"
That means that the default version of Java is currently the pre-release version, which breaks some "normal" packages (in my case, VisualVM).
I can't set JAVA_HOME because launching applications ignores environment variables, even when launching from the command line (e.g. $ open /Applications/VisualVM.app).
So, is there a file I can edit where I can set my JVM ordering preferences globally?
(Please don't tell me to launch the Java Preferences Panel because that simply does not work: it does not contain anything useful and only lists one of the 4 JVMs that I have installed.)
Update:
Oracle JVMs live in /Library/Java/JavaVirtualMachines. Re-naming the JDK 1.8 directory to jdk1.8.0.jvm.xyz does not change anything: java_home still finds it in the right place, and running /usr/bin/java still executes the 1.8 JVM. This is not an issue with synlinks, etc.
Answers to Similar Questions
While this answer offers what amounts to a hack that will remove versions of Java from being picked up by java_home, it still does not answer this question of how java_home chooses its default and whether or not users can non-destructively set it.
I think JAVA_HOME is the best you can do. The command-line tools like java and javac will respect that environment variable, you can use /usr/libexec/java_home -v '1.7*' to give you a suitable value to put into JAVA_HOME in order to make command line tools use Java 7.
export JAVA_HOME="`/usr/libexec/java_home -v '1.7*'`"
But standard double-clickable application bundles don't use JDKs installed under /Library/Java at all. Old-style .app bundles using Apple's JavaApplicationStub will use Apple Java 6 from /System/Library/Frameworks, and new-style ones built with AppBundler without a bundled JRE will use the "public" JRE in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home - that's hard-coded in the stub code and can't be changed, and you can't have two different public JREs installed at the same time.
Edit: I've had a look at VisualVM specifically, assuming you're using the "application bundle" version from the download page, and this particular app is not an AppBundler application, instead its main executable is a shell script that calls a number of other shell scripts and reads various configuration files. It defaults to picking the newest JDK from /Library/Java as long as that is 7u10 or later, or uses Java 6 if your Java 7 installation is update 9 or earlier. But unravelling the logic in the shell scripts it looks to me like you can specify a particular JDK using a configuration file.
Create a text file ~/Library/Application Support/VisualVM/1.3.6/etc/visualvm.conf (replace 1.3.6 with whatever version of VisualVM you're using) containing the line
visualvm_jdkhome="`/usr/libexec/java_home -v '1.7*'`"
and this will force it to choose Java 7 instead of 8.
I've been there too and searched everywhere how /usr/libexec/java_home works but I couldn't find any information on how it determines the available Java Virtual Machines it lists.
I've experimented a bit and I think it simply executes a ls /Library/Java/JavaVirtualMachines and then inspects the ./<version>/Contents/Info.plist of all runtimes it finds there.
It then sorts them descending by the key JVMVersion contained in the Info.plist and by default it uses the first entry as its default JVM.
I think the only thing we might do is to change the plist: sudo vi /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Info.plist and then modify the JVMVersion from 1.8.0 to something else that makes it sort it to the bottom instead of the top, like !1.8.0.
Something like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<dict>
...
<key>JVMVersion</key>
<string>!1.8.0</string> <!-- changed from '1.8.0' to '!1.8.0' -->`
and then it magically disappears from the top of the list:
/usr/libexec/java_home -verbose
Matching Java Virtual Machines (3):
1.7.0_45, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
1.7.0_09, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home
!1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
Now you will need to logout/login and then:
java -version
java version "1.7.0_45"
:-)
Of course I have no idea if something else breaks now or if the 1.8.0-ea version of java still works correctly.
You probably should not do any of this but instead simply deinstall 1.8.0.
However so far this has worked for me.
I actually looked at this a little in the disassembler, since source isn't available.
/usr/bin/java and /usr/libexec/java_home both make use of JavaLaunching.framework. The JAVA_HOME environment variable is indeed checked first by /usr/bin/java and friends (but not /usr/libexec/java_home.) The framework uses the JAVA_VERSION and JAVA_ARCH envirionment variables to filter the available JVMs. So, by default:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
11.0.5, x86_64: "Amazon Corretto 11" /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
1.8.0_232, x86_64: "Amazon Corretto 8" /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
But setting, say, JAVA_VERSION can override the default:
$ JAVA_VERSION=1.8 /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home
You can also set JAVA_LAUNCHER_VERBOSE=1 to see some additional debug logging as far as search paths, found JVMs, etc., with both /usr/bin/java and /usr/libexec/java_home.
In the past, JavaLaunching.framework actually used the preferences system (under the com.apple.java.JavaPreferences domain) to set the preferred JVM order, allowing the default JVM to be set with PlistBuddy - but as best as I can tell, that code has been removed in recent versions of macOS. Environment variables seem to be the only way (aside from editing the Info.plist in the JDK bundles themselves.)
Setting default environment variables can of course be done through your .profile or via launchd, if you need them be set at a session level.
It's actually pretty easy.
Let's say we have this in our JavaVirtualMachines folder:
jdk1.7.0_51.jdk
jdk1.8.0.jdk
Imagine that 1.8 is our default, then we just add a new folder (for example 'old') and move the default jdk folder to that new folder.
Do java -version again et voila, 1.7!
I tested "jenv" and other things like setting "JAVA_HOME" without success.
Now I ended up with following solution:
function setJava {
export JAVA_HOME="$(/usr/libexec/java_home -v $1)"
launchctl setenv JAVA_HOME $JAVA_HOME
sudo ln -nsf "$(dirname ${JAVA_HOME})/MacOS" /Library/Java/MacOS
java -version
}
(added to ~/.bashrc or ~/.bash.profile or ~/.zshrc)
And calling like that:
setJava 1.8
java_home will handle the wrong input. So you can't do something wrong.
Maven and other stuff will pick up the right version now.
It's pretty simple, if you don't mind rolling up your sleeves... /Library/Java/Home is the default for JAVA_HOME, and it's just a link that points to one of:
/System/Library/Java/JavaVirtualMachines/1.?.?.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.?.?_??.jdk/Contents/Home
So I wanted to change my default JVM/JDK version without changing the contents of JAVA_HOME... /Library/Java/Home is the standard location for the current JVM/JDK and that's what I wanted to preserve... it seems to me to be the easiest way to change things with the least side effects.
It's actually really simple. In order to change which version of java you see with java -version, all you have to do is some version of this:
cd /Library/Java
sudo rm Home
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home ./Home
I haven't taken the time but a very simple shell script that makes use of /usr/libexec/java_home and ln to repoint the above symlink should be stupid easy to create...
Once you've changed where /Library/Java/Home is pointed... you get the correct result:
cerebro:~ magneto$ java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM)
64-Bit Server VM (build 25.60-b23, mixed mode)
Oracle's uninstallation instructions for Java 7 worked for me.
Excerpt:
Uninstalling the JDK
To uninstall the JDK, you must have Administrator privileges and execute the remove command either as root or by using the sudo(8) tool.
Navigate to /Library/Java/JavaVirtualMachines and remove the directory whose name matches the following format:*
/Library/Java/JavaVirtualMachines/jdk<major>.<minor>.<macro[_update]>.jdk
For example, to uninstall 7u6:
% rm -rf jdk1.7.0_06.jdk
A bit late but as this is an ongoing issue with Mac OSX...
The simplest solution I found was to simply remove the OpenJDK stuff that Apple installs. Every time an update of Mac OSX arrives it gets installed and you'll need to remove it again.
This works really well if you develop apps for Google App Engine on your mac using Java. The OpenJDK does not work well and the Java version that comes with the Mac OSX Yosemite upgrade will make the Eclipse Plug-in for App Engine crash on every deployment with the helpful error: "Read timed out".
Edit: this information is for visualvm specifically, not for any other java app
As mentioned by others, you need to modify the visualvm.conf
For the latest version of JvisualVM 1.3.6 on Mac, the install directories have changed.
It is currently in
/Applications/VisualVM.app/Contents/Resources/visualvm/etc/visualvm.conf.
However this may depend on where you have installed VisualVM. The easiest way to find where your VisualVM is to start it, and then look at the process using:
ps -ef | grep VisualVM
You will see something like:
... -Dnetbeans.dirs=/Applications/VisualVM.app/Contents/Resources/visualvm/visualvm...
You want to take the netbeans.dir property and look up a directory and you will find the etc folder.
Uncomment this line in the visualvm.conf and change the path to the jdk
visualvm_jdkhome="/path/to/jdk"
Additionally, if you are having slowness with your visualvm and you have a lot of memory, I would suggest greatly increasing the amount of memory available and running it in server mode:
visualvm_default_options="-J-XX:MaxPermSize=96m -J-Xmx2048m -J-Xms2048m -J-server -J-XX:+UseCompressedOops -J-XX:+UseConcMarkSweepGC -J-XX:+UseParNewGC -J-XX:NewRatio=2 -J-Dnetbeans.accept_license_class=com.sun.tools.visualvm.modules.startup.AcceptLicense -J-Dsun.jvmstat.perdata.syncWaitMs=10000 -J-Dsun.java2d.noddraw=true -J-Dsun.java2d.d3d=false"
Simplify the need :-)
Steps to changes --
Find the installed version on your machine by executing following command
/usr/libexec/java_home -V
then you might see like below if there are multiple version of JDK installed
11.0.16 (arm64) "Homebrew" - "OpenJDK 11.0.16" /opt/homebrew/Cellar/blabla
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/bala
Create Env file based your machine, for mac like below create or edit the your system Environment file either .zshenv, .zshprofile etc...
nano ~/.zshenv
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_292)
Then the magic is done, where ever this symlink used all those java refer to version which you set! Happy coding Boys & Girls.
I had a similar situation, and the following process worked for me:
In the terminal, type
vi ~/.profile
Then add this line in the file, and save
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home
where version is the one on your computer, such as 1.7.0_25
Exit the editor, then type the following command make it become effective
source ~/.profile
Then type java -version to check the result
java -version
What is .profile?
From:http://computers.tutsplus.com/tutorials/speed-up-your-terminal-workflow-with-command-aliases-and-profile--mac-30515
.profile file is a hidden file. It is an optional file which tells the system which commands to run when the user whose profile file it is logs in. For example, if my username is bruno and there is a .profile file in /Users/bruno/, all of its contents will be executed during the log-in procedure.
MacOS uses /usr/libexec/java_home to find the current Java Version. One way to bypass is to change the plist file as explained by #void256 above.
Other ways is to take the backup of the java_home and replace it with your own script java_home having the code
echo $JAVA_HOME
Now export the JAVA_HOME to the desired version of the SDK by adding the following commands to the ~/.bash_profile.
export JAVA_HOME="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home"
launchctl setenv JAVA_HOME $JAVA_HOME /// Make the environment variable global
Run the command source ~/.bash_profile to the run the above commands.
Anytime one needs to change the JAVA_HOME he can reset the JAVA_HOME value in the ~/.bash_profile file.
I wanted to change default java version form 1.6* to 1.7*. I tried the following steps and it worked for me:
Removed link "java" from under /usr/bin
Created it again, pointing to the new location:
ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java java
verified with "java -version"
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Even if $JRE_HOME is not set java -version is displaying properly

I used sudo apt-get open-jdk7 to install Java on my Ubuntu OS.
When I open a terminal and type echo $JRE_HOME it displays nothing. But then I do java -version it shows me correct version of OpenJDK. Also when I simply type java it works.
Not sure how Ubuntu set the path of java.
Similarly javac works but I don't see anything when I say echo $JAVA_HOME.
My path does not show java directory path in it.
Where is Ubuntu setting the java path? Why does Ubuntu not set JAVA_HOME and JRE_HOME during installation of OpenJDK?
It's a common misconception that Java somehow requires JAVA_HOME, JRE_HOME or similar environment variables to be set to work.
In fact the java and javac (and related) executables don't really care about those variables. They only need to be on the PATH (or can be executed via a full path if not on PATH) and will find their related install by looking "near itself" (i.e. the executable are usually installed in a known location relative to the JVM files).
There are a few third party scripts that use JAVA_HOME and/or JRE_HOME to find a Java installation (Apache Tomcat is a well-known example for this), but the Java executables themselves don't need those variables.
As to how Ubuntu finds the executables is easy to answer: use which java and which javac to find out which executables are executed when you type those commands. On Debian-based systems they are usually a series of symlinks through the alternatives systems that point to the actual executables.
"Where is ubuntu setting java path?", ubuntu does not set the open-jdk-java-path automatically.
try
find / -name "java"
to locate the javac. And write
export JRE_HOME="xxx"
(where xxx is the base of the open-jdk-jre.)
BTW: You got nothing than trouble with open-jdk. You better use the sun-jdk.
As many Linux distros, Ubuntu has /etc/alternatives in its paths. If you type:
which java
Then you'll get
/etc/alternatives/java
If you type:
ls -l /etc/alternatives/java
Then you'll get
/etc/alternatives/java -> /usr/lib/jvm/<your java version>
A few mor ls -l -s can help you to get the full picture.

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