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

19,296

Solution 1

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, e.g. List<MyObject> list = new ArrayList<>();
  • try-with-resources, e.g. 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.

Solution 2

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

Solution 3

Eclipse Mars dropped support for Java 6 :(

So we have to change to IntelliJ IDEA.

See:

Solution 4

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

Share:
19,296
Patrick
Author by

Patrick

Updated on June 04, 2022

Comments

  • Patrick
    Patrick almost 2 years

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