使用 Java 1.6 在 Eclipse Mars 上运行 Ant

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

Run Ant on Eclipse Mars with Java 1.6

javaeclipseanteclipse-mars

提问by Chandika Gunawardhana

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. But when I try to execute my ant target it creates an error:

我在 Eclipse 中配置了已安装的 JRE 以使用 Java 1.6。但是当我尝试执行我的 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?

回答by Badal

No you cannot go for JDK1.6 or less because Eclipse Mars only runs with Java >=1.7. Refer thislink.

不,您不能使用 JDK1.6 或更低版本,因为 Eclipse Mars 只能在 Java >=1.7 下运行。参考这个链接。

回答by greg-449

The Java you use to run Eclipse does not have to be the same as the one you use for your projects. You must run Eclipse Mars using Java 7 (or 8) but you can use Java 6 for your projects.

用于运行 Eclipse 的 Java 不必与用于项目的 Java 相同。您必须使用 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 并将其设置为默认值(或在单个项目中选择它)。

回答by greg-449

please take a look at the <javac>reference https://ant.apache.org/manual/Tasks/javac.html#compilervaluesand add the following attributes to your <javac>-Task: compiler="javac1.6"source="1.6"target="1.6"executable="[path-to-jdk-1.6/bin/javac]"fork="true"taskname="javac1.6".

请查看<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"

回答by Amr ElAdawy

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

Open External Tools Configurations

打开外部工具配置

Then change the JRE

然后更改JRE

Change 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">

Change Task target to 1.6

将任务目标更改为 1.6

As per suggested from @dag and @Chris, Here is updated ant javac task.enter image description here

根据@dag 和@Chris 的建议,这里是更新的 ant javac 任务。在此处输入图片说明

回答by rjahn

We fixed the problem for us using a patched Ant plugin. For Eclipse Neon, also see this linkas is noted in the comments on the first page.

我们使用修补过的 Ant 插件为我们解决了这个问题。对于 Eclipse Neon,还可以查看此链接,如第一页的评论中所述。

回答by Adam

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. I used some suggestions above but also encountered some strange behavior during the Ant build process. In the end it worked, here were my steps:

我最近在我的机器上使用 Java 8,使用 Elicpse Oxygen 并尝试使用 Ant 构建 Java 6 项目时遇到了这个问题。我使用了上面的一些建议,但在 Ant 构建过程中也遇到了一些奇怪的行为。最后它奏效了,这是我的步骤:

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 级别的构建。

回答by Barney

I have Eclipse Oxygen running on JRE 1.8 but building some old 1.7 projects, and have jdk1.7.0_40installed 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.launchfile, 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 支持方面是前进还是后退,但它解决了我的问题。