Friday, October 7, 2011

Emacs and J2EE on OSX notes

Here's how I've managed to setup Emacs for J2EE / Tomcat / Maven application development on OSX. 

Install Emacs

I use Emacs 23.3 from Emacs for OSX.

Malabar mode

Malabar mode offers easy code navigation, Maven, JUnit and Groovy integration. Download release 1.4 and extract in a directory to build it.

As the build will invoke emacs in batch mode, be sure that Emacs 23.3 is in your path (instead of using the one packaged with OSX).

What works on my machine:
cd espenhw-malabar-mode-1daaee4
export PATH=/Applications/$PATH
mvn package -DskipTests=true
Now in target directory there's a Unzip it where you put all your emacs librairies:

cd ~/.emacs.d/
unzip /path/to/malabar/target/

Configure Emacs for Malabar

Open your .emacs and add:
(add-to-list 'load-path (expand-file-name "~/.emacs.d/malabar-1.4.0/lisp/")) 

(require 'cedet)
(require 'malabar-mode)
(setq malabar-groovy-lib-dir "/path/to/malabar/lib")
(add-to-list 'auto-mode-alist '("\\.java\\'" . malabar-mode))

(semantic-mode 1)
(require 'malabar-mode)
(setq malabar-groovy-lib-dir "~/.emacs.d/malabar-1.4.0/lib"
      malabar-load-source-from-sibling-projects t
      '( "/absolute/path/to/project1/src" 
        "/absolute/path/to/project2/src" ) ) ;; you need to setup this 

(add-to-list 'auto-mode-alist '("\\.java\\'" . malabar-mode))

(add-hook 'malabar-mode-hook
     (lambda () 
       (add-hook 'after-save-hook 'malabar-compile-file-silently
                  nil t)))

Now open a .java file within Emacs.

Try malabar-jump-to-thing on one of your class and it should open the corresponding file.

Open a test file and try malabar-run-test. It should run maven test for this file. If all tests OK but finished with "BUILD FAILURE", then see next section

Java troubleshooting

Malabar should use java from jdk, not jde. I've tried setting JAVA_HOME and configure malabar without success. So quick dirty hack is:

cd /usr/bin
mv java java.jre
ln -s  /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java


Entreprise Java Development With Emacs

Debugging Tomcat

Install JDIBug. In jdibug-expr.el, change the line (and to forget to byte-recompile or remove .elc):
(require 'semantic-java)
(require 'semantic/java)
Configure emacs:

(add-to-list 'load-path "~/.emacs.d/jdibug-0.5")
(setq jdibug-connect-hosts (quote ("localhost:8000"))
      jdibug-use-jde-source-paths nil
			 "/path/to/project2/src"  ))
(require 'jdibug)
Start Tomcat like with debugger enabled:
./bin/ jpda start
Then try jdibug-connect to attach Emacs to tomcat.


  1. Do you get any of the refactoring facilities that exist in typical JAVA IDEs? If not, how do you *live* without
    extract method
    extract local variable
    inline method/variable

  2. I can't stand eclipse / netbeans anymore. Feel at home in Emacs. That's my reason to change. I want to go fast.

  3. Did you already try all of the refactoring shortcuts and the ctrl+1 suggestions?

    I'm curious because I can understand the choice for any language but Java. My choice comes very much from the easy refactoring, code navigation, ctrl+1 suggestions, and code generation in general you get with Eclipse and I suppose Netbeans. I use the automated refactorings *a lot* so basically I wanted to know how far you went with the "Eclipse way" before deciding to go back to emacs. I'm sure you go faster with emacs but I don't know whether I would (with training).

  4. Personnally I prefer dynamic languages more than statically typed languages because of faster feedback. And I prefer object-oriented programming (Java is not).

    As an IDE that has automated refactorings I prefer Pharo.

    As an IDE that manipulates code instead of objects I prefer Emacs.

    Emacs and Pharo have both the ability to be changed / adapted to the context while you are using it, you cannot do that with Eclipse.