Skip to main content

skip to main content

developerWorks  >  Java technology  >

Emacs a top-notch Java IDE? You bet!

JDE for Emacs, Java Mode, and Hilit Java turn Emacs into a full-strength development environment

developerWorks
Document options

Document options requiring JavaScript are not displayed

Discuss


Rate this page

Help us improve this content


Level: Introductory

Greg Travis (mito@panix.com), Freelance Java programmer

01 May 2001

Emacs is one of the most popular development environments in the history of software programming, so it's no surprise that there are a number of excellent Emacs tools for Java programming. In this article, Greg Travis examines three of the best tools to alter your Emacs environment into a killer Java-language integrated development platform -- Java Mode, Hilit Java, and the showpiece, the Java Development Environment for Emacs. He describes what they do, how to use them, and how to get them.

"Sure, I'd like to try Emacs, but I don't want to use UNIX," says one Java software developer. Another one complains, "I've used Emacs for Java programming before, but it's a bit primitive. Not like my other Java development tools."

Myths and misconceptions such as these may be keeping you from using Emacs, potentially a top-notch, full-fledged integrated development environment (IDE) for Java software programming.

If you haven't been using Emacs, you might want to give it a try for two reasons:

  • It's been under continuous development for many years, accruing useful contributions from hundreds of programmers.
  • It's free software.

Even if you have been using Emacs to develop Java programs, you may not have been using it to its fullest potential. Many of the Emacs devotees use only the command-line tools in the developer kit. But this isn't the only way to use Emacs to develop in the Java language. Using the JDE package, you can convert Emacs into an encompassing IDE that is completely integrated with the regular Emacs editing features.

And remember, you don't have to use UNIX to use Emacs. Emacs has been ported to just about everything. (The Resources section will help you find the version in the operating system or platform of your choice.)

For those interested, the next section will deliver a little general information about Emacs. If you're only interested in the tools, jump to the next section.

What is Emacs?

Emacs was created by Richard Stallman, the founder of the Free Software Foundation. Stallman created the first version as a modification to the TECO editor, running under ITS (the Incompatible Time-Sharing System) at MIT back in 1975.

Over the years, Emacs has existed in a variety of forms, including versions written by other teams. The most popular version is still Stallman's. Called GNU Emacs, it currently sports the staggering version number of 20.7.

What makes Emacs more than just a text editor is the fact that it has an extension language based on a LISP dialect called Emacs Lisp or Elisp. Using this extension language, you can easily create new features for Emacs without having to mess with the source code (which is written in C). Because of this feature, the number of useful packages that have been written for Emacs is immeasurable.

The GNU Project home page defines Emacs as an:

Extraordinarily powerful text editor with additional features including content sensitive major modes, complete online documentation, highly extensible through LISP, [with] support for many languages and their scripts, and a large number of other extensions available either separately or with the GNU Emacs distribution. [It] Runs on seven different operating systems regardless of machine.


Back to top


Java Mode

For just about any programming language in popular use, Emacs has a major mode, which augments the regular editing features with new features specific to that language. The Java language is no exception.

The syntax of the Java language is similar to the syntax of C and C++, so Java support is integrated into the standard CC Mode that comes bundled with the more recent versions of Emacs.

When you load a .java file into Emacs, you should see "Java" in the mode line at the bottom of the screen. If you see something else, such as Text or Fundamental, then something's wrong. (See Resources for links to documentation about CC Mode.)

One of the main features that Java Mode (and any CC Mode, for that matter) provides is automatic indenting. If you indent your code properly, you know that getting your cursor over to the right spot on a new line can take up a large portion of your daily keystrokes. With automatic indenting, all you have to do is hit the Tab key and Java Mode will zip your cursor to exactly the right spot.

Java Mode also provides commands for properly formatting an entire region of text (for example, M-x indent-region).

Installing Java Mode
If your version of Emacs doesn't have Java Mode pre-installed, you can easily do it yourself. First, you'll need to get the file java-mode.el (see Resources). Install this file somewhere on your system.

If you have the proper permissions, you can install the file in a public place, such as /usr/share/emacs/site-lisp. If you don't possess those permissions, you can install the file somewhere in your home directory.

You may need to add the directory containing java-mode.el to your load path, if it isn't already in there. Assuming that you've installed java.mode.el as ~/elisp/java-mode.el, add the following line to your .emacs file (which is generally found in your home directory):

(setq load-path (cons "~/home/elisp" load-path))

Once you've made sure that java-mode.el is installed on your system, add the following lines to your .emacs file:

;; Java Mode
(autoload 'java-mode "java-mode" "java mode" t nil)
(setq auto-mode-alist
      (append '(("\.java$" . java-mode)) auto-mode-alist))

Enjoy automatic indenting!



Back to top


Hilit Java

If you think Java Mode sounds like a cool tool, wait until you get a look at Hilit Java. Hilit Java does something called syntax coloring, which colors different parts of your file based on what functions the part performs and how it relates to the rest of the file.

To see this in action, first take a look at Figure 1, which contains a short code snippet.


Figure 1. Emacs code without syntax coloring
Emacs code without syntax coloring

With syntax coloring switched on, Emacs has the added burden of analyzing the syntax of your code and coloring various parts of it according to the "part of speech" each represents.

In Figure 2 you see the same code snippet enhanced with color. Keywords are purple, class names are green, comments are red, access modifiers are pink, member variables are orangey-gold, and package names are turquoise. Anything not falling into a special category is colored black.


Figure 2. Emacs code highlighted by syntax coloring
Emacs code highlighted by syntax coloring

Syntax coloring isn't just a gimmick. Because the color of the erroneous text will change (usually to black), syntax coloring gives you the ability to spot syntactical errors as soon as you type them. It can also make it easy to track down nasty unterminated-string and unterminated-comment errors.

Syntax coloring is a lot like the automatic spell-checking feature found in popular word processors, only the color/pattern recognition adds a new dimension to troubleshooting, making it easier to spot errors.

Installing Hilit Java

Installing Hilit Java is much like installing Java Mode. First, you install hilit-java.el somewhere in your load path. Then, you simply add this line to your .emacs file:

(load "hilit-java")

Here's to a colorful code-editing experience!



Back to top


The JDE

So far, we've examined Java Mode and Hilit Java, two tools that basically just facilitate the editing of code. Now I'd like to introduce a more comprehensive tool for crafting Java software.

The Java Development Environment for Emacs, or the JDE, is a complete integrated development environment for the Java language. It runs entirely inside Emacs, and you don't need any external applications to use it.

Although the JDE is full-featured, it may not contain all the features you'd find in the latest commercial packages. However, the advantage of the JDE is its ability to be fully integrated with Emacs. That way, the user gets all Emacs editing features, as well as the many code-development tools that have existed for Emacs for years, such as one-key compilation, symbol tags, keyword completion, and others.

And if you're willing and able, you can customize the JDE as much as you want by modifying the Emacs Lisp source code -- something you can't do with most traditional IDEs. Because programmers around the world are doing the same thing with Emacs Lisp, you can count on frequent, high-quality upgrades to this tool.

Installing the JDE

The JDE makes use of a couple of other packages -- speedbar and semantic by Eric Ludlam (see Resources) -- that you'll need to get first.

Once you have installed the requisite packages, you'll need to add some items to your .emacs file to get the JDE up and running. Listing 1 shows the .emacs file (it assumes that you've installed the packages into /usr/share/emacs/site-lisp).

; jde
(add-to-list 'load-path
  "/usr/share/emacs/site-lisp/speedbar-0.13a")
(autoload 'speedbar-frame-mode "speedbar"
  "Popup a speedbar frame" t)
(autoload 'speedbar-get-focus "speedbar"
  "Jump to speedbar frame" t)
(global-set-key [(f4)] 'speedbar-get-focus)
(add-to-list 'load-path
  "/usr/share/emacs/site-lisp/semantic-1.3.2")
; for C code integration
(require 'semantic-c)
; for Lisp integration
(require 'semantic-el)
; for Makefile integration
(require 'semantic-make)
; if you use imenu or wich-function
(require 'semantic-imenu)
(add-hook 'speedbar-load-hook
  (lambda () (require 'semantic-sb)))
; for speedbar integration
(autoload 'semantic-bnf-mode "semantic-bnf"
  "Mode for Bovine Normal Form." t)
; for editing .bnf parser files.
(add-to-list 'auto-mode-alist
  '("\.bnf$" . semantic-bnf-mode))
; for semantic-minor-mode
(autoload 'semantic-minor-mode "semantic-mode"
"Mode managing semantic parsing." t)
(add-to-list 'load-path (expand-file-name
  "/usr/share/emacs/site-lisp/jde-2.2.5/lisp"))
(require 'jde)

Caveat emptor: JDE doesn't have a Java developer kit

One thing that nearly all commercial IDEs have, but which JDE most emphatically does not have, is a bundled Java developer kit. This omission might seem strange to some users because a Java developer kit is the first thing you need to actually develop Java code. When it comes to JDE, you have to get one yourself.

You can, of course, use the developer kit you already have. In fact, you can probably use any reasonably up-to-date version that you can find because JDE makes use of the standard command-line tools to interface with the developer kit, ensuring wide compatibility.

This kind of tool independence strongly defines the spirit of the open-source movement.

(You can download various versions of the Java developer kit from IBM or from a variety of other sources, including Sun, CNET, and ZD Net; MRJ at Apple; and Java-Linux at Blackdown.)

Now that you're ready to go with the JDE, let's take a look at some of its major features.



Back to top


Dissecting the JDE environment

Once you've set up the JDE and necessary packages, you can restart Emacs and load up a Java file.

The first thing you'll notice is that you've got a bunch of new menus at the top of your Java buffer, including Classes, JDE, and Java.

These three menus are a comprehensive interface to the many features of the JDE. Because this is Emacs, many of the more commonly used functions are, of course, bound to keys sequences.

The JDE can deal with individual Java source files and it can also group Java files together in a project. The project menu (JDE->Project) allows you to load or save a project. It also provides access to a wealth of customization options, such as specifying additional command-line options for the Java tools or inserting Emacs Lisp hooks to be run at certain points in the development process.

Let's take a look at some the main features of the JDE.

Automatic code generation

For you lazy programmers (or rather, those who "work smart, not hard"), JDE provides a template mechanism that automates some of the more repetitive chores of Java programming.

There's a template for event listeners, another for automatically generating a get/set method pair, and even one for the simple println().

The documentation also tells you how you can create your own templates.

The speedbar

The JDE also incorporates a speedbar, shown in Figure 3, that uses Eric Ludlam's speedbar package.

A speedbar is a tall, thin Emacs window that provides a tree-structured file menu. In the case of the JDE, it's used to give you fast and easy access to your source files.

Simply press the F4 key and your cursor is warped to the speedbar. Using the arrow keys, you can quickly select another source file and then, by pressing Enter or the middle mouse button, display the new file in the main window.


Figure 3. The JDE Speedbar
The JDE Speedbar

Wizards

The JDE includes several wizards that assist in generating classic object-oriented structures, such as overriding a method in a subclass or delegation. There is also a wizard that can generate a set of function stubs that implement a specified interface.

Debugging

Let's not forget the most crucial part of any quality IDE: the debugger. The JDE has its own debugger, JDEbug. JDEbug sports tight integration with the JDE environment. Of course, you can also use jdb, the debugger packaged with your Java developer kit.



Back to top


Making the move to Emacs

Developing Java software in an Emacs environment doesn't have to mean running the command-line tools. As I've illustrated in this article, Emacs has a number of useful packages that convert this humble text editor into a comprehensive, efficient, and well-integrated development environment.

The combination of Emacs with the JDE provides similar seamless integration that you often find in commercial IDEs, and you receive a bonus prize -- you get access to the hands-on ability to customize and the experience of a virtual world of software developers, both compliments of open-source software.



Resources

  • Participate in the discussion forum.

  • Java Mode is available as part of cc-mode.el. Download it if it isn't already part of your Emacs installation.

  • The JDE home page contains information relating to all aspects of JDE, including documentation and the latest releases of the software.

  • The Collection of Emacs Development Environment Tools (CEDET) is a broad, unified project to help Emacs deal with many aspects of Emacs-based development.

  • CEDET has its own IDE called EDE. EDE is not Java-language specific.

  • Download the IBM developer kit for your operating system or platform.

  • You can also download Java developer kits from the following sources:
  • In a recent installment of Cultured Perl (developerWorks, March 2001), author Teodor Zlatanov customizes the configuration of his desktop in Linux, which is optimized for a Java and Perl programming setup.


About the author

Greg Travis is a freelance programmer living in New York City. His interest in computers can be traced back to that episode of "The Bionic Woman" where Jamie is trying to escape a building whose lights and doors are controlled by an evil artificial intelligence, which mocks her through loudspeakers. Greg is a firm believer that, when a computer program works, it's a complete coincidence. Contact Greg at mito@panix.com.




Rate this page


Please take a moment to complete this form to help us better serve you.



YesNoDon't know
 


 


12345
Not
useful
Extremely
useful
 


Back to top