Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically capture compiler errors from Eclipse

I am trying to write a program which can extract all the compiler errors (or other things like bad build paths) from Eclipse's "Problems" view.

I have not been able to find any documentation on how this might be done.

like image 683
himanshu Avatar asked Oct 05 '22 05:10

himanshu


1 Answers

I've got three possible solutions for you:

Don't use Eclipse

This is the easiest way to go. Eclipse is producing a visual presentation of build errors, and you want a structured presentation, so they're a bad match.

On the other hand, if you just run javac from the command-line, the output is easy to capture and relatively easy to parse right there. If you need to be more precise in parsing the errors, look at running JavaCompiler in your code and providing a custom DiagnosticListener to it.

If it's important that you capture some nuances of the Eclipse compiler, you can even run that from the command line (in batch mode). There's a nice blog post that describes the process here: ECJ - Eclipse Compiler for Java. Here's the short version: find the ECJ jar in your Eclipse distribution (mine is in plugins/org.eclipse.jdt.core_3.8.3.v20130121-145325.jar) and run it on the command line as

java -jar <ecj.jar> <sourcefiles>

Naturally, you can also run this from within Java for finer control.


Use GUI automation to fetch the errors

This will be clumsy, but could be useful for testing scenarios, where you want an automated tester to verify that an Eclipse project builds the way you expect.

You can use a tool like AutoHotKey or Sikuli or the AWT Robot to automatically fetch the errors. The simplest routine I can think of goes like this:

  1. Click on the "Problems" tab to bring it to the front
  2. Click on the "Errors" top-level node, selecting it
  3. Type the hot-key to copy; all the errors will be copied into the clipboard, one per line
  4. Paste or otherwise dump the clipboard into a file
  5. Repeat 2-4 for any other categories you want to capture, like "Warnings"

Rewrite the Eclipse plugin

Finally, if want you really need is some kind of real-time capture of error messages that's transparent to the user, then you're stuck with this.

You'll need to go into the Eclipse source code, probably the definition of the "Problems" view and modify it to pass your code information on the side. Then whoever needs this will have to use your custom-built version of Eclipse and not the standard version.

like image 160
Nathaniel Waisbrot Avatar answered Oct 13 '22 10:10

Nathaniel Waisbrot