This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to update the JAVA_HOME in RedHat?  RSS feed

 
Ranch Hand
Posts: 1313
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have installed redhat 6.6 server version.It default comes with openjdk. I have install java 1.8. So how to update the JAVA_HOME to 1.8 ?

I tried checking .bashrc file in the root folder, there is no JAVA_HOME setting now.

below are the details when i try to use "java -version"

java version "1.6"
OpenJDK Runtime Environment
OpenJDK 64-Bit Server VM (build xxxxxxxxx, mixed mode)
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you don't see JAVA_HOME anywhere, it just means that your system administrator never setup that environment variable -- after all, although using such a variable is considered common/best practice, it is not mandated as required.

Anyway... you can see where the java executable, that is being run, is located via the "which" command. With the location, you can see which of the directories in your PATH environment variable is triggering that executable. Just make sure that the location of your newly installed JVM is earlier in the PATH than the JVM that you don't want.

Henry
 
sam liya
Ranch Hand
Posts: 1313
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks henry, anyway in redhat which file contain the JAVA_HOME details?
i check in the .bashrc file. it is empty. is there any other place that we can update JAVA_HOME variable?
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sam liya wrote:
i check in the .bashrc file. it is empty. is there any other place that we can update JAVA_HOME variable?



Yeah. It is not as simple as checking a single file.... basically...

  • Bash uses the concept of a profile and an rc file. The profile is used for initial login, while the rc is used for that shell and all subshells after login.
  • Bash uses the concept of a profile and a bash profile. The bash profile, if provided, will override the profile. This is used to maintain compatibility with the Bourne shell.
  • Bash, like the Bourne shell, also have global setups, for the profile and the rc file, located in the /etc directory.


  • If you really want to change it, by changing the bash files, then ... I recommend reading up on the Bash shell, in order to understand this startup procedure.

    Henry
     
    Marshal
    Posts: 64496
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can find out by feeding echo $JAVA_HOME to a terminal; if it comes up blank, then it isn't set anywhere. What happens if you pass export JAVA_HOME=... to a terminal? I presume you know how to write the path; it would start / and end with jdk1.8.0_181/ At least I think there is a / at its end; it does not include bin.
    You might not be able to edit the profile/bash_profile or /etc/bashrc files.
     
    Bartender
    Posts: 20775
    124
    Android Eclipse IDE Java Linux Redhat Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Every shell instance in Linux has a set of environment variables. That set will either be a complete set of its own or one inherited from a parent shell. When a new shell is created via logon, the logon process itself invokes a number of system-wide and user-local scripts that can all add variables to the environment. That set can be hard to track, since among other things, it depends on which shell you're running under (bash, zsh, ash, csh, and so forth). Stuff comes in from all over.

    JAVA_HOME is neither an OS environment variable or even a Java environment variable. It's a convention that many (but not all) Java application products use to determine which JDK or JRE to run under. Depending on how your system is set up, it may be set from a central script (usually in a file somewhere under /etc), or from something like your login script (I (mis)use .bash_profile). In actuality, anyone can set/change JAVA_HOME any time they want and whatever the last setting was is the value that will be used until something changes it again. And note that unless the environment is shared, that setting will NOT change the JAVA_HOME values for any other shell process. That, in fact is why the "export" statement exists. So that you can set environment variables in one shell process and have them be inherited by child shells that it spawns (for example, to run a shell script).

    When you install Java on a Red Hat/CentOS/Fedora or similar system, Oracle provides a RedHat Package Manager (RPM) file. All this package does is copy the standard JRE or JDK fileset that is provided in the ZIP distros to the /usr/java directory and make some file aliases so that the Java files also appear to be in Linux-standard locations. So for the most part, you get about the same results downloading the ZIP and unzipping it to a /usr/java directory (which you can create if you need to), or in downloading the RPM and installing it. Although I prefer the RPM, since that way, Java also shows up in the system's installed applications (rpm) database and can be verified and repaired by the RPM tools, if necessary.

    There's one further annoyance, however. Red Hat implemented a rather nasty system called "alternatives" which was originally designed for Ubuntu I think. What alternatives does is permit multiple service providers to install on a system in ways that would otherwise conflict and allow selecting a specific alternative (hence the name) on a per-user basis. It's very non-intuitive, I've never seen a concise set of documentation on it, and I prefer to use brute force and ignore it.

    Alternatives is actually what determines which Java compiler and runtime you'd get if you just typed "java" in at a shell prompt and hadn't set a specific PATH or used a fully-qualified execution pathname (e.g., /usr/bin/java or /usr/java/latest/bin/java or something like /usr/java/jdk_1.23_00/bin/java. The java runtime and java compilers don't use JAVA_HOME. I normally add my default java bin directory to my shell path in my login script, like so:
     
    sam liya
    Ranch Hand
    Posts: 1313
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks Henry, Campbell  and Tim. I just need to finalized below thing.
    there are three file .profiles, .bash_profile  and .bashrc . As for my knowledge below is the details of these iles.

    1)  .profiles          =  set system wide environmental variables. Executed only for interactive shell. read by many shells
    2)  .bash_profile   =  is executed for both interactive and non-interactive shells .
    3)  .bashrc           = . only for bash

    i need to if we set up same envirment vriable in both .profile and .bashrc what will happen. For example as below
    in .profiles    JAVA_HOME = java 1.6
    in .bashrc    JAVA_HOME  = java 1.7          

    so what JAVA_HOME will take when we open a ssh?

     
    Campbell Ritchie
    Marshal
    Posts: 64496
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't know. It probably depends on which is executed first, and what the instructions are in the file executed second. Are you writing something like .../myJavaInstallation/jdk1.7.0_99/ or .../myJavaInstallation/jdk1.7.0_99/:$JAVA_HOME or $JAVA_HOME:.../myJavaInstallation/jdk1.7.0_99/? Each of those will behave differently. Do you have access to .bash_profile and profile in the first place? If not, you will have to write the JAVA_HOME in ~/.bashrc regardless. Remember you can change it for each terminal by writing an export instruction, but that will probably not affect the current PATH for that terminal.

    I suggest you try a few things and see what happens. I suspect that whichever file is executed last will overwrite the environment variable exported by the others, but I am not sure.
     
    Tim Holloway
    Bartender
    Posts: 20775
    124
    Android Eclipse IDE Java Linux Redhat Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's a pretty authoritative description of how the different profiles relate: Bash reference manual

    Remember, if more than one profile is "sourced" containing an assignment for JAVA_HOME, then the last setting is the one that will be kept.

    When you connect via ssh, it's just like a local login as far as shells go. The main thing is that the shell initialization script(s) executed will be for the account you are logging into on the target machine. The settings for the machine you are connecting from are not used.


     
    sam liya
    Ranch Hand
    Posts: 1313
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have updated java in root/.bashrc file as below



    So when i login as root user and type echo $JAVA_HOME it shows the java home value. But when i login as any other user it shows empty value. So that means .bashrc file related to user wise. is it true ?
     
    Campbell Ritchie
    Marshal
    Posts: 64496
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    sam liya wrote:I have updated java in root/.bashrc file . . . .bashrc file related to user wise. is it true ?

    I don't think that is a good idea. That will only affect root, so those setting are not applied to any other users. Don't log on as root, anyway. Only use su or in F28+ sudo -i as long as you need to do administrator's tasks and log out when you have finished.
     
    God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!