在CentOS 6中设置JAVA_HOME & CLASSPATH。

时间:2022-08-29 23:15:59

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

我在/usr/java/中打开了我的jdk。

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

我将类路径、路径、JAVA_HOME放入/etc/profile中,如下所示。

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,

当我在/usr/java/jdk1.0.7_21/bin编译一些java文件时,

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

它的工作原理。但是当我在其他文件夹上做同样的事情时,它不会。

It displays NoClassDefFoundError.

它显示NoClassDefFoundError。

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

所以我通过echo检查了ClASSPATH、PATH和JAVA_HOME。

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?

我想在控制台使用java,我能做什么呢?

Thanks in advance.

提前谢谢。

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

我做了源代码/etc/ profile。

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

= = = = = = = = = = = = = = = = =我面对的错误= = = = = = = = = = = = = = = = = = = = = = =

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

当我命令java A时(我的类名是A)。

Error: Could not find or load main class A

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

case I命令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

= = = = = = = = = = = = = = = = = = = = =我的代码的完整内容= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = java部分。路径是/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.

/ etc / profile的部分。左部分违约。

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.
  • 而java打印“/ usr / java / bin”。符号链接。
  • My jdk location is /usr/java/jdk1.7.0_21. inside of ./bin every code works fine.
  • 我的jdk位置是/usr/java/jdk1.7.0_21。每个代码都可以正常工作。
  • I did not touch /root/.bash_profile. I just edited /etc/profile.
  • 我没有接触/root/.bash_profile。我只是编辑/etc/profile.

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

谢谢你这样的兴趣。即使它不知道。你的帮助对我有很大的帮助:再次感谢。

5 个解决方案

#1


2  

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

我在/home/prasanth中创建了一个文件夹,并将您的代码复制到名为A.java的文件中。我从/home/prasanth编译为javac a/ a。java和run javac a.A.我得到了输出。

a!

#2


31  

Search here for centos jre install all users:

在这里搜索centos jre安装所有用户:

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

在CentOS中设置环境变量的最简单方法是使用导出。

$> 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

然而,以这种方式设置的变量是瞬态的,即当您退出shell时它们就会消失。显然,在设置需要持久化的环境变量时,即使系统重新启动时,这也没有帮助。在这种情况下,您需要在系统范围内设置变量。在CentOS(我使用v5.2),文件夹/etc/ profile。d是向系统概要文件添加自定义的推荐位置。例如,在安装Sun JDK时,您可能需要设置JAVA_HOME和JRE_HOME环境变量。在本例中:创建一个名为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).

现在,当您重新启动计算机时,java中的环境变量。如果您想知道/etc/ profile中的文件,则将自动初始化(checkout /etc/profile)。d /加载)。

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:

如果您想要在java中加载环境变量。不必重新启动机器,您可以使用source命令:

$> source java.sh

#3


6  

Instructions:

产品说明:

  1. Click on the "Terminal" icon in the desktop panel to open a terminal window and access the command prompt.
  2. 单击桌面面板中的“终端”图标,打开终端窗口并访问命令提示符。
  3. Type the command "which java" to find the path to the Java executable file.
  4. 键入命令“哪个java”来找到指向java可执行文件的路径。
  5. Type the command "su -" to become the root user.
  6. 键入命令“su -”成为根用户。
  7. 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.
  8. 键入命令“vi /root/”。bash_profile“在Vi文本编辑器中打开系统“bash_profile”文件。您可以用首选的文本编辑器替换“vi”。
  9. Type "export JAVA_HOME=/usr/local/java/" at the bottom of the file. Replace "/usr/local/java" with the location found in step two.
  10. 在文件的底部输入“export JAVA_HOME=/usr/local/java/”。将“/usr/local/java”替换为步骤2中的位置。
  11. Save and close the "bash_profile" file.
  12. 保存并关闭“bash_profile”文件。
  13. Type the command "exit" to close the root session.
  14. 键入命令“exit”以关闭根会话。
  15. Log out of the system and log back in.
  16. 退出系统并重新登录。
  17. Type the command "echo $JAVA_HOME" to ensure that the path was set correctly.
  18. 键入命令“echo $JAVA_HOME”,以确保正确设置了路径。

set java_home in centos

设置java_home centos

#4


1  

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

似乎你对环境变量没有任何问题。

Compile your file from src with

从src中编译您的文件。

javac a/A.java

javac / A.java

Then, run your program as

然后,运行您的程序。

java a.A

java有一

#5


0  

I had to change /etc/profile.d/java_env.sh to point to the new path and then logout/login.

我必须改变/etc/profile.d/java_env。sh指向新路径,然后注销/登录。

#1


2  

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

我在/home/prasanth中创建了一个文件夹,并将您的代码复制到名为A.java的文件中。我从/home/prasanth编译为javac a/ a。java和run javac a.A.我得到了输出。

a!

#2


31  

Search here for centos jre install all users:

在这里搜索centos jre安装所有用户:

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

在CentOS中设置环境变量的最简单方法是使用导出。

$> 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

然而,以这种方式设置的变量是瞬态的,即当您退出shell时它们就会消失。显然,在设置需要持久化的环境变量时,即使系统重新启动时,这也没有帮助。在这种情况下,您需要在系统范围内设置变量。在CentOS(我使用v5.2),文件夹/etc/ profile。d是向系统概要文件添加自定义的推荐位置。例如,在安装Sun JDK时,您可能需要设置JAVA_HOME和JRE_HOME环境变量。在本例中:创建一个名为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).

现在,当您重新启动计算机时,java中的环境变量。如果您想知道/etc/ profile中的文件,则将自动初始化(checkout /etc/profile)。d /加载)。

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:

如果您想要在java中加载环境变量。不必重新启动机器,您可以使用source命令:

$> source java.sh

#3


6  

Instructions:

产品说明:

  1. Click on the "Terminal" icon in the desktop panel to open a terminal window and access the command prompt.
  2. 单击桌面面板中的“终端”图标,打开终端窗口并访问命令提示符。
  3. Type the command "which java" to find the path to the Java executable file.
  4. 键入命令“哪个java”来找到指向java可执行文件的路径。
  5. Type the command "su -" to become the root user.
  6. 键入命令“su -”成为根用户。
  7. 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.
  8. 键入命令“vi /root/”。bash_profile“在Vi文本编辑器中打开系统“bash_profile”文件。您可以用首选的文本编辑器替换“vi”。
  9. Type "export JAVA_HOME=/usr/local/java/" at the bottom of the file. Replace "/usr/local/java" with the location found in step two.
  10. 在文件的底部输入“export JAVA_HOME=/usr/local/java/”。将“/usr/local/java”替换为步骤2中的位置。
  11. Save and close the "bash_profile" file.
  12. 保存并关闭“bash_profile”文件。
  13. Type the command "exit" to close the root session.
  14. 键入命令“exit”以关闭根会话。
  15. Log out of the system and log back in.
  16. 退出系统并重新登录。
  17. Type the command "echo $JAVA_HOME" to ensure that the path was set correctly.
  18. 键入命令“echo $JAVA_HOME”,以确保正确设置了路径。

set java_home in centos

设置java_home centos

#4


1  

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

似乎你对环境变量没有任何问题。

Compile your file from src with

从src中编译您的文件。

javac a/A.java

javac / A.java

Then, run your program as

然后,运行您的程序。

java a.A

java有一

#5


0  

I had to change /etc/profile.d/java_env.sh to point to the new path and then logout/login.

我必须改变/etc/profile.d/java_env。sh指向新路径,然后注销/登录。