M-x all-things-emacs

Emacs, JDEE, Ant, and the Eclipse Java Compiler

February 13th, 2007 by Rob Christie · 145 Comments

This post describes how to integrate jdee, ant, and the eclipse batch compiler to get the same warning and error messages as your Eclipse-loving friends. I am assuming that you already know your way around the jdee environment (i.e., you have it setup and configured such that you can run an ant build from within emacs), and the ant build tool.

  1. Download the jdt core batch compiler. You can install the entire eclipse application if you like, but it is easier and faster to just download the batch compiler and ant adapter (the size of the download is an order of magnitude smaller for the compiler only). It is named ecj.jar. If you have eclipse installed and want to use the installed compiler, then you will need to locate the org.eclipse.jdt.core_3.2.x.xxx.jar in the plugins directory of you eclipse installation.
  2. Install ant 1.7. You may be able to get away with using an older version, but the newer version allows you to specify additional parameters that you may need on the command line.
  3. Place the ecj.jar file (if you downloaded the compiler and ant adapter) in the <ANT_HOME>/lib directory, or make it available on the classpath so that it is available to ant. If you are using the eclipse jdt core jar, then make it available either by copying or adding to the ant classpath. You will also need to make the jdtCompilerAdapter.jar available to ant. It is bundled inside the jdt core jar (if you went this route).
  1. You are now ready to test your ant/eclipse compiler setup. Go to a java project that you want to compile. Use your normal compile target with the following additional parameter(s). Note: the ant.build.javac.target and ant.build.javac.source only need to be used if the default values are not what you desired.
ant -Dbuild.compiler=org.eclipse.jdt.core.JDTCompilerAdapter 
    -Dant.build.javac.source=1.5 compile

The build.compiler is self explanatory. The reason I suggested upgrading to ant version 1.7 or above is so that you have the ability to specify the source and target type on the command line. At this point you should be able to run ant with the additional properties from within emacs in whatever way you normally do, and it will use the eclipse compiler. If your ant build within emacs does not propmpt you for additional arguments, then customize the following variable: jde-ant-read-args. The first time you perform a build with the eclipse compiler, you will notice that the error messages are not formatted nicely in the compilation buffer. There are two ways that I have come up with for getting the compilation output formatted for emacs. First, you can use the compilerarg element in ant to pass compiler specific settings to the compiler. The eclipse compiler takes the parameter -Xemacs to format its output in the format the emacs compilation buffer expects. See the example below:

           line="-warn:+unused -Xemacs"/>
        <classpath refid="compile.classpath" />

The compilerarg element also allows you to pass in additional command line args to the eclipse compiler. The downside of the compilerarg approach is that you must modify your build.xml, which may not be an option on your project. An alternative approach is to change the compilation-error-regexp-alist in emacs to handle the eclipse formatting.

(setq compilation-error-regexp-alist
  (cons '("----------\n[0-9]+. \\(ERROR\\|WARNING\\) in \\(.*\\)\n (at line \\([0-9]+\\))\n\\(\\(.*\n\\)+?\\).*^+\n\\(.*\n\\)"
         2 3)

My compilation formatting example is taken directly from the following package: jde eclipse compiler server. The eclipse compiler server is also interesting, and I plan to try that out at some point; however, the projects that I work on are ant-driven, so the only benefit I would get from using the compiler server is if I got it working with jde-flymake to get continuous syntax checking … hmmm … maybe another day.

Tags: java · tips

145 responses so far ↓

  • 1 Alessandro // Apr 11, 2007 at 9:16 pm

    Good post !

    Do you know if its possible to force ant use ejc , even if javac statement already has another compiler set ?

  • 2 Rob Christie // Apr 12, 2007 at 5:29 am

    My understanding is that you can have multiple child compilerarg elements in the the javac element. Then depending on the compiler being used, the appropriate compiler arguments are picked up.

    If you invoke ant with -Dbuild.compiler=org.eclipse.jdt.core.JDTCompilerAdapter then that should override any settings within the build.xml since the command line option will set the ant property before any setting within the build.xml.

  • 3 credmp » Blog Archive » on-the-fly syntax checking Java in Emacs // Jul 20, 2007 at 6:37 pm

    [...] (it seems I am not the only one) led me to the Emacs wiki in order to find elisp code that will allow me to use flymake with Java. [...]

  • 4 Xan // Sep 16, 2009 at 9:23 pm

    hmmmm, very intresting!!!

  • 5 Angela Merkel // Mar 4, 2010 at 11:30 am

    Nice post, however the regexp didn’t work for me at least in combination with ant with the -e (for emacs output flag). In this case there is a \n more in the regexp (the second one).

  • 6 Rob Christie // Mar 4, 2010 at 2:57 pm


    Thanks for the update. I haven’t been doing any Java development in the past two years, but I assume some of the formatting from the eclipse compiler has changed.

  • 7 How to: Build Eclipse Java Project from Command Line | SevenNet // Dec 26, 2014 at 2:01 am

    […] complete AndrĂ©’s answer, an ant solution could be like the one described in Emacs, JDEE, Ant, and the Eclipse Java Compiler, as […]