How Do I Set JAVA_HOME in Centos For Apache Tomcat?

29,279

Solution 1

Run below commands on shell prompt before adding it into java.sh:

export JAVA_HOME=/usr/bin/java/java-1.8.0-openjdk.x86_64 
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar

Then run echo $JAVA_HOME

Solution 2

If your usage is covered by their licence, I strongly recommend to use Oracle's JDK RPM: when installed it provides much more sane directory layout than OpenJDK RPM package(s): you would be able to use "/usr/java/latest" as Java home. To have persistent environment variable, add export command to ~/.bashrc or ~/.bash_profile file (depending on how you perform remote login, add it to both if unsure): export JAVA_HOME=/usr/java/latest.

Share:
29,279

Related videos on Youtube

Ityav
Author by

Ityav

Experienced Full Stack Engineer with 10+ years in the industry, worked in different environments, from big consultancy companies to start-ups. I am a self-motivated and self-taught professional who likes to solve problems. I merge a passion for usability and user experience with technical knowledge to create cool digital experiences. My repertoire includes the programming languages: Java, JavaScript/Typescript, Python, Ruby, SQL and tools such as ReactJS, Redux, VueJS,Angular, NodeJS, NestJS, GraphQL, Nginx and Apache server configuration, Docker and more. I am very familiar and experienced with web application architectures: Microservices, MVC, MERN and MEAN stacks.

Updated on July 09, 2022

Comments

  • Ityav
    Ityav almost 2 years

    I have purchased a VPS with a top hosting company. I am new to Linux. Since I am not able to purchase a CPanel License, I need to manually install JDK, Tomcat, and MariaDB. All this through SSH using PUTTY.

    There are tutorials which I have followed:

    Setting JAVA_HOME & CLASSPATH in CentOS 6

    How to Install Apache Tomcat 8.5 on CentOS 7.3

    But since I am a newbie in Linux am only able to install JDK8.

    Now I need to set JAVA_HOME in a bash file to remain permanent before I can continue with tomcat installation.

    From PUTTY, I have login as root user with my password:

    1. I checked the location of the Java "which java" : /usr/bin/java

    2. To get the exact jdk name I used command "sudo update-alternatives --config java" >java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.144-0.b01.el7_4.x86_64/jre/bin/java)

    3. I createed a new file through command "vim /etc/profile.d/java.sh" which gave the error below:

        E325: ATTENTION
        Found a swap file by the name "/etc/profile.d/.java.sh.swp"
                  owned by: root   dated: Thu Oct 19 14:21:28 2017
                 file name: /etc/profile.d/java.sh
                  modified: YES
                 user name: root   host name: rtp
                process ID: 31766
        While opening file "/etc/profile.d/java.sh"
        
        (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 /etc/profile.d/java.sh"
            to recover the changes (see ":help recovery").
            If you did this already, delete the swap file "/etc/profile.d/.java.sh.swp"
            to avoid this message.
        
        Swap file "/etc/profile.d/.java.sh.swp" already exists!
        [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
    

    I pressed d to delete the existing one.

    1. I copy this and pasted:
        export JAVA_HOME=/usr/bin/java/java-1.8.0-openjdk.x86_64 
        export PATH=$PATH:$JAVA_HOME/bin
        export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
    

    And then I Press ENTER

    The file is in insert mode so I press Esc :w java.sh to save and exit.

    Then I closed the PUTTY session and started again to check if the JAVA_HOME has been set: "echo $JAVA_HOME"

    No result!

    I don't understand what to do again. I kept on repeating this for two days now. Please, any help?

  • Ityav
    Ityav over 6 years
    i followed a giude as newbie i used this command:"yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64" i don't know how to get the Oracle's JDK or should i substitute?
  • scrutari
    scrutari over 6 years
    Normally you need one JDK. So if you choose to use one it's better to remove other (just to keep your system simpler). Oracle JDK RPM can be downloaded from Oracle's website.
  • Ityav
    Ityav over 6 years
    I will like to remove the open jdk and i used these command but it does not work:[root@rtp ~]# java -version openjdk version "1.8.0_144" OpenJDK Runtime Environment (build 1.8.0_144-b01) OpenJDK 64-Bit Server VM (build 25.144-b01, mixed mode) [root@rtp ~]# yum -y remove java* Loaded plugins: fastestmirror No Match for argument: java.sh No Packages marked for removal If i decide to install Oracle jdk8, will there any issue, Sir?
  • scrutari
    scrutari over 6 years
    Before running yum remove ... change you directory to some other directory where there is no file which would match pattern java* (like java.sh in your case) - YUM matches your pattern with local files first and only afterwards with RPM names.