Browse files

Migrated another old article, updated links

  • Loading branch information...
1 parent a36040e commit 66ea8c358a70856641807ec3d16c54b56724b26e @credmp committed Jan 1, 2013
Showing with 104 additions and 0 deletions.
  1. +96 −0 _posts/
  2. +8 −0
@@ -0,0 +1,96 @@
+tags: [GNU Emacs, Java]
+title: "on-the-fly syntax checking Java in Emacs"
+image: /assets/post_images/emacs-flymake-success.gif
+image_credit: Commodore 64
+layout: post
+A couple of days ago I wrote [about using fly-make](
+Even though I had only used flymake with C++ code, I tend to do most of my work related coding in Java.
+Curiosity ([it seems I am not the only one]( led me to the
+[EmacsWiki]( in order to find elisp code that will allow me to use flymake with Java. Turns out there are
+a couple of options, of course none of them worked (just my luck), but
+I will share them nonetheless:
+ <a href="">JdeeFlymake</a>
+ </dt>
+ <dd>
+ A flymake extension for Jdee that uses jikes. Since jikes seems to have died when IBM adopted Eclipse this mode is pretty useless except for code references.
+ </dd>
+ <dt>
+ <a href="">Jdee Eclipse Compiler Server</a>
+ </dt>
+ <dd>
+ Using Eclipse's 'JDT Core Batch Compiler' this extension can run a compile server or do a-process-per-time checking. This actually came very close to working, however it hung my Emacs for very strange reasons, however it gave me enough of a start to get to a working solution.
+ </dd>
+Since none of the provided means worked, what is a good coder to do? Right! Write your own. Taking a look at the already existing modes I found that creating a 'a-process-per-time' check is actually quite easy and on my MacBook it is hardly noticeable. So here is the lisp code that I added to my .emacs:
+{% highlight scheme %}
+(require 'flymake)
+(defun flymake-java-ecj-init ()
+ (let* ((temp-file (flymake-init-create-temp-buffer-copy
+ 'jde-ecj-create-temp-file))
+ (local-file (file-relative-name
+ temp-file
+ (file-name-directory buffer-file-name))))
+ ;; Change your ecj.jar location here
+ (list "java" (list "-jar" "/path/to/ecj.jar" "-Xemacs" "-d" "/dev/null"
+ "-source" "1.5" "-target" "1.5" "-proceedOnError"
+ "-sourcepath" (car jde-sourcepath) "-classpath"
+ (jde-build-classpath jde-global-classpath) local-file))))
+(defun flymake-java-ecj-cleanup ()
+ "Cleanup after `flymake-java-ecj-init' -- delete temp file and dirs."
+ (flymake-safe-delete-file flymake-temp-source-file-name)
+ (when flymake-temp-source-file-name
+ (flymake-safe-delete-directory (file-name-directory flymake-temp-source-file-name))))
+(defun jde-ecj-create-temp-file (file-name prefix)
+ "Create the file FILE-NAME in a unique directory in the temp directory."
+ (file-truename (expand-file-name (file-name-nondirectory file-name)
+ (expand-file-name (int-to-string (random)) (flymake-get-temp-dir)))))
+(push '(".+\\.java$" flymake-java-ecj-init flymake-java-ecj-cleanup) flymake-allowed-file-name-masks)
+(push '("\\(.*?\\):\\([0-9]+\\): error: \\(.*?\\)\n" 1 2 nil 2 3 (6 compilation-error-face)) compilation-error-regexp-alist)
+(push '("\\(.*?\\):\\([0-9]+\\): warning: \\(.*?\\)\n" 1 2 nil 1 3 (6 compilation-warning-face)) compilation-error-regexp-alist)
+{% endhighlight %}
+As you might have noticed, you need ecj.jar. I just grabbed it from <a href="">the Eclipse download site</a>, it's a relatively small jar of 1.5 MB. What this code gives you is the ability to enable flymake-mode in an JDEE controlled java file and see the following happen when you make a mistake or just do something dumb, like create unused variables:
+Flymake itself comes with a function to display the error of a line in a nice X11 (or carbon for that matter) menu, however I am not one for the menus, so I wrote a simple function to display the error or warning in the minibuffer:
+{% highlight scheme %}
+(defun credmp/flymake-display-err-minibuf ()
+ "Displays the error/warning for the current line in the minibuffer"
+ (interactive)
+ (let* ((line-no (flymake-current-line-no))
+ (line-err-info-list (nth 0 (flymake-find-err-info flymake-err-info line-no)))
+ (count (length line-err-info-list))
+ )
+ (while (> count 0)
+ (when line-err-info-list
+ (let* ((file (flymake-ler-file (nth (1- count) line-err-info-list)))
+ (full-file (flymake-ler-full-file (nth (1- count) line-err-info-list)))
+ (text (flymake-ler-text (nth (1- count) line-err-info-list)))
+ (line (flymake-ler-line (nth (1- count) line-err-info-list))))
+ (message "[%s] %s" line text)
+ )
+ )
+ (setq count (1- count)))))
+{% endhighlight %}
+So, you could say I am a very happy camper now, this is a useful addition to my coding tool-chain and allows for writing cleaner code, also faster since I no longer have to wait for the build scripts to run through everything.
+Now, the only thing remains is documentation lookup methods for the Java API documentation.
@@ -5,6 +5,14 @@ title: Links
This page is an collection of interesting links.
+ * [Reddit](
+ * [Programming subreddit](
+ * [Java subreddit](
+ * [Emacs subreddit](

0 comments on commit 66ea8c3

Please sign in to comment.