简体   繁体   English

使用 Java 1.6 在 Eclipse Mars 上运行 Ant

[英]Run Ant on Eclipse Mars with Java 1.6

I downloaded the latest release of Eclipse (Mars) and changed the required Java version to 1.6 in eclipse.ini file as my project uses Java 1.6.我下载了最新版本的 Eclipse (Mars) 并在 eclipse.ini 文件中将所需的 Java 版本更改为 1.6,因为我的项目使用 Java 1.6。

I configured installed JREs inside Eclipse to use Java 1.6.我在 Eclipse 中配置了已安装的 JRE 以使用 Java 1.6。 But when I try to execute my ant target it creates an error:但是当我尝试执行我的 ant 目标时,它会产生一个错误:

JRE version less than 1.7 is not supported.不支持低于 1.7 的 JRE 版本。

Is there any workaround to use Java 1.6 in Mars version as I'm unable to upgrade to Java 1.7 at the moment?是否有任何解决方法可以在 Mars 版本中使用 Java 1.6,因为我目前无法升级到 Java 1.7?

I faced the same problem after upgrading to Eclipse Mars.升级到 Eclipse Mars 后,我遇到了同样的问题。

I solved this by changing the runtime environment of the external tool configuration of the project to JDK7 .我通过将项目的外部工具配置的运行时环境更改为 JDK7解决了这个问题。

I assume you know how to add JDK7 to your installed jre in eclipse我假设您知道如何在 Eclipse 中将 JDK7 添加到已安装的 jre 中

Open External Tools Configurations... and then change the JRE to JDK 1.7打开外部工具配置...,然后将 JRE 更改为 JDK 1.7

打开外部工具配置

Then change the JRE然后更改JRE

更改 JRE

But this will create another problem , the compiled jar will be in JDK 7 and this will not work on production servers with JDK6.但这会产生另一个问题,编译后的 jar 将在 JDK 7 中,这不适用于具有 JDK6 的生产服务器。

To solve this, simply change the target attribute in the task to be 1.6要解决这个问题,只需将任务中的目标属性更改为 1.6

<javac target="1.6"> <javac target="1.6">

将任务目标更改为 1.6

As per suggested from @dag and @Chris, Here is updated ant javac task.根据@dag 和@Chris 的建议,这里是更新的 ant javac 任务。在此处输入图片说明

We fixed the problem for us using a patched Ant plugin .我们使用修补过的 Ant 插件为我们解决了这个问题。 For Eclipse Neon, also see this link as is noted in the comments on the first page.对于 Eclipse Neon,还可以查看此链接,如第一页的评论中所述。

请查看<javac>参考https://ant.apache.org/manual/Tasks/javac.html#compilervalues并将以下属性添加到您的<javac> -Task: compiler="javac1.6" source="1.6" target="1.6" executable="[path-to-jdk-1.6/bin/javac]" fork="true" taskname="javac1.6"

I have Eclipse Oxygen running on JRE 1.8 but building some old 1.7 projects, and have jdk1.7.0_40 installed as a separate JRE and set up in the tools external config, but still got the "jre less than 1.8 not supported" error.我在 JRE 1.8 上运行 Eclipse Oxygen,但构建了一些旧的 1.7 项目,并且将jdk1.7.0_40作为单独的 JRE 安装并在工具外部配置中进行设置,但仍然出现“不支持小于 1.8 的 jre”错误。

What fixed it for me was just updating the build xml configuration directly, especially if you have another project which does work that you can copy from.对我来说修复它的只是直接更新构建 xml 配置,特别是如果您有另一个可以复制的工作项目。

Specifically, I went to the launch configurations at:具体来说,我去了启动配置:

workspace/.metadata/.plugins/org.eclipse.debug.core/.launches

And edited the relevant ...build.xml.launch file, replacing:并编辑了相关的...build.xml.launch文件,替换为:

<booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_40"/>

With:和:

<booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_INSTALL_NAME" value="jdk1.7.0_40"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_INSTALL_TYPE_ID" value="org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType"/>

And restarted Eclipse to pick it up.并重新启动 Eclipse 以获取它。

No idea if this is moving forward or backwards in terms of Eclipse support, but it fixed my problem.不知道这在 Eclipse 支持方面是前进还是后退,但它解决了我的问题。

No you cannot go for JDK1.6 or less because Eclipse Mars only runs with Java >=1.7.不,您不能使用 JDK1.6 或更低版本,因为 Eclipse Mars 只能在 Java >=1.7 下运行。 Refer this link.参考这个链接。

The Java you use to run Eclipse does not have to be the same as the one you use for your projects.用于运行 Eclipse 的 Java 不必与用于项目的 Java 相同。 You must run Eclipse Mars using Java 7 (or 8) but you can use Java 6 for your projects.您必须使用 Java 7(或 8)运行 Eclipse Mars,但您可以将 Java 6 用于您的项目。

Tell Eclipse about Java 6 in the Preferences in 'Java > Installed JREs' and set that as the default (or select it in individual projects).在“Java > Installed JREs”的首选项中告诉 Eclipse Java 6 并将其设置为默认值(或在单个项目中选择它)。

I recently ran into this issue with Java 8 being on my machine, using Elicpse Oxygen and trying to use Ant to build a Java 6 project.我最近在我的机器上使用 Java 8,使用 Elicpse Oxygen 并尝试使用 Ant 构建 Java 6 项目时遇到了这个问题。 I used some suggestions above but also encountered some strange behavior during the Ant build process.我使用了上面的一些建议,但在 Ant 构建过程中也遇到了一些奇怪的行为。 In the end it worked, here were my steps:最后它奏效了,这是我的步骤:

1) Java home ended up staying pointed to Java 8. 1)Java home 最终指向 Java 8。

2) Set in the Ant script the values suggested by @Chris. 2) 在 Ant 脚本中设置@Chris 建议的值。

3) Do not change the Ant Runtime JRE, mine was left at 8, and in fact would not run the Ant build if I changed it to 6... 3) 不要更改 Ant Runtime JRE,我的保留在 8,实际上如果我将其更改为 6,则不会运行 Ant 构建......

4) Project settings build path and compiler levels were all set to 6. 4)项目设置构建路径和编译器级别都设置为6。

5) Run the build. 5) 运行构建。

This produces a build at the Java 6 level that worked for me.这会生成一个对我有用的 Java 6 级别的构建。

Solution for me was to download an ant version compatible with JRE 6/7 and change in the "External Tool Configuration" the Ant Home Path to match the one I downloaded (ClassPath > Ant Home")我的解决方案是下载与 JRE 6/7 兼容的 ant 版本,并在“外部工具配置”中更改 Ant 主页路径以匹配我下载的路径(ClassPath > Ant Home”)

在此处输入图片说明

添加大于 1.7 的新 JRE 版本

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

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