Emacs and Java (Linux)

Contents:


If you use Emacs, or Xemacs and program in Java, you may find the JDE a very helpful resource.

JDE give the user ability to compile and run Java applications within Emacs. In addition JDE can be used to customise your code better.


Installing JDK 1.2.2

  • First download SDK 1.2.2 for Linux from here.
  • Install SDK, ie. tar zxvf jdk1_2_2-linux-i386.tar.gz --directory=/opt .
  • Put the /opt/jdk1.2.2/bin/ directory in your path (depends on what shell you use).
  • Simply type java -version at the prompt and you should see:
    java version "1.2.2"
    Classic VM (build 1.2.2-L, green threads, nojit)
    


As for getting JDE to work with Emacs, this is what the final look will be:

Here is what Emacs looks like with the JDE. Notice the extra menus?

The split window shows an example of compiling a Java application.


I do not like Xemacs as much as Emacs, so I have not tested the following with Xemacs, but I presume it should work in the same way. If not, please refer to the documentation.

JDE Installation

  • Make sure JDK is installed (see above)

  • Now, download the JDE from here. I use JDE-2.1.6. You will want to untar this in a specific directory, ie: untar jde-latest.tar.gz --directory=/opt

  • The requirements for the JDE include the speedbar and semantic bovinator, from Eric Laudam's page. I downloaded speedbar v0.11 and semantic v1.1 . I untarred these packages into my Emacs Lisp directory, ie. /usr/share/emacs/site-lisp .

  • The last step is to configure your .emacs file. You can see my full .emacs file beneath, but it is essential you at least add the following line in your ~/.emacs file:
    (setq load-path  (nconc '("/path/to/jde-2.1.6/lisp") load-path))
    (require 'jde)
    
  • Restart Emacs and there you go! If you are editing a Java file (.java) the menus should pull up.

My .emacs file

(please change the specific directories and email addresses!) Note: ; donotes a comment. This works under the emacs -nw mode...
(setq inhibit-startup-message t)                ; inhibit start up messages
(display-time)                                  ; Show TIME
(setq display-time-day-and-date t)              ; Show date also.
(setq line-number-mode t)                       ; Show line number.
;(load "term/vt100")                            ; set term vt100
(set-default 'fill-column 78)                   ; Wrap at this column
(turn-on-auto-fill)                             ; AutoFill
(setq inhibit-startup-message t)                ; do not show
(setq lpr-switches '("-Pnetprinter" "-h"))      ; printer
(setq default-major-mode 'text-mode)            ; default mode
(setq require-final-newline 'yes)
(global-set-key [?\C-h] 'delete-backward-char)
(global-set-key [?\C-x ?h] 'help-command)
(global-unset-key "\C-d")
(define-key global-map "\C-d\C-g" 'goto-line)
(keyboard-translate ?\C-h ?\C-?)
(autoload 'c-mode "cc-mode")
(setq auto-mode-alist (cons '("\.js$" . c-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\.cgi$" . perl-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\.java$" . java-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\.php3$" . c-mode) auto-mode-alist))   
(setq auto-mode-alist (cons '("\.html$" . html-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\.cool$" . perl-mode) auto-mode-alist))

(add-hook 'c-mode-hook
        '(lambda ()
         (c-set-style "K&R")))
(add-hook 'perl-mode-hook 'turn-on-font-lock)
(add-hook 'html-mode-hook 'turn-on-font-lock)
(add-hook 'c-mode-hook 'turn-on-font-lock)
(add-hook 'c++-mode-hook 'turn-on-font-lock)
(add-hook 'java-mode-hook 'turn-on-font-lock)
(add-hook 'java-mode-hook 'toggle-transient-mark-mode)
(add-hook 'perl-mode-hook 'toggle-transient-mark-mode)
(add-hook 'html-mode-hook 'toggle-transient-mark-mode)
(add-hook 'c-mode-hook 'toggle-transient-mark-mode)
(add-hook 'c++-mode-hook 'toggle-transient-mark-mode)
;;(setq font-lock-face-attributes '((font-lock-comment-face "dark grey"))
(custom-set-variables
 '(user-mail-address "afra@prongs.org" t)
 '(query-user-mail-address nil))
(custom-set-faces)
;;; Set JDE variables
(custom-set-variables
 '(jde-run-applet-viewer "appletviewer")
 '(jde-compile-option-debug (quote ("all" (t nil nil))))
 '(jde-compile-option-sourcepath (quote ("./" "../")))
 '(jde-compile-option-deprecation t)
 '(jde-quote-classpath nil)
 '(jde-db-source-directories (quote (".")))
 '(jde-jdk-doc-url "http://java.sun.com/products/jdk/1.2/docs/api/overview-summary.html")
 '(jde-compile-option-sourcepath (quote ("./")))
 '(jde-appletviewer-option-vm-args (quote ("-classpath" "/opt/java_classes/")))
 '(jde-global-classpath (quote ("./" "../" "/opt/java_classes")))
 '(jde-enable-abbrev-mode t))
 (custom-set-faces)

(setq load-path 
          (nconc 
            '( 
               "/opt/jde-2.1.6/lisp" 
             ) 
             load-path))
(require 'jde)


Resources

I own these books and highly reccomend them to anyone learning Emacs, for beginners and advanced users.
(always trust the O'Reilly books):

Learning GNU Emacs (By Cameron, Debra / Rosenblatt, Bill / et al.) 533 Pages.
This is a superb book with a Quick Reference pullout page with for Emacs.
Did you know you could check your email, surf the web, learn more from the tutorials, etc, all within Emacs?
This book tells you how and how to also create custom Lisp packages.

Buying from here saves you 20% - $23.95 .

GNU Emacs Pocket Reference ( By Cameron, Debra ) - 64 pages.
This book explains most of the keystrokes in Emacs, and is always on my desk.

You'll save 20% from here - $7.15 .


Afra Ahmad 2000.
Homepage