java编译器 ant,Eclipse / Ant下的Java编译器默认为%JAVA_HOME%?

I recently encountered a build environment behavior that I need to better understand:

Overview of my freshly installed workstation:

Installed jdk1.6.0_45, then jdk1.7.0_80, then jdk1.8.0_131.

%JAVA_HOME% is set to C:\Program Files\Java\jdk1.8.0_131\

Installed Eclipse Kepler (4.3.2)

I then manually (by way of unzipping) added to C:\Program Files\Java the following: jdk1.7.0_45, jre1.7.0_76, jre1.7.0_79.

Checked out a legacy Ant-based project, designed to run under JRE7 only.

My Eclipse's Window > Preferences > Java > Installed JREs now looks:

ACWTM.png

Execution environments are standard (i.e. I have not added any):

5r4be.png

Now, when I right-click the project's build.xml and run that Ant Build... I can see that an execution environment that uses jre7 (jdk1.7.0_80) as its default, is selected:

Y94aX.png

So, when I click Apply, then Run, it will use a Java 7 compiler, right?

Wrong. For some strange reason, all .class files generated by this build have a major_version of Java 8!

I solved this problem by brute-forcing Ant to use Javac 1.7, via build.xml:

My question is: Why would a build in Eclipse running under JDK8 default to JRE8 despite Execution Environment set to JDK7?

IOW, is this a documented feature? If so, where can I learn more about this?

Update:

Thanks to the answer below, I tried to verify the role of %JAVA_HOME% in Ant's execution. Externally changing my workstation's %JAVA_HOME% system variable would be defeating the purpose of my setup, so I tried changing %JAVACMD% only. That didn't help. So, I echoed relevant env vars in my build.xml:

and this is what I got:

jdk_version_validation:

[echo] Java Version: 1.7.0_80

[echo] Java home: C:\Program Files\Java\jre7

[echo] JAVA_HOME: C:\Program Files\Java\jdk1.8.0_131\

[echo] JAVACMD: C:\Program Files\Java\jdk1.7.0_80\bin

Without the brute-force property overriding described above, this still produces Java 8 class files. Amazing.

解决方案

A lot of those things you list appear to be JREs (which don't contain a javac compiler). So Ant is probably falling back on using the 1.8 JDK listed in JAVA_HOME.

You could try using the JavaSE-1.7 Execution Evironment and actually select the one JDK you have as the default in the 'Compatible JREs' list.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值