bash mvn -version 错误:我们无法执行 /usr/libexec/java_home/bin/java

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

mvn -version error: We cannot execute /usr/libexec/java_home/bin/java

javamacosbashmaven.bash-profile

提问by Tai

====== EDIT 2:

====== 编辑 2:

I used the same code as mentioned below in edit 1, my error apparently was not in the code itself but rather due to source ~/.bash_profile not updating my file as expected. I think, however (thank you @eckes) please see the maven bug:( BTW: jira.codehaus.org/browse/MNG-5686 – eckes 39 secs ago)

我在编辑 1 中使用了与下面提到的相同的代码,我的错误显然不在代码本身中,而是由于源 ~/.bash_profile 没有按预期更新我的文件。我认为,但是(谢谢@eckes)请查看 maven 错误:(顺便说一句:jira.codehaus.org/browse/MNG-5686 – eckes 39 秒前)

EDIT 1: this is not a duplicate of: What should I set JAVA_HOME to on OSX

编辑 1:这不是重复:我应该在 OSX 上将 JAVA_HOME 设置为什么

I have tried all of the solutions mentioned in the above post, and they do not work for me. My issue is that I am doing one of the solutions listed there here (I tried the others). I am still getting the error.

我已经尝试了上面帖子中提到的所有解决方案,但它们对我不起作用。我的问题是我正在执行此处列出的解决方案之一(我尝试了其他解决方案)。我仍然收到错误。

I'm currently trying as per above instructions:

我目前正在按照上述说明进行尝试:

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

and getting:

并获得:

  source ~/.bash_profile
  mvn -version
Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/libexec/java_home/bin/java

======= I installed the latest java jdk from oracle

======== 我从oracle安装了最新的java jdk

java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

I installed Xcode to install maven however recent documentation states that I may use brew (Maven Install on Mac OS X)

我安装了 Xcode 来安装 maven,但是最近的文档指出我可以使用 brew(Mac OS X 上的 Maven 安装

I installed maven with:

我安装了 maven:

//install brew
brew doctor
brew install maven

I then tried:

然后我尝试:

mvn -version
     Error: JAVA_HOME is not defined correctly.

I found: http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac

我发现:http: //hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac

However this is based on java 7 I believe. But it seemed likely to be a fix.

但是,我相信这是基于 java 7 的。但这似乎是一个修复。

There is no bash_profile on the new mac (https://apple.stackexchange.com/questions/26928/no-bash-profile-in-os-x-lion)

新 Mac 上没有 bash_profile ( https://apple.stackexchange.com/questions/26928/no-bash-profile-in-os-x-lion)

So I created and edited my bash_profile with:

所以我创建并编辑了我的 bash_profile :

touch ~/.bash_profile
vim ~/.bash_profile
//and added the line export JAVA_HOME=(/usr/libexec/java_home)

My problem is that even with this fix I am still getting the following error:

我的问题是,即使使用此修复程序,我仍然收到以下错误:

 mvn -version
      Error: JAVA_HOME is not defined correctly.
      We cannot execute /usr/libexec/java_home/bin/java

I checked however that within my file system java_home is where the file lies. There is no "/usr/libexec/java_home/bin/java" in my file system as maven error'ed out on.

然而,我检查了我的文件系统 java_home 是文件所在的位置。我的文件系统中没有“/usr/libexec/java_home/bin/java”,因为 maven 出错了。

Has anyone else encountered this with the new java 8, and brew installed maven? I'm using a MacBook Pro (Retina, 13-inch, Mid 2014) running Yosemite. I've installed a setup like this before but never encountered this issue.

有没有其他人在新的 java 8 中遇到过这个问题,并且 brew 安装了 maven?我正在使用运行 Yosemite 的 MacBook Pro(Retina,13 英寸,2014 年中)。我以前安装过这样的设置,但从未遇到过这个问题。

**Note:

**笔记:

I found this: http://www.mkyong.com/java/maven-java_home-is-not-defined-correctly-on-mac-osx/

我发现了这个:http: //www.mkyong.com/java/maven-java_home-is-not-defined-correctly-on-mac-osx/

Which I thought would definitely be my fix. Correct me if I'm wrong but I believe the difference is the parenthesis.

我认为这绝对是我的解决方案。如果我错了,请纠正我,但我相信区别在于括号。

Ex:

前任:

 export JAVA_HOME=/usr/libexec/java_home

vs

对比

 export JAVA_HOME=(/usr/libexec/java_home)

However my error has not been fixed. (Also just in case anyone asks I source(ed) it before rerunning.) It did however change my error. It removed the additional /bin/java.

但是我的错误没有得到修复。(也以防万一有人在重新运行之前询问我来源(编辑)它。)但是它确实改变了我的错误。它删除了额外的 /bin/java。

 mvn -version
      Error: JAVA_HOME is not defined correctly.
      We cannot execute /usr/libexec/java_home/bin/java

JAVA_HOME:

JAVA_主页:

 echo $JAVA_HOME
      /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home

**note i tried the $() syntax and this did not fix the problem.

**注意我尝试了 $() 语法,但这并没有解决问题。

回答by RealSkeptic

You missed on one little thing, I believe:

你错过了一件小事,我相信:

export JAVA_HOME=$(/usr/libexec/java_home)
                 ??
          This dollar sign

This tells the shell "The value of this expression is the output from executing the following command (/usr/libexec/java_home))"

这告诉shell“这个表达式的值是执行以下命令的输出(/usr/libexec/java_home))”

Running the command gives the proper java home for the mac (if you have a single JDK installed), and that value is placed in JAVA_HOME. Without the dollar sign, it just puts the name of the command there.

运行该命令会为 mac 提供正确的 java home(如果您安装了单个 JDK),并且该值放置在 JAVA_HOME 中。没有美元符号,它只是把命令的名字放在那里。

回答by Reza Dehnavi

I write on .bash_profile file these lines and then work for me.

我在 .bash_profile 文件上写下这些行,然后对我来说有效。

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=/Users/(YOUR USER)/Documents/apache-maven-3.0.3/bin:$PATH
export PATH=$JAVA_HOME/jre/bin:$PATH

Note: work on macOS Sierra (10.12.6)

注意:适用于 macOS Sierra (10.12.6)

回答by Shubham Verma

Follow this link : https://stackoverflow.com/a/44564303/5005676If you see the mvn script: The code fails here ---

按照此链接:https: //stackoverflow.com/a/44564303/5005676如果您看到 mvn 脚本:代码在此处失败 ---

Steps for debugging and fixing:

调试和修复步骤:

Step 1: Open the mvn script /Users/Username/apache-maven-3.0.5/bin/mvn (Open with the less command like: less /Users/Username/apache-maven-3.0.5/bin/mvn)

第 1 步:打开 mvn 脚本 /Users/Username/apache-maven-3.0.5/bin/mvn(使用 less 命令打开,例如:less /Users/Username/apache-maven-3.0.5/bin/mvn)

Step 2: Find out the below code in the script:

第 2 步:在脚本中找出以下代码:

  if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD="`which java`"
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

Step3: It is happening because JAVACMD variable was not set. So it displays the error.

Step3:发生这种情况是因为未设置 JAVACMD 变量。所以它显示错误。

Note: To Fix it

注意:修复它

export JAVACMD=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/java

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/

Key: If you want it to be permanent open emacs .profile

关键:如果你想让它永久打开 emacs .profile

post the commands and press Ctrl-x Ctrl-c ( save-buffers-kill-terminal ).

发布命令并按 Ctrl-x Ctrl-c ( save-buffers-kill-terminal )。

回答by Pankaj Avhad

In my case, I had two entries for JAVA_HOME in my ~/.bash_profile and the last one was taking precedence. I just deleted the one which was wrong and it worked.

就我而言,我的 ~/.bash_profile 中有两个 JAVA_HOME 条目,最后一个优先。我刚刚删除了错误的那个并且它起作用了。