简体   繁体   English

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

[英]Running Ant with JDK 1.6 on Mac OS X

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 -version in 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时遇到问题。即使在OS X的Java Preference中将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_HOME environment variable to specify which JDK to use. 我知道Ant依赖于JAVA_HOME环境变量来指定要使用的JDK。 However, I do not quite understand how this variable can be set on Mac OS X. 但是,我不太明白如何在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_HOME environment variable, how to set the variable on OS X. 因此,我的问题是如何在Mac OS X上使用JDK 1.6运行Ant。如果正确的方法仍然是设置JAVA_HOME环境变量,那么如何在OS X上设置变量。

The JAVA_HOME environment variable is set in your home directory's .profile file. JAVA_HOME环境变量在主目录的.profile文件中设置。 (/Users/ejel/.profile ?) Edit it and set it to what you want it to be. (/Users/ejel/.profile?)编辑它并将其设置为您想要的结果。 Eg: 例如:

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. 从这一点开始,每次打开一个新的终端窗口时,它都会将JAVA_HOME设置为这个新值。 It will not have changed with any existing open windows. 任何现有的打开窗口都不会改变它。

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. 如果您真的对将其置于配置文件中感到骇然,或者如果它与其他软件冲突,则可以始终在终端中手动运行export语句,或者进入在开始之前运行一次的脚本(例如:setj6ev.sh)运行蚂蚁任务。

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的话)。

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. Ted,使用Java Preferences应用程序不会更改/System/Library/Frameworks/JavaVM.framework/Versions中的CurrentJDK符号链接,如果未设置JAVA_HOME环境变量,Ant将使用该符号链接。 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. 因此,您可以手动更改该符号链接或设置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的版本。

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. 我希望这有帮助。

Explicitly setting the JAVA_HOME variable in your .profile/.bashrc/.zshrc isn't actually the recommended way to do it on the mac. 在.profile / .bashrc / .zshrc中明确设置JAVA_HOME变量实际上并不是在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). 有些程序我已经看到了一个明确设置JAVA_HOME到特定版本(例如,带有一些弹簧资源的grails 1.1)。

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. 在那里,您将要使用的java版本拖到顶部。 This will enable that version for all applications (both those run from the command line and those launched through GUI processes). 这将为所有应用程序启用该版本(包括从命令行运行的应用程序和通过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. 我实际上并不喜欢mac处理整个java符号链接目录和文件集的方式。 It's not obvious and people often screw it up. 这并不明显,人们常常搞砸了。

See the apple developer page on this for more details. 有关更多详细信息,请参阅Apple开发者页面

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM