macos JAVA_HOME 未正确定义 编译时出错

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

JAVA_HOME is not defined correctly Error while compiling

javacocoamacos

提问by MobX

I am developing an aplication in cocoa which uses some java classes .I am getting an error "JAVA_HOME is not defined correctly We cannot execute /System/Library/Frameworks/JavaVM.framework/Home/bin/java".I dont know how to resolve this..Please anyone help me..

我正在可可中开发一个使用一些 java 类的应用程序。我收到一个错误“JAVA_HOME 没有正确定义我们无法执行 /System/Library/Frameworks/JavaVM.framework/Home/bin/java”。我不知道如何解决这个..请任何人帮助我..

Thanks in advance

提前致谢

回答by Alex

export JAVA_HOME=`/usr/libexec/java_home` is exactly what you want to do.

In fact, all of the Apache projects (well, any project really) that hardcodes /System/Library/Frameworks/JavaVM.framework/Versions/... needs to use /usr/libexec/java_home if it exists. It's the only way you will know if Java is actually installed.

事实上,所有硬编码 /System/Library/Frameworks/JavaVM.framework/Versions/... 的 Apache 项目(实际上是任何项目)都需要使用 /usr/libexec/java_home 如果它存在。这是您知道是否实际安装了 Java 的唯一方法。

At some point in the future the symlinks in /System/Library/Frameworks/JavaVM.framework/Versions/ will be going away, which will even more severely break these projects if they want to load using an Oracle/OpenJDK JVM.

在未来的某个时候,/System/Library/Frameworks/JavaVM.framework/Versions/ 中的符号链接将消失,如果它们想使用 Oracle/OpenJDK JVM 加载,这将更严重地破坏这些项目。

回答by liwp

Set JAVA_HOMEto point to the .../Homedirectory, not the javabinary. E.g.

设置JAVA_HOME为指向.../Home目录,而不是java二进制文件。例如

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home

Why are you setting the environment variable in the first place? You shouldn't have to do this on OS X...

为什么要首先设置环境变量?你不应该在 OS X 上这样做......

回答by user3445140

In your .bashrc file, add below lines:

在您的 .bashrc 文件中,添加以下几行:

export JAVA_HOME=/usr
export PATH=$PATH:$JAVA_HOME

This worked for me.

这对我有用。

回答by shaunthomas999

Add the following line to ~/.mavenrcfile

将以下行添加到~/.mavenrc文件中

export JAVA_HOME=$(/usr/libexec/java_home)

Reference: Maven ignoring JAVA_HOME on OSX?

参考:Maven 在 OSX 上忽略 JAVA_HOME?

回答by jiaojiao wang

Some times changing Java_HOME does not help, because in apache ant script the java_home is hardcoded. If when you change the JAVA_HOME still failed to run ant, you can try to change the default hardcode to your JAVA_HOME in ant script, like:

有时更改 Java_HOME 没有帮助,因为在 apache ant 脚本中 java_home 是硬编码的。如果更改 JAVA_HOME 后仍然无法运行 ant,您可以尝试在 ant 脚本中将默认硬编码更改为您的 JAVA_HOME,例如:

    $ vi /Users/apache-ant-1.8.3/bin/ant 
    #ant from line 83 to 93
    case "`uname`" in
      CYGWIN*) cygwin=true ;;
      Darwin*) darwin=true
               if [ -z "$JAVA_HOME" ] ; then
 #delete JAVA_HOME=XXXXX(default line) , and give JAVA_HOME ur java home url       




JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
                           fi
                           ;;
                  MINGW*) mingw=true ;;
                esac

回答by Nikita

This can also happen because of the misconfigured jenv, in case it is installed.

jenv如果安装了错误配置,也可能会发生这种情况。

In the ~/.bash_profile, there should be these 3 lines:

在 中~/.bash_profile,应该有以下 3 行:

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
eval "$(jenv enable-plugin export)"

Then reload it by: source ~/.bash_profile

然后通过以下方式重新加载它: source ~/.bash_profile