setting JAVA_HOME & CLASSPATH in CentOS 6

177,151

Solution 1

I created a folder named a in /home/prasanth and copied your code to a file named A.java. I compiled from /home/prasanth as javac a/A.java and run javac a.A. I got output as

a!

Solution 2

Search here for centos jre install all users:

The easiest way to set an environment variable in CentOS is to use export as in

$> export JAVA_HOME=/usr/java/jdk.1.5.0_12

$> export PATH=$PATH:$JAVA_HOME

However, variables set in such a manner are transient i.e. they will disappear the moment you exit the shell. Obviously this is not helpful when setting environment variables that need to persist even when the system reboots. In such cases, you need to set the variables within the system wide profile. In CentOS (I’m using v5.2), the folder /etc/profile.d/ is the recommended place to add customizations to the system profile. For example, when installing the Sun JDK, you might need to set the JAVA_HOME and JRE_HOME environment variables. In this case: Create a new file called java.sh

vim /etc/profile.d/java.sh

Within this file, initialize the necessary environment variables

export JRE_HOME=/usr/java/jdk1.5.0_12/jre
export PATH=$PATH:$JRE_HOME/bin

export JAVA_HOME=/usr/java/jdk1.5.0_12
export JAVA_PATH=$JAVA_HOME

export PATH=$PATH:$JAVA_HOME/bin

Now when you restart your machine, the environment variables within java.sh will be automatically initialized (checkout /etc/profile if you are curious how the files in /etc/profile.d/ are loaded).

PS: If you want to load the environment variables within java.sh without having to restart the machine, you can use the source command as in:

$> source java.sh

Solution 3

Instructions:

  1. Click on the Terminal icon in the desktop panel to open a terminal window and access the command prompt.
  2. Type the command which java to find the path to the Java executable file.
  3. Type the command su - to become the root user.
  4. Type the command vi /root/.bash_profile to open the system bash_profile file in the Vi text editor. You can replace vi with your preferred text editor.
  5. Type export JAVA_HOME=/usr/local/java/ at the bottom of the file. Replace /usr/local/java with the location found in step two.
  6. Save and close the bash_profile file.
  7. Type the command exit to close the root session.
  8. Log out of the system and log back in.
  9. Type the command echo $JAVA_HOME to ensure that the path was set correctly.

set java_home in centos

Solution 4

It seems that you dont have any problem with the environmental variables.

Compile your file from src with

javac a/A.java

Then, run your program as

java a.A

Share:
177,151

Related videos on Youtube

Juneyoung Oh
Author by

Juneyoung Oh

Hi, I'm working in South Korea as a web programmer since 2013. Main Experience : RDB, SpringFramework, Java, Linux Also Experience with : Javascript, Amazon Cloud Products Happy coding :D

Updated on April 02, 2020

Comments

  • Juneyoung Oh
    Juneyoung Oh about 4 years

    I have unpacked my jdk in /usr/java/.

    and I put CLASSPATH, PATH, JAVA_HOME into /etc/profile like below.

    export JAVA_HOME=/usr/java/jdk1.7.0_21
    export PATH=$PATH:$JAVA_HOME/bin
    export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
    

    And when I compile some java file in /usr/java/jdk1.0.7_21/bin,

    it works. But when I am doing same thing on other folder, it doesn't.

    It displays NoClassDefFoundError.

    So I have checked ClASSPATH, PATH, JAVA_HOME via echo.

    It shows like below.

    [root@localhost a]# echo $JAVA_HOME
    /usr/java/jdk1.7.0_21
    [root@localhost a]# echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/guest/bin:/usr/java/jdk1.7.0_21/bin:/usr/java/bin:/usr/java/jdk1.7.0_21/bin
    [root@localhost a]# echo $CLASSPATH
    /usr/java/jdk1.7.0_21/jre/lib/ext:/usr/java/jdk1.7.0_21/lib/tools.jar
    

    I want to use java in console, What can I do fir this situation?

    Thanks in advance.

    PS. of couse I did source /etc/profile.

    =================The Errors what I'm facing with =======================

    when I command java A(My class name is A).

    Error: Could not find or load main class A
    

    case I command java -cp /home/guest/workspace/AAA/src/a/ A

    Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: a/A)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
    

    =====================full content of my code====================================== java part. path is /usr/guest/workspace/AAA/src/a/A.java

    package a;
    
    public class A {
        public static void main(String[] args) {
            System.out.println("a!\n");
        }
    }
    

    /etc/profile part. left part is default.

    export JAVA_HOME=/usr/java/jdk1.7.0_21
    export PATH=$PATH:$JAVA_HOME/bin
    export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
    

    other parts might be helpful to solve.

    • which java prints "/usr/java/bin". there's symbolic link.
    • My jdk location is /usr/java/jdk1.7.0_21. inside of ./bin every code works fine.
    • I did not touch /root/.bash_profile. I just edited /etc/profile.

    Thanks for such interest. even though it does not figured out. your help was great healing to me :D Thanks again.

    • prasanth
      prasanth about 11 years
      Have you checked java -version and javac -version from console??
    • Juneyoung Oh
      Juneyoung Oh about 11 years
      @prasanth Yes, It works nice and shows proper Message. I'm currently using 1.7.0_19. thanks for asking
    • prasanth
      prasanth about 11 years
      Can you post the complete stack trace of you error?
    • Juneyoung Oh
      Juneyoung Oh about 11 years
      @prasanth I added my specific situation to my write.
    • prasanth
      prasanth about 11 years
      In what package your class is present?? Is it inside 'a'? Then try java a.A.
    • Juneyoung Oh
      Juneyoung Oh about 11 years
      @prasanth yes, my package name is a. and it still doesn't work. I have tried java a.A, java A, java -cp /stupid/full/path A. only last one works. Is there anything left I can try? Thanks for comment on my question:-)
  • Juneyoung Oh
    Juneyoung Oh about 11 years
    I did what it says, but after that(including restart and re logged in), when I type echo $JAVA_HOME, it shows nothing. But some situation has been changed. for instance, I don't need -cp option anymore, thanks for this :D
  • Juneyoung Oh
    Juneyoung Oh about 11 years
    [root@localhost a]# javac a/A.java javac: file not found: a.A.java Usage: javac <options> <source files> use -help for a list of possible options
  • prasanth
    prasanth about 11 years
    Try this javac `find * -name '*.java'` from src
  • prasanth
    prasanth about 11 years
    You should compile from src not from 'a'
  • Juneyoung Oh
    Juneyoung Oh about 11 years
    The folder thing does not work also. I tried that one in src and a.
  • Juneyoung Oh
    Juneyoung Oh about 11 years
    Yes, that's normal, unfortunately except mine. I wasted whole day, and totally exhausted:-< Anyway,Thanks for you effort :D
  • Juneyoung Oh
    Juneyoung Oh about 11 years
    you were right. I have to use java command not in the folder, but its parent folder, means 'src'. yesterday was so stressed so did not test properly, I think. thanks again:D
  • Juneyoung Oh
    Juneyoung Oh about 11 years
    But I still do not know the reason why I can not execute in package folder. if you have any idea, just let me know^^
  • prasanth
    prasanth almost 11 years
    remove package a; declaration and you will be able to run from your package folder.
  • Ammad
    Ammad over 6 years
    Thanks man. Work like a breeze.I added to java.sh t and copy paste the same on command line which actually enable it right away.