Eclipse basics for Java development

Just a basic into to Eclipse, aimed at people who are new to Java. Covers creating a new project, debugging, common shortcuts/navigation, git.

Workspace

Workspace contains your settings, ex your keyboard shortcut preferences, list of your open projects. You can have multiple workspaces.

Selection_097.png

You can switch between Workspaces via File ->Switch Workspaces

Projects

A project is essentially an application, or a library to an application. Projects can be opened or closed. Content of closed projects don’t appear in searches.

Hello world Project

To run some basic java code:

  • File -> New -> Java project
  • Give the project some name ->  finish.
    Selection_085.png
  • Right click on src -> New -> Class
    Selection_086.png
  • Give your Class some name, check “Public static void main(String [] args)”
    Selection_087.png
  • Add “Hello World” print line:
    System.out.println(“Hello world”);
    Selection_088.png
  • Right click on “SomeName.java” -> run as -> Java Application
    Selection_089.png
  • Output printed in Console:
    Selection_090.png
  • Next time you can run the file via run button:Selection_092.png
  • Or via “Ctrl+F11”

Debugging

Set a breakpoint by double clicking on the line numbers in the margin, then click on the bug icon or right click and “Debug as” -> “Java appliaction”
Selection_098.png

For more info on debugging, head over to Vogella:
http://www.vogella.com/tutorials/EclipseDebugging/article.html

Switching perspectives

Eclipse has the notion of Perspectives. One is for Java development, one for debugging, (others could be C++ development, or task planning etc..). It’s basically a customisation of features and layout.

When you finish debugging, you can switch back to the java perspective:
Selection_099.png

Common keyboard shortcuts

  • Ctrl+/    – comment code “//”
    Selection_093.png
  • Ctrl+shift+/    – comment code ‘/* … */’
    Selection_094.png
  • Ctrl+F11   – Run last run configuration
  • Ctrl+Shift+L  Keyboard reminder cue sheet. (Type to search)
    Selection_095.png
  • Ctrl+Shift+L, then Ctrl+Shift+L again, open keyboard preferences.
  • Ctrl+O – Java quick method Outline:
    Selection_096.png
    Note: Regex and case search works. Ex “*Key” will find “getBackgroundColorKey()”, so will  “gBFCK”.
  • Ctrl+Shift+r – search for resource (navigate between your classes).
  • Ctrl+Shift+f – automatically format the selected code. (Or all code if no block selected).

For more on shortcuts, head over to Vogella:
http://www.vogella.com/tutorials/EclipseShortcuts/article.html

Source code navigation

Right click on a method/variable to bring up a context menu, from there select:

Open Declaration (F3)

This is one of the most used functions. It’s a universal “jump to where the method/variable/class/constant is defined”.

 

Open Call hierarchy

See where variable or method is called.

Tip: For variables, you can narrow down the Field Access so that it only shows where a field is read/written.

Selection_105.png

Quick outline (Ctrl+O)

The quick outline is a quick way to find a function in your class. It has regex support and case search. E.g “*size” will find any method with ‘size’ in it and “cSI” will find ‘computeSizeInPixels’.
Tip: Press Ctrl+O again and you will be shown methods that get inherited from parent classes.

Selection_102.png

Navigate to super/implementation (Ctrl+click)

Sometimes you may want to see which sub-classes overrides a method. You can hover over a method and press ctrl+click, then on “Open Implementation”.

Selection_106.png

You will be presented with a list of sub-implementations.

Selection_108.png

You can similarly navigate to parent classes.

Code completion

Predict variable names, method names,

Start typing something, press: “Ctrl+space”

Selection_101.png

It can complete by case also, ex if you type “mOF” and press Ctrl+Space, it will expand to “myOtherFunction()”.

Templates

Typing “System.out.println();” is tedious. Instead you can type “syso” and then press ‘ctrl+space’. Eclipse can fill in the template code.Selection_084.png
You can find more on templates in Eclipse Preferences.

Git integration

99% of my git workflow happens inside Eclipse.

You will want to open three useful views:

Window -> Show view -> others

  • Team -> History
  • git -> Git Repositories
  • git -> Git Staging

You can manage git repositories in the “Git Repositories” view:

Selection_109.png

You can add changed files in the “Git Staging View” via drag and drop, and fill in the commit message. You can view your changes by double clicking on the files:

Selection_110.png

In the “History” view, you can create new branches, cherry pick commits, checkout older versions. Compare current files to previous versions etc..

Selection_112.png

Selection_113.png

More on Eclipse

If you want to know more about the Eclipse interface, feel free to head over to Vogella’s in-depth Eclipse tutorial:
http://www.vogella.com/tutorials/Eclipse/article.html

Also free free to leave comments with questions.

4 thoughts on “Eclipse basics for Java development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s