Contents:

Introduction

Eclipse is an integrated development environment with strong support for Java. In addition to various features that facilitate writing Java code (such as autocompletion and code refactoring), it has built-in support for JUnit, and Ant — tools that we use in CSE 331.

Eclipse (version 4.5, Mars) is already installed on CSE departmental machines (and the department virtual machines). See here for how to use Eclipse at home.

See here for how to use version control with Eclipse.

You can read more about and get help on the features of Eclipse in its online documentation.

Eclipse Quick Reference

Here are some tips for things that can make your life easier in Eclipse:

Autocomplete Ctrl-Space asks Eclipse to help you complete some code you've started.  Eclipse can complete lots of things:
  • variables, method names, class names
    • ArrayL Ctrl-Space    -->  ArrayList
    • random.next  Ctrl-Space  -->  random.nextInt
  • constructor and method parameters
    • new ArrayList(    Ctrl-Space  --> popup menu of ArrayList's constructors
    • random.nextInt(   Ctrl-Space --> tooltip showing nextInt's parameters
  • methods to override
    • class Foo extends Bar {    Ctrl-Space --> menu of Bar's methods that can be overridden
Organize import statements Ctrl-Shift-O (that's O as in Organize) automatically updates the import statements at the top of your class, adding classes that need to be imported from other packages and removing classes that you're no longer using.  If a class name is ambiguous — e.g., List might be either java.util.List and java.awt.List — then Eclipse pops up a dialog asking you which one you want.
Look up Java API documentation Shift-F2 when your cursor is on a class or method name (You have to configure this feature with the location of the API documentation).
Comment/uncomment a block of code Ctrl-/ comments the highlighted region. Ctrl-\ uncomments the highlighted region.
Renaming or moving packages, classes, methods, and variables (updates all references)   Alt-Shift-R (Rename) or Alt-Shift-V (Move) when your cursor is over the package/class/method/variable. Equivalently, right-click on any package, class, method or variable in the Package Explorer and select Refactor » Rename to rename it or Refactor » Move to put it in another package or class.
Delete current row Ctrl-D when cursor is in row to be deleted.
Mark TODO items for yourself Start a comment with TODO to leave yourself a note about a piece of code that you need to fix. Eclipse will automatically put the comment in the Tasks pane, the pane where it shows your compile errors.  (If you don't see the Tasks pane, use Window » Show View » Tasks.) You can jump to TODO items or compile errors in your code quickly by double-clicking on them in the Tasks pane.
Generate get() and set() methods Make sure the fields for which you would like to create get() and set() methods are declared in the class, then right-click and use Source » Generate Getter and Setter.
Run classes or unit tests that you've run recently The little "play" icon on the Eclipse toolbar runs the last class or unit test you just ran. Pull down its menu for your recent history of runs.
Turn off console autoraise When a program run under Eclipse writes to standard out (say, via println), Eclipse raises the "console" perspective. This may be annoying when you run tests, or when you use Continuous Testing. To turn this off, go to Window » Preferences » Run/Debug » Console and uncheck “Show when program writes to standard out”.
Emacs key bindings If you prefer to use Emacs key bindings while editing code, do:
Window » Preferences...» General » Keys » Modify and set Scheme to Emacs

Use the right version of the JDK

If you get an error such as

java.lang.UnsupportedClassVersionError: utils/tests/LabelledParameterized : Unsupported major.minor version 51.0
     at java.lang.ClassLoader.defineClass1(Native Method)

while compiling or while running tests, then you are using the wrong version of Java. CSE331 uses Java 8. A common reason for using the wrong version of Java is making a mistake when following our setup instructions, or making an incorrect change to your Eclipse setup since doing so.

The following might fix the problem:

Right click your Project » Properties » Java Build Path » Libraries Tab » Remove the JRE and JUnit Libraries (keep junit-4.11.jar though) » Click "Add Library" » JRE System Library » Workspace default JRE (jdk1.8.0) » Finish

With luck, as of 15au all of the labs and all student machines should be running Java 8 and this will not be a problem.