Eclipse Mars:ANT 不再支持 JDK 1.6?

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

Eclipse Mars: ANT doesn't support JDK 1.6 anymore?

javaeclipseant

提问by Patrick

When I use JDK6 for calling an ANT script in Eclipse Mars (Run as > Ant Build ... > JRE > Separate JRE), I get following error message:

当我使用 JDK6 在 Eclipse Mars 中调用 ANT 脚本时(运行方式 > Ant Build ... > JRE > 分离 JRE),我收到以下错误消息:

Problem occured: JRE version less than 1.7 is not supported.

出现问题:不支持低于 1.7 的 JRE 版本。

Is this only a bug or intentionally? I can't find a corresponding bug report at Eclipse. So Eclipse dropped Java 6 support for ANT?!

这只是一个错误还是故意的?我在 Eclipse 中找不到相应的错误报告。所以 Eclipse 放弃了对 ANT 的 Java 6 支持?!

回答by rjahn

We fixed the problem with a custom ANT plugin. It's a replacement of the Mars bundled ANT plugin. The original plugin didn't support Java < 7 because it was written with Java 7 syntax and it had a check for Java version. It was easy to replace Java7 syntax to be compatible with >= 5 and to remove the Java 7 check.

我们使用自定义 ANT 插件修复了该问题。它是 Mars 捆绑的 ANT 插件的替代品。原始插件不支持 Java < 7,因为它是用 Java 7 语法编写的,并且它检查了 Java 版本。很容易替换 Java7 语法以与 >= 5 兼容并删除 Java 7 检查。

The two syntax "problems" were:

两个语法“问题”是:

  • Diamond operator, e.g. List<MyObject> list = new ArrayList<>();
  • try-with-resources, e.g. try (InputStream stream = createInputStream()) { ...}
  • 菱形运算符,例如 List<MyObject> list = new ArrayList<>();
  • 尝试资源,例如 try (InputStream stream = createInputStream()) { ...}

Backwards compatibility for Diamond operator:

Diamond 运算符的向后兼容性:

List<MyObject> list = new ArrayList<MyObject>();

and for try-with-resources:

和尝试资源:

InputStream stream;

try
{
  ...
}
finally
{
   stream.close();
}

After we replaced the bundled plugin with our custom plugin, it was possible to start an ANT task with a custom JRE, as usual.

在我们用自定义插件替换捆绑插件后,可以像往常一样使用自定义 JRE 启动 ANT 任务。

It's possible to create your own ANT plugin with original sources from Eclipse git repository: http://git.eclipse.org/c/platform/eclipse.platform.git/refs/tags(use Tag ID: I20150430-1445) or to use my pre-compiled bundle: Eclipse Mars ANT plugin with support for Java < 7

可以使用来自 Eclipse git 存储库的原始源创建自己的 ANT 插件:http://git.eclipse.org/c/platform/eclipse.platform.git/refs/tags(使用标签 ID:I20150430-1445)或使用我的预编译包:Eclipse Mars ANT plugin with support for Java < 7

Installation is easy:

安装很简单:

  • Download the zip archive*, extract the content to <eclipse_dir>/plugins.
  • Start eclipse with parameter -clean (only once)
  • Configure JRE6 for your ANT task, via Externals Tool configuration...
  • 下载 zip 存档*,将内容解压缩到 <eclipse_dir>/plugins。
  • 使用参数 -clean 启动 eclipse(仅一次)
  • 通过外部工具配置为您的 ANT 任务配置 JRE6 ...

More details about the solution can be found in this blog post.

有关该解决方案的更多详细信息,请参阅此博客文章

回答by OzgurH

There's something rather messed up about this!? My (Windows 7) environment is like this:

这有什么好乱的!?我的(Windows 7)环境是这样的:

  • Eclipse Neon R2 (4.6.2)
  • JDK 1.8 to start Eclipse
  • JDK 1.6 as default JRE in Eclipse
  • Eclipse 霓虹灯 R2 (4.6.2)
  • JDK 1.8 启动 Eclipse
  • JDK 1.6 作为 Eclipse 中的默认 JRE

In this configuration, one "solution"(!) to this problem is to run the build file using the keyboard shortcut (or RMC->Run as->Ant Build), without any customization in the "External Tools Configuration" dialogs!?

在此配置中,此问题的一个“解决方案”(!)是使用键盘快捷键(或 RMC->Run as->Ant Build)运行构建文件,而无需在“外部工具配置”对话框中进行任何自定义!?

The build file works fine when I use the keyborard shortcut (Alt-Shift-X, Q), (and it does start javaw, and javacfrom JDK 1.6 as I've observed from Process Explorer), but once I do a modification to the "launch configuration" (say choose a new target for example), then I also start getting the "JRE version less than 1.7 is not supported" error!?

当我使用键盘快捷键(Alt-Shift-X,Q)时,构建文件工作正常(并且它确实从 JDK 1.6开始javawjavac正如我从 Process Explorer 观察到的那样),但是一旦我对“启动配置”(例如选择一个新目标),然后我也开始收到“不支持低于 1.7 的 JRE 版本”错误!?

If, after getting the error, I go "External Tools Configuration..." -> "Delete selected launch configuration(s)", and start the build with the keyboard shortcut, it works again!?

如果在收到错误后,我转到“外部工具配置...”->“删除选定的启动配置”,并使用键盘快捷键开始构建,它又可以正常工作了!?

Apparently the "JDK must be >= 1.7" check (of the default ant plug-in) mentioned by @rjahn above doesn't always get executed, but I haven't debugged it all the way to see why...

显然上面@rjahn 提到的“JDK 必须>= 1.7”检查(默认蚂蚁插件)并不总是被执行,但我没有一直调试它以了解为什么......

回答by voji

Java 1.7 required to launch ant build environment, but you can compile class files with any JDK. By default ant using javac and javaw from launch environment, but you can override it within ant tasks.

启动 ant 构建环境需要 Java 1.7,但您可以使用任何 JDK 编译类文件。默认情况下,ant 在启动环境中使用 javac 和 javaw,但您可以在 ant 任务中覆盖它。

I used the following constants in the examples:

我在示例中使用了以下常量:

<property name="javac.location" value="c:/Program Files/Java/jdk1.6.0_45/bin/javac.exe" />
<property name="java.location" value="c:/Program Files/Java/jdk1.6.0_45/bin/javaw.exe" />

Java compilation:You can define javac location parameter named "executable":

Java编译:您可以定义名为“executable”的javac位置参数:

<javac srcdir="@{source.dir}" destdir="@{target.dir}" debug='@{debug}' encoding="UTF-8" fork="true" source="@{source}" target="@{target}" executable="${javac.location}">

After this, ant use javac to compile class files from JDK 1.6.

此后,ant 使用 javac 编译来自 JDK 1.6 的类文件。

To run application from ant, with java 1.6, use jvm argument for java task:

要从 ant 运行应用程序,使用 java 1.6,对 java 任务使用 jvm 参数:

<java classname="com.google.gwt.dev.Compiler" fork="yes" failonerror="true" maxmemory="${gwt.compile.maxmemory}" jvm="${java.location}">

Some ant task use the default compiler by default, for example the wsimport-ant task (it's generate source files from wsdl and compile it, with default java). To prevent this, run wsimport with -Xnocompile argument, and compile generated source files with javac (see above).

一些 ant 任务默认使用默认编译器,例如 wsimport-ant 任务(它从 wsdl 生成源文件并编译它,使用默认的 java)。为防止出现这种情况,请使用 -Xnocompile 参数运行 wsimport,并使用 javac 编译生成的源文件(见上文)。

<wsimport-ant xadditionalHeaders="true">
    <xjcarg value="-XautoNameResolution" />
    <arg value="-d" />
    <arg value="${src-gen.dir}/wsdls" />
    <arg value="-keep" />
    <arg value="@{wsdlsource}" />
    <arg value="-Xnocompile" />
</wsimport-ant>

This methods work flawlessly in latest eclipse (Neon .3) with Oracle JDK 1.6 (or any other JDK).

这种方法在带有 Oracle JDK 1.6(或任何其他 JDK)的最新 Eclipse(Neon .3)中完美运行。

回答by Patrick

Eclipse Mars dropped support for Java 6 :(

Eclipse Mars 不再支持 Java 6 :(

So we have to change to IntelliJ IDEA.

所以我们不得不改用IntelliJ IDEA。

See:

看:

回答by nitind

Java 6 and 7 no longer receive public updatesfrom Oracle. While you're fine compiling against the runtime libraries from older versions to suit those requirements, you should use a newer JRE to actually run anything.

Java 6 和 7 不再接收来自 Oracle 的公开更新。虽然您可以根据旧版本的运行时库进行编译以满足这些要求,但您应该使用较新的 JRE 来实际运行任何东西。

回答by grumpy

This is just plain stupidity. We also either have to switch back to older version of eclipse or switch to other IDE. Legacy systems still run in 1.6 and when you can create project with 1.6 jre in Mars,ant should also be able to compile it natively against 1.6.

这简直是​​愚蠢至极。我们还必须切换回旧版本的 eclipse 或切换到其他 IDE。旧系统仍然在 1.6 中运行,当您可以在 Mars 中使用 1.6 jre 创建项目时,ant 也应该能够针对 1.6 进行本地编译。

The other solution is to update your build.xml where ever you call javac. In a corporate build it would be 100s of places.

另一个解决方案是在您调用 javac 的任何地方更新您的 build.xml。在企业建筑中,它将有 100 个位置。

Seems every eclipse version screws up some basic things.

似乎每个 eclipse 版本都搞砸了一些基本的事情。