在 CentOS 6 中设置 JAVA_HOME 和 CLASSPATH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16271316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:06:57  来源:igfitidea点击:

setting JAVA_HOME & CLASSPATH in CentOS 6

javapathcentosnoclassdeffounderror

提问by Juneyoung Oh

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

我已经在 /usr/java/ 中解压了我的 jdk。

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

我将 CLASSPATH、PATH、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.

附注。当然我做了source /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

案例我命令 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.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.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 javaprints "/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.
  • which java打印“/usr/java/bin”。有符号链接。
  • 我的 jdk 位置是 /usr/java/jdk1.7.0_21。在 ./bin 中,每个代码都可以正常工作。
  • 我没有碰/root/.bash_profile。我刚刚编辑了 /etc/profile.d 文件。

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

感谢您的关注。即使它没有想通。你的帮助对我很有帮助:D 再次感谢。

采纳答案by prasanth

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.javaand run javac a.A. I got output as

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

a!

回答by bonecrusher

Instructions:

指示:

  1. Click on the Terminalicon in the desktop panel to open a terminal window and access the command prompt.
  2. Type the command which javato 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_profileto open the system bash_profilefile in the Vi text editor. You can replace viwith your preferred text editor.
  5. Type export JAVA_HOME=/usr/local/java/at the bottom of the file. Replace /usr/local/javawith the location found in step two.
  6. Save and close the bash_profilefile.
  7. Type the command exitto close the root session.
  8. Log out of the system and log back in.
  9. Type the command echo $JAVA_HOMEto ensure that the path was set correctly.
  1. 单击桌面面板中的终端图标以打开终端窗口并访问命令提示符。
  2. 键入命令which java以查找 Java 可执行文件的路径。
  3. 键入命令su -以成为 root 用户。
  4. 键入命令vi /root/.bash_profile以在 Vi 文本编辑器中打开系统 bash_profile文件。您可以将vi替换为您喜欢的文本编辑器。
  5. 键入export JAVA_HOME=/usr/local/java/该文件的底部。替换/usr/local/java为在第二步中找到的位置。
  6. 保存并关闭bash_profile文件。
  7. 键入命令exit以关闭根会话。
  8. 退出系统并重新登录。
  9. 键入命令echo $JAVA_HOME以确保路径设置正确。

set java_home in centos

在centos中设置java_home

回答by prasanth

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/A.java

Then, run your program as

然后,运行你的程序

java a.A

java a.A

回答by BruceG

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 以指向新路径,然后注销/登录。

回答by asraful009

Search herefor centos jre install all users:

在这里搜索centos jre install all users:

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

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

$> 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 calledjava.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.sh 中的环境变量将自动初始化(如果您好奇 /etc/profile.d/ 中的文件是如何加载的,请查看 /etc/profile)。

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

PS:如果你想在java.sh不重启机器的情况下加载环境变量,你可以使用 source 命令,如下所示:

$> source java.sh

回答by YouAreAwesome

Do the following steps:

执行以下步骤:

  1. sudo -s
  2. yum install java-1.8.0-openjdk-devel
  3. vi .bash_profile , and add below line into .bash_profile file and save the file.
  1. 须藤 -s
  2. yum 安装 java-1.8.0-openjdk-devel
  3. vi .bash_profile ,并将以下行添加到 .bash_profile 文件中并保存该文件。

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/

导出 JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/

Note - I am using CentOS7 as OS.

注意 - 我使用 CentOS7 作为操作系统。

回答by alxdrl

Providing javacis set up through /etc/alternatives/javac, you can add to your .bash_profile:

提供javac通过设置/etc/alternatives/javac,您可以添加到您的 .bash_profile:

JAVA_HOME=$(l=$(which javac) ; while : ; do nl=$(readlink ${l}) ; [ "$nl" ] || break ; l=$nl ; done ; echo $(cd $(dirname $l)/.. ; pwd) )
export JAVA_HOME