Tools and tips for Java development under Emacs

This document deals with configurations and short programs useful for Java/JSP development under Emacs/Linux. It requires a very good knowledge of Emacs and its configuration. It's kinda obsolete as I now use Eclipse which is so-many-times-better than Emacs for java programming (IMHO).


Running small standalone programs

It is always useful to be able to perform small programming tests from within Emacs; to that end, you can use The following allows to invoke it:

    (defun javacomp-standalone ()
      (save-some-buffers 1)
            (compile (concat " "
                             (buffer-file-name (current-buffer)))))

Following exceptions and spontaneous backtraces paths in Emacs

All programs have bugs. In Java, hopefully, the JVM will not crash on serious bugs, but display a backtrace and exit. The script allows to detect the latest backtrace and output it in a format suitable for compile of Emacs. Configure the top parameters in the script according to the locations and names of your classes and log files.

You can also want to spontaneously generate backtraces for debugging purposes. is an example class providing such a feature (note that throwing a fake Exception is not necessary anymore with java 1.5, but is mandatory up to java 1.4).

Invoke with the following:

    (defun get-trace (segfault)
      (compile (concat ""
               (if segfault " --segfault"))

Directly opening API documentation

Let's integrate API documentation better in Emacs. The goal is to be able to display API documentation for the class name under the point in current Emacs buffer. To do so, put the script find-java-api-doc.php in any php-enabled web server you have access to. It currently supports the J2SE, J2EE and Jakarta HttpClient classes and is not perfect for easy extension, but hey, it's already that, isn't it? You have to extract the classes in a format suitable for the script, in the same directory the php script is in. The following command will perform this extraction for HttpClient classes, for example:

    curl | perl -ne 'print "$1\t".lc($2)."\n" if m,<A HREF="([^"]+)" title="(?:class|interface).*>(?:<I>)?([^<]+)+(?:</I>)?</A>,'

You can allow invoking from Emacs with:

    (defun show-api-help ()
      (browse-url (concat "http://localhost/~username/find-java-api-doc.php?classname="

Final configuration

Finally, to integrate the previous niceties in java-mode, here's what you can do:

    (defmacro ilam (&rest body) `(lambda () (interactive) ,@body))
    (add-hook 'java-mode-hook 
    	  '(lambda ()
             (local-set-key [(control return)] (ilam (javacomp-standalone)))
    	     (local-set-key "\M-=" (ilam (get-trace nil)))
    	     (local-set-key "\M--" (ilam (get-trace t)))
    	     (local-set-key "\M-'" (ilam (show-api-help)))

You can add the following to prevent Emacs from completing class files:

    (setq completion-ignored-extensions (cons ".class" completion-ignored-extensions))

Editing JSPs

The best mode for editing JSPs I could find is multi-mode. This is far from perfect but this is already something. It will use java-mode, html-mode and indented-text-mode depending on where in the document the point is, with the following:

    (defun jsp-mode () (interactive)
      (multi-mode 1
                  '("<%--" indented-text-mode)
                  '("<%@" indented-text-mode)
                  '("<%=" html-mode)
                  '("<%" java-mode)
                  '("%>" html-mode)
                  '("<script" java-mode)
                  '("</script" html-mode)

Tue Mar 22 12:13:50 2005
Valid XHTML 1.0!