简体   繁体   中英

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

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

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

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?!

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.

The two syntax "problems" were:

  • Diamond operator, eg List<MyObject> list = new ArrayList<>();
  • try-with-resources, eg try (InputStream stream = createInputStream()) { ...}

Backwards compatibility for Diamond operator:

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.

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

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...

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

Eclipse Mars dropped support for Java 6 :(

So we have to change to IntelliJ IDEA.

See:

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

  • Eclipse Neon R2 (4.6.2)
  • JDK 1.8 to start Eclipse
  • JDK 1.6 as default JRE in Eclipse

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 !?

The build file works fine when I use the keyborard shortcut (Alt-Shift-X, Q), (and it does start javaw , and javac from 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!?

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...

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.

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

<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.

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

<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).

<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).

Java 6 and 7 no longer receive public updates from 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.

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.

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

Seems every eclipse version screws up some basic things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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