macos 在 Mac OS X 上使用 JDK 1.6 运行 Ant

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

Running Ant with JDK 1.6 on Mac OS X

javamacosant

提问by ejel

I am having a problem running Ant with JDK 1.6 on Mac OS X. Even though Java application versions is set to Java SE 6 in OS X's Java Preference, executing java -versionin Terminal also shows java version "1.6.0_07", Ant still seems to use JDK 1.5 to be using JDK 1.5 as it does not see JDK 1.6 classes when compiling my code.

我在 Mac OS X 上使用 JDK 1.6 运行 Ant 时遇到问题。即使 Java 应用程序版本在 OS X 的 Java 首选项中设置为 Java SE 6,java -version在终端中执行也显示java version "1.6.0_07",Ant 似乎仍然使用 JDK 1.5 来使用 JDK 1.5因为它在编译我的代码时没有看到 JDK 1.6 类。

I understand that Ant relies on JAVA_HOMEenvironment variable to specify which JDK to use. However, I do not quite understand how this variable can be set on Mac OS X.

我知道 Ant 依赖于JAVA_HOME环境变量来指定要使用的 JDK。但是,我不太明白如何在 Mac OS X 上设置此变量。

Hence, my question is how to make Ant runs with JDK 1.6 on Mac OS X. If the correct way is still to set JAVA_HOMEenvironment variable, how to set the variable on OS X.

因此,我的问题是如何使 Ant 在 Mac OS X 上运行 JDK 1.6。 如果正确的方法仍然是设置JAVA_HOME环境变量,那么如何在 OS X 上设置变量。

回答by Stu Thompson

The JAVA_HOME environment variable is set in your home directory's .profile file. (/Users/ejel/.profile ?) Edit it and set it to what you want it to be. E.g.:

JAVA_HOME 环境变量在您的主目录的 .profile 文件中设置。(/Users/ejel/.profile ?) 编辑它并将其设置为您想要的。例如:

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

From this point onward, every time you open a new terminal window it will have JAVA_HOME set to this new value. It will not have changed with any existing open windows.

从现在开始,每次打开一个新的终端窗口时,它都会将 JAVA_HOME 设置为这个新值。它不会随着任何现有的打开窗口而改变。

If you are truly aghast to putting this in the profile, or if it conflicts with other software, the export statement could always be run in the terminal manually or go into a script (eg: setj6ev.sh) that is run once before you start running ant tasks.

如果你真的很讨厌把它放在配置文件中,或者它与其他软件冲突,导出语句总是可以在终端中手动运行或进入脚本(例如:setj6ev.sh),在你开始之前运行一次运行蚂蚁任务。

回答by Theo

I've added the line

我已经添加了该行

export JAVA_HOME=`/usr/libexec/java_home`

To my .zshrc file, it seems to do the trick (.bash_profile or whatever if you use bash).

对于我的 .zshrc 文件,它似乎可以解决问题(.bash_profile 或其他,如果你使用 bash)。

回答by Theo

Ted, using the Java Preferences app doesn't change the CurrentJDK symlink in /System/Library/Frameworks/JavaVM.framework/Versions, which is what Ant will use if the JAVA_HOME environment variable isn't set. Thus, you can either change that symlink manually or set the JAVA_HOME environment variable, but if you do neither, then Ant won't use the correct JDK.

Ted,使用 Java Preferences 应用程序不会更改 /System/Library/Frameworks/JavaVM.framework/Versions 中的 CurrentJDK 符号链接,如果未设置 JAVA_HOME 环境变量,Ant 将使用该符号链接。因此,您可以手动更改该符号链接或设置 JAVA_HOME 环境变量,但如果两者都不做,则 Ant 将不会使用正确的 JDK。

You can see the version of the jdk that Ant is using by issuing an <echo message="${ant.java.version}"/> in your build.xml file.

您可以通过在 build.xml 文件中发出 <echo message="${ant.java.version}"/> 来查看 Ant 正在使用的 jdk 版本。

回答by Ted Naleid

Explicitly setting the JAVA_HOME variable in your .profile/.bashrc/.zshrc isn't actually the recommended way to do it on the mac. There are programs that I've seen get hosed up with an explicitly set JAVA_HOME to a particular version (grails 1.1 with some spring resources for example).

在 .profile/.bashrc/.zshrc 中显式设置 JAVA_HOME 变量实际上并不是在 mac 上推荐的方法。我见过一些程序被显式设置为特定版本的 JAVA_HOME(例如,grails 1.1 和一些 spring 资源)。

The correct way to set the version of Java that you want to use is to use the /Application/Utilities/Java Preferences.app application.

设置要使用的 Java 版本的正确方法是使用 /Application/Utilities/Java Preferences.app 应用程序。

In there, you drag the version of java that you want to use to the top. This will enable that version for all applications (both those run from the command line and those launched through GUI processes).

在那里,您将要使用的 java 版本拖到顶部。这将为所有应用程序启用该版本(从命令行运行的应用程序和通过 GUI 进程启动的应用程序)。

You can test the current version by running this from the command line:

您可以通过从命令行运行来测试当前版本:

java -version

I don't actually like the way that the mac handles the entire set of java symlinked directories and files. It's not obvious and people often screw it up.

我实际上并不喜欢 mac 处理整个 java 符号链接目录和文件的方式。这并不明显,人们经常把它搞砸。

See the apple developer pageon this for more details.

有关更多详细信息,请参阅苹果开发者页面

回答by Fabien Barbier

I try everything, and only one thing works for me : unlink CurrentJDK, and link to 1.6 :

我尝试了所有方法,但只有一件事对我有用:取消链接 CurrentJDK,然后链接到 1.6:

cd /System/Library/Frameworks/JavaVM.framework/Versions   
sudo unlink CurrentJDK   
sudo ln -sF "1.6" CurrentJDK  

Finally I get :

最后我得到:

java -version   
java version "1.6.0_22"

I hope this help.

我希望这有帮助。

回答by Martin OConnor

You may need to open a new command prompt instance so that the shell can pick up any changes to the environment variables.

您可能需要打开一个新的命令提示符实例,以便 shell 可以获取对环境变量的任何更改。