基于 Linux OpenJDK Debian 的发行版的 JAVA_HOME 环境变量的正确目标是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/663658/
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-11 17:34:52  来源:igfitidea点击:

What is the correct target for the JAVA_HOME environment variable for a Linux OpenJDK Debian-based distribution?

javagrailsjava-homedebian-based

提问by Luis Soeiro

In Windows, JAVA_HOMEmust point to the JDK installation folder (so that JAVA_HOME/bincontains all executables and JAVA_HOME/libscontains all default jarlibraries).

在 Windows 中,JAVA_HOME必须指向 JDK 安装文件夹(以便JAVA_HOME/bin包含所有可执行文件和JAVA_HOME/libs所有默认jar库)。

If I download Sun's JDK bundle and installs it in Linux, it is the same procedure.

如果我下载 Sun 的 JDK 包并将其安装在 Linux 中,则是相同的过程。

However, I need to use Kubuntu's default OpenJDK package. The problem is that all executables are placed in /usr/bin. But the jars are placed in /usr/share/java. Since they are not under the same JAVA_HOMEfolder I'm having trouble with Grails and maybe there will be trouble with other applications that expect the standard Java structure.

但是,我需要使用 Kubuntu 的默认 OpenJDK 包。问题是所有可执行文件都放在/usr/bin. 但是罐子放在/usr/share/java. 由于它们不在同一个JAVA_HOME文件夹下,因此我在使用 Grails 时遇到了问题,并且其他需要标准 Java 结构的应用程序可能也会遇到问题。

  1. If I use:

    JAVA_HOME=/usr
    

    All applications and scripts that want to use any Java executable can use the standard procedure call $JAVA_HOME/bin/executable. However, since the jars are in a different place, they are not always found (example: in grails I'm getting ClassDefNotFoundfor native2ascii).

  2. On the other hand, if I use:

    JAVA_HOME=/usr/share/java
    

    None of the Java executables (java, javac, etc.) can be found.

  1. 如果我使用:

    JAVA_HOME=/usr
    

    所有想要使用任何 Java 可执行文件的应用程序和脚本都可以使用标准过程call $JAVA_HOME/bin/executable。然而,由于罐子在不同的地方,它们并不总是被找到(例如:在我得到的 grailsClassDefNotFoundnative2ascii)。

  2. 另一方面,如果我使用:

    JAVA_HOME=/usr/share/java
    

    在Java可执行文件(没有javajavac等等)都可以找到。

So, what is the correct way of handling the JAVA_HOMEvariable in a Debian-based Linux?

那么,JAVA_HOME在基于 Debian 的 Linux中处理变量的正确方法是什么?

Thanks for your help, Luis

谢谢你的帮助,路易斯

采纳答案by Luis Soeiro

What finally worked for me (Grails now works smoothly) is doing almost like Steve B. has pointed out:

最终对我有用的(Grails 现在工作顺利)几乎就像 Steve B. 指出的那样:

JAVA_HOME=/usr/lib/jvm/default-java

This way if the user changes the default JDK for the system, JAVA_HOMEstill works.

这样,如果用户更改了系统的默认 JDK,JAVA_HOME仍然有效。

default-javais a symlink to the current JVM.

default-java是当前 JVM 的符号链接。

回答by leeand00

My correct target has always been to download it from Sun and just install it that way. Then you know exactly what directory everything goes in.

我的正确目标一直是从 Sun 下载它并以这种方式安装它。然后你确切地知道所有东西都在哪个目录中。

But if you'd prefer to stick with the odd way that Debian installs it, my best guess would be the parent directory just above where the java and javac binaries are located.

但是,如果您更愿意坚持使用 Debian 安装它的奇怪方式,我最好的猜测是 java 和 javac 二进制文件所在位置的正上方的父目录。

(since when you specify it in your path it's $JAVA_HOME/bin) (So in your case it would be ... $JAVA_HOME/share and $JAVA_HOME would be /usr ?)

(因为当你在你的路径中指定它时它是 $JAVA_HOME/bin)(所以在你的情况下它会是...... $JAVA_HOME/share 和 $JAVA_HOME 将是 /usr ?)

Eh, that doesn't sound right...

呃,这听起来不太对……

I'm interested to hear the answer to this too!

我也有兴趣听到这个答案!

回答by iny

I usually don't have any JAVA_HOME environment variable. Java can set it up itself. Inside java java.home system property should be available.

我通常没有任何 JAVA_HOME 环境变量。Java 可以自行设置。内部 java java.home 系统属性应该是可用的。

回答by Steve B.

The standard Ubuntu install seems to put the various Java versions in /usr/lib/jvm. The javac, java you find in your path will softlink to this.

标准的 Ubuntu 安装似乎将各种 Java 版本放在/usr/lib/jvm. javac您在路径中找到的, java 将软链接到此。

There's no issue with installing your own Java version anywhere you like, as long as you set the JAVA_HOMEenvironment variable and make sure to have the new Java binon your path.

只要您设置了JAVA_HOME环境变量并确保bin在您的路径上安装了新的 Java,在您喜欢的任何地方安装您自己的 Java 版本都没有问题。

A simple way to do this is to have the Java home exist as a softlink, so that if you want to upgrade or switch versions you only have to change the directory that this points to - e.g.:

一个简单的方法是让 Java 主目录作为软链接存在,这样如果你想升级或切换版本,你只需要更改它指向的目录 - 例如:

/usr/bin/java --> /opt/jdk/bin/java,

/opt/jdk --> /opt/jdk1.6.011

回答by elcuco

Please see what the update-alternatives command does (it has a nice man...).

请查看 update-alternatives 命令的作用(它有一个好人...)。

Shortly - what happens when you have java-sun-1.4 and java-opensouce-1.0 ... which one takes "java"? It debian "/usr/bin/java" is symbolic link and "/usr/bin/java-sun-1.4" is an alternative to "/usr/bin/java"

很快 - 当你有 java-sun-1.4 和 java-opensouce-1.0 时会发生什么......哪一个需要“java”?debian "/usr/bin/java" 是符号链接,"/usr/bin/java-sun-1.4" 是 "/usr/bin/java" 的替代

Edit: As Richard said, update-alternativesis not enough. You actually need to use update-java-alternatives. More info at:

编辑:正如理查德所说,update-alternatives这还不够。您实际上需要使用update-java-alternatives. 更多信息请访问:

https://help.ubuntu.com/community/Java

https://help.ubuntu.com/community/Java

回答by Cantillon

Try setting the JAVA_LIB variable also.

也尝试设置 JAVA_LIB 变量。

回答by Tobias Schulte

As far as I remember, I used the update-java-alternatives script instead of the update-alternatives. And it did set the JAVA_HOME for me correctly.

据我所知,我使用了 update-java-alternatives 脚本而不是 update-alternatives。它确实为我正确设置了 JAVA_HOME。

回答by MVang

If you have issues with JAR files not being found I would also ensure your CLASSPATH is set to include the location of those files. I do find however that the CLASSPATH often needs to be set differently for different programs and often ends up being something to set uniquely for individual programs.

如果您遇到找不到 JAR 文件的问题,我还会确保您的 CLASSPATH 设置为包含这些文件的位置。然而,我确实发现 CLASSPATH 通常需要为不同的程序设置不同的设置,并且通常最终会为单个程序设置唯一的设置。

回答by Hyman

Updated answer that will solve your problem and also just a general good how-to for installing Oracle Java 7 on Ubuntu can be found here: http://www.wikihow.com/Install-Oracle-Java-on-Ubuntu-Linux

可以在此处找到可以解决您的问题的更新答案以及在 Ubuntu 上安装 Oracle Java 7 的一般性好方法:http: //www.wikihow.com/Install-Oracle-Java-on-Ubuntu-Linux

回答by bbaassssiiee

If you use alternatives to manage multiple java versions, you can set the JAVA_HOMEbased on the symlinked java (or javac) like this:

如果您使用替代方法来管理多个 java 版本,您可以JAVA_HOME像这样基于符号链接的 java(或 javac)设置:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")