Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting JAVA_HOME & CLASSPATH in CentOS 6

Tags:

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.

like image 709
Juneyoung Oh Avatar asked Apr 29 '13 04:04

Juneyoung Oh


People also ask

What should I set JAVA_HOME to?

Traditionally, JAVA_HOME is set to the JRE or SDK main directory. The bin/ subdirectory might be in your PATH, though. That is because your PATH is wrong. Your PATH needs to point to the bin/ directory within your Java SDK.

What is JAVA_HOME path for Windows?

JAVA_HOME = C:\Program Files\Java\jdk1.7.0 [Location of your JDK Installation Directory] Once you have the JDK installation path: Right-click the My Computer icon on. Select Properties. Click the Advanced system setting tab on left side of your screen.

Should JAVA_HOME point to JDK or JRE?

If you're doing any sort of development, or building with Maven or Ant, you need to point to the JDK (Java Development Kit) where utilities such as javac (the Java Compiler) reside. Otherwise, you can point to the JRE (Java Runtime Environment). The JDK contains everything the JRE has and more.

Does JAVA_HOME need to be set?

Do we need to set both JAVA_HOME and PATH? Some modern programs are intelligent enough to extract the JDK location from PATH if JAVA_HOME is not set. For example, with Gradle you can set either PATH or JAVA_HOME and it will run fine.


1 Answers

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 
like image 179
asraful009 Avatar answered Sep 27 '22 17:09

asraful009