FlymakeJava

You will need these lines in your .emacs file

(require 'flymake)
(add-hook 'java-mode-hook 'flymake-mode-on)

Adhoc single files

To use flymake with individual Java files use the following init function, along with the stock flymake-simple-cleanup function:

(defun my-java-flymake-init ()
  (list "javac" (list (flymake-init-create-temp-buffer-copy
                       'flymake-create-temp-with-folder-structure))))

Then, to use these functions:

(add-to-list 'flymake-allowed-file-name-masks '("\\.java$" my-java-flymake-init flymake-simple-cleanup))

Java syntax checking with Make

This is the command that stock flymake.el runs on a Java source file:

make -s -C ~/src/java/hello/ CHK_SOURCES=/tmp/../hello.java SYNTAX_CHECK_MODE=1 check-syntax

To get this to work, add this stanza to your Makefile:

.PHONY: check-syntax

check-syntax:
	javac -Xlint $(CHK_SOURCES)

I also add the following. Typing “make test-fast” will compile and run the most recently modified Java program:

RECENT_JAVA := $(shell ls -1t *.java | head -1)

test-fast: $(basename $(RECENT_JAVA)).run

%.run:	%.class
	java $(basename $<)
%.class:	%.java
	javac $<

To make sure Flymake is working correctly, and to tune your experience, try adding these lines to your ~/.emacs.d/init.el file:

(when (require 'flymake)
  (set-variable 'flymake-log-level 9)
  (setq flymake-start-syntax-check-on-newline nil)
  (setq flymake-no-changes-timeout 10)
  (add-hook 'java-mode-hook 'flymake-mode-on))

Java syntax checking with JDEE

If you are using the JavaDevelopmentEnvironment, check JdeeFlymake.

- NascifAbousalhNeto

(JdeeFlymake and thealternative did not work for me and after some hacking I came up with a workable situation: FlyMakeJdeeEclipse

- jwickers

I have also posted some instructions on how you can tweak the ECJ options to be a little more useful, and how to integrate this with another checker such as check style: http://jwickers.wordpress.com/2008/10/13/tweaking-flymake-for-java/

If you are not using jdee, but still want to run flymake for java you can try to use ecj flymake


Comments

Flymake does not like lower-cased makefile names. I had to rename it to “Makefile” to make flymake stop complaining about a missing makefile. Very strange.


FlyMake CategoryJava