在 Ubuntu 中设置 JAVA_HOME 环境变量

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

Setting the JAVA_HOME environment variable in Ubuntu

javaubuntujava-home

提问by user2461031

I'm pretty new on ubuntu, at some point in the terminal I'm running:

我在 ubuntu 上很新,在我运行的终端的某个时候:

mortar local:illustrate pigscripts/retail-recsys.pig purchase_input -f params/retail.params

but I have this following error:

但我有以下错误:

A suitable java installation could not be found. If you already have java installed
please set your JAVA_HOME environment variable before continuing. Otherwise, a suitable java installation will need to be added to your local system.

Installing Java

On OSX run javacfrom the command line. This will intiate the installation. For Linux systems please consult the documentation on your relevant package manager.

找不到合适的 java 安装。如果您已经安装了 java,
请在继续之前设置您的 JAVA_HOME 环境变量。否则,需要将合适的 java 安装添加到您的本地系统。

安装 Java

在 OSX 上,从命令行运行javac。这将启动安装。对于 Linux 系统,请查阅相关包管理器的文档。

But I'm pretty sure I have Java, so please how can I set my JAVA_HOME environment variable?

但我很确定我有 Java,所以请问我如何设置我的 JAVA_HOME 环境变量?

回答by chanaka777

put the line export JAVA_HOME=/usr/lib/jvm/java-xxx-oraclein your .profile file at home directory. Note that you have to replace xxx. You may need to logout and login again

将该行export JAVA_HOME=/usr/lib/jvm/java-xxx-oracle放在主目录的 .profile 文件中。请注意,您必须替换xxx。您可能需要注销并重新登录

回答by mok

Normally you can set paths in

通常你可以设置路径

~/.bashrc

~/.bashrc

with export JAVA_HOME=/usr/lib/jvm/java-version

带有导出 JAVA_HOME=/usr/lib/jvm/java-version

However you may followe instructions from herefor a comprehensive instruction.

但是,您可以按照此处的说明获取全面说明。

回答by Sanjay Rabari

export JAVA_HOME=/usr/lib/jvm/java-7-oracle

in your ~/.bashrcfile.

在您的~/.bashrc文件中。

If you want this environment variable available to all users and on system start then you can add the following to /etc/profile.d/java.sh (create it if necessary):

如果您希望此环境变量可供所有用户使用并在系统启动时使用,那么您可以将以下内容添加到 /etc/profile.d/java.sh(如有必要,请创建它):

export JDK_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME=/usr/lib/jvm/java-7-oracle

Then in a terminal run:

然后在终端运行:

sudo chmod +x /etc/profile.d/java.sh
source /etc/profile.d/java.sh

回答by djangofan

By far, the ultimate guide to doing this is here. You don't need to set PATH as much as you just need to adjust the default 'java alternative' location.

到目前为止,执行此操作的最终指南在这里。您不需要像只需要调整默认的“java 替代”位置一样设置 PATH。

回答by nico zhang

you can type java in terminal,if it does not work means your did not install java.if it works, type javac in terminal.if javac dose not work,you should set the java environment variable,if it works ,there maybe something wrong with you program.

你可以在终端输入java,如果它不起作用意味着你没有安装java。如果它起作用,在终端输入javac。如果javac不起作用,你应该设置java环境变量,如果它起作用,可能有问题与你的程序。

回答by Elliott Frisch

First, you need to decide which installed version of Java to use? No fear, you can pick any you have -

首先,您需要决定使用哪个已安装的 Java 版本?不用担心,您可以选择任何您拥有的 -

update-java-alternatives -l

One "easy" solution is to add this to "$HOME/.bashrc",

一种“简单”的解决方案是将其添加到“$HOME/.bashrc”中,

export JAVA_HOME=$(update-java-alternatives -l | head -n 1 | cut -f3 -d' ')

This picks the first installed JDK and takes it's JAVA_HOME (the third field) - on my system that's

这将选择第一个安装的 JDK 并使用它的 JAVA_HOME(第三个字段) - 在我的系统上

/usr/lib/jvm/java-1.7.0-openjdk-amd64

回答by PovilasB

The simplest method to set environment variable is with export:

设置环境变量的最简单方法是导出:

    $ export JAVA_HOME="/usr/bin"

This will temporarily set the desired variable. You can check if it was set with:

这将临时设置所需的变量。您可以检查它是否设置为:

    $ echo $JAVA_HOME

or

或者

    $ printenv

If you want a more permanent solution, append 'export JAVA_HOME="/usr/bin"' to .bashrc or .bash_profile file.

如果您想要更持久的解决方案,请将“export JAVA_HOME="/usr/bin"”附加到 .bashrc 或 .bash_profile 文件。

To check if java is properly installed:

要检查 java 是否已正确安装:

    $ which java
    $ which javac

You should get similar output:

你应该得到类似的输出:

    /usr/bin/java

回答by All ?? Vаи?тy

For JAVA_HOMEto point to the active jdk, add to your ~/.bashrc

对于JAVA_HOME以点到活动JDK,添加到您的~/.bashrc

export JAVA_HOME=$(update-alternatives --query javac | sed -n -e 's/Best: *\(.*\)\/bin\/javac//p')

which will dynamically set the $JAVA_HOMEto the JDK selected by update-alternatives.

这将动态地设置$JAVA_HOME到所选择的JDK update-alternatives

回答by user271777

In Debian/Ubuntu/Linux Mint, we could add to .bashrc export JAVA_HOME=$(update-java-alternatives -l | head -n 1 | sed 's/\s//g')

在 Debian/Ubuntu/Linux Mint 中,我们可以添加到 .bashrc export JAVA_HOME=$(update-java-alternatives -l | head -n 1 | sed 's/\s//g')