Showing posts with label How to use Eclipse. Show all posts
Showing posts with label How to use Eclipse. Show all posts

Subclipse and Eclipse 3.5/Galileo

With the Eclipse 3.5 final release now available, I thought it would be good to get a post up for Subversion users that are looking to install this release. Subclipse works great in Eclipse 3.5 and is easy to install. There are two versions of Subclipse available with support for Eclipse 3.5.

Subclipse 1.4.x is based on Subversion 1.5 client API
Subclipse 1.6.x is based on Subversion 1.6 client API

Install the version of Subclipse based on the version of Subversion you want to use. This is mainly an issue if you want to use multiple clients with the same Subversion working copy. If you do all of your work from Eclipse, then just grab the latest version. All Subversion 1.x clients can work with all Subversion 1.x servers. So, if possible, just use the latest version.

OSX and Linux users need to install the right version of the JavaHL library (1.5 or 1.6). Most Linux distros are still providing 1.5.x, but the RPM's from CollabNet include JavaHL and install on every Linux distro that I have tried (including Ubuntu). CollabNet also provides binaries and JavaHL for OSX.

I maintain a wiki on the Subclipse site with detailed information about getting JavaHL working on your system.

In other news, Subclipse 1.6.x now includes the CollabNet Merge client. This was developed as part of the merge tracking feature in Subversion 1.5 and makes merging from Eclipse very easy to do and manage. The CollabNet Merge client is part of the CollabNet Desktop - Eclipse Edition, which includes Mylyn and connectors for CollabNet's trackers. The merge client is now also available directly for Subclipse users with no other dependencies. Users that want the full merge client, which adds the change set merge option, can install the CollabNet Desktop.




Click here to View more...

Use Eclipse effectively - tips and trics

In Germany they have an entire magazine devoted to Eclipse, called (imaginatively ;) "Eclipse Magazin"". I don't speak (or read) German, but I have to say the content always looks really good, and they've had some excellent coverage of AspectJ and AJDT in past issues. The magazine has a guest column, "MyEclipse" in which various users of Eclipse describe their favourite hints and tips for working with the IDE.

It was my turn to write the column this month.

I have to say, I enjoyed writing the piece more than I thought I would - amongst other things it made we really think about how I actually do use Eclipse, and whether I'm using it as effectively as possible. The original column can be found here. With permission, I'm making an English translation (strictly, the English original!) available here for those that don't read German. If you have any favourite Eclipse tips that I don't know about, please do let me know!

My Eclipse

I’ve seen a lot of different people using Eclipse over the last few years. Nearly every one of them was using Eclipse pretty much in its default configuration. My Eclipse looks quite different to that, and in this article I’ll walk you through a collection of hints and tips for getting the most out of the IDE for Java development.

I’ll show you how to get organized when working with multiple workspaces, multiple projects, and lots of jar files. I’ll show you how to configure the IDE to help you write better code, and how to organize the Java perspective for maximum efficiency. As you might expect, I’ll also give you a couple of hints and tips for working with AspectJ!





Getting Organized

Using multiple workspaces

I tend to have multiple workspaces on the go at any one time (about 8 at present having just done a quick check). I use different workspaces for each major project (or version of a project) that I’m working with. For example, right now I’ve got workspaces for AspectJ 1.5.1 development, the AspectJ 1.5.0 release branch, Spring development, a couple of demonstration workspaces I use when giving talks, a workspace I use when giving training courses, and so on. It’s not uncommon for me to have 2 or 3 eclipses open at the same time.

The first practical question becomes knowing which eclipse is which when looking at the task bar or selecting between open windows using Alt-Tab. I always launch eclipse from the command-line using a script. Passing the “-showlocation” flag adds the workspace location to the windows title bar so that you can easily see which window is for which workspace. I normally start Eclipse with a little extra memory too (I find it works a little snappier that way). A good default for launching Eclipse if you’re working with sizeable projects would be something like this:

eclipse.exe –showlocation –vmargs –Xmx512m > /dev/null 2>&1

(I use cygwin, and I find that Eclipse tends to dump nonsense to the console that I don’t want to see, hence the redirect).

Another problem with using multiple workspaces is keeping your preferences and settings up to date across all of them. There is no need to configure each workspace individually every time you create a new one! Get one workspace set up exactly how you like it, and then select File -> Export -> Preferences. This will create a file that contains all of your preferences settings that you can keep in (e.g.) your home directory. Whenever you create a fresh workspace, use File -> Import -> Preferences and you’re good to go.

(By the way, if you're adding plugins to the base eclipse set, see Colin Sampaleanu's excellent blog entry on how best to manage that).

Coping with lots of projects inside a workspace:

It’s also not uncommon for me to have a lot of projects inside a workspace (one project per module). This is where working sets can make a big difference. Take a look at how the package explorer shows my core spring training workspace by default:

It’s just a jumbled mess of projects. Working sets to the rescue! From the drop down menu at the top right-hand corner of the Package Explorer view click on “Select Working Sets…”. From here you can define logical groupings of projects. With your groupings defined, select “Show -> Working Sets” from the drop-down menu. Now the Package Explorer will look something like this:

Much better!

Sorting out the clutter inside a project:

When looking at an individual project inside the package explorer, all of the jar files on the project build path show up by default. Most of the time I find these just get in the way. Here’s how my spring development workspace looks with the jar files showing:

The list of jars goes on even further, and worse, there are additional folders that I do care about at the end of the list. Select “Filters…” from the Package Explorer drop-down menu and define a new name filter matching “*.jar”. This will hide all of these jars from view making it much easier to find what you really want.

Organizing jar files:

On the subject of jar files, if you have common groups of jar files it can be very effective to define them as “User Libraries” that can be named and added to a build path as a unit. You’ll find the settings page to define user libraries under “Windows -> Preferences…” and then select Java -> Build Path -> User Libraries.

Sharing projects with others:

To make projects easy to share with others (for example, checking into a shared CVS repository) make the maximum use of variables. Right next to the option to define User Libraries, you’ll find the option to define Classpath Variables. When adding an external library to a build path of a shared project, always define a classpath variable that points to the containing directory on your local machine (or just the jar file itself). Then you “extend” the variable when defining a build path entry. This way each user can define the variable appropriately for their own local environment, but the project settings can still be shared in the repository.

Using Eclipse for Java Development

Helping Eclipse to help you write better code:

I used to use a small editor font in order to see the maximum amount of code possible inside the Java editor window. A while ago I changed tactics. Now I use a larger font than normal – 14 pt bold. As a consequence, I can see a lot less code in the editor at a time. This helps me write better code.

Let me explain.

It’s natural to want to see a whole method in one go. Seeing less code on the screen at a time has the subtle effect of making me write shorter, clearer methods. It’s also easier to sit back and reflect on code, and easier to pair program and work with others. Give it a try and see if it works for you too.

Organising the Java Perspective:

It used to be that I was never satisfied with the width I’d set for the Package Explorer and JUnit views. Too narrow and I couldn’t see the package and type names properly, or the stack traces in a test failure. Too wide and the editor window would get crushed between the Package Explorer on the left, and the Outline View on the right. Especially if you follow my suggestion of using a bigger font this can be a real pain.

I found the answer in a combination of fast views and in-place views. Now my eclipse dedicates as much real estate as possible to the source code. I have no views on the right-hand side, and no views on the left-hand side. The views I typically work with are the JUnit View, Type Hierarchy View, Package Explorer, and Javadoc View. Open these views, right-click in the title bar, and select “Fast View”. By default fast views dock at the bottom of the screen, but it’s not as convenient to move a mouse up and down to open them as it is to left-to-right. Select the fast view bar and choose “Dock on… left”. You have now reclaimed all of the space on the left-hand side that these views used to take, and your perspective should look something like this:

Another hidden benefit of this mode is that you can make the views wider than they were before, so that you can see the content properly (or even better for e.g. JUnit and Search, set the orientation to "horizontal" from the context menu). You can also set the widths of each view individually (JUnit wider than the Package Explorer for example, which is a significant improvement). If you don’t like having to move the mouse across to open a view, I’ll discuss a solution to that in a moment.

Any views you may have on the right-hand side, just close them (for most people, this means the Outline View). Instead use the in-place views. Ctrl-O opens an in-place Outline View (over the top of the editor). It’s actually better than the more usual docked view because it supports type ahead to quickly find the member you are looking for. Along with Ctrl-O, you also need to learn Ctrl-T which opens an in-place “quick” type hierarchy. This is a really easy way to see all implementers of an interface for example.

A final tip on the subject of view arrangement. If you have a dual monitor setup (becoming very common in the client sites I visit) it is quite nice to undock the Javadoc view completely – just grab it and drag it right outside of your Eclipse window and onto your second monitor screen. Now you’ll have the javadoc permanently open as a reference tool while you work. (Yes, you can undock views in Eclipse now, a lot of people seem not to know that).

On the subject of key bindings, you can significantly improve your productivity in Eclipse by learning some of the basic keyboard short cuts for common tasks (and indeed, for some things in the Java editor that aren’t available from the menus at all). However it’s also true that some of the default bindings for common tasks are absurd: “Alt-Shift-X,T” to run a test case for example – one of the tasks you’ll be doing very frequently I trust. When using fast views, I also find it convenient to add key bindings to open the Package Explorer and JUnit views quickly without having to move the mouse at all.

I make the following changes to the default key bindings:

Key combinationResult
Ctrl-P Open the Package Explorer (normally bound to print, but I hardly ever print from Eclipse, and will happily use the menu for that)
Ctrl-R Run as a test case. (normally bound to run-to-line in the debugger). Running tests should be a simple key-stroke away at any time.
Ctrl-J Open the JUnit View (normally bound to incremental search)
Ctrl-I Incremental search
Ctrl-G Generate getter/setter
Ctrl-B Toggle breakpoint

Now that you have Ctrl-R to run tests, it’s worth knowing that you can use this shortcut from within the editor when editing a test case, when a package is selected (to run all the tests in the package), when a source folder is selected (to run all the tests under the source folder), and when a project is selected (to run all the tests in the project).

Here are some of the other key shortcuts that I find amongst the most useful:

You’re already familiar with using the arrow keys (left,right,up,down) to move around within the editor. Using the arrow keys in combination with Alt, Shift, and Ctrl put a whole new set of capabilities at your fingertips:

Key combinationResult
Arrow keys Move cursor point in editor
Shift + arrow keys Increase/decrease selection in editor
Alt+Shift + left,right Select previous syntactic element / next syntactic element
Alt+Shift + up, down Select enclosing element / restore last selection
Ctrl+Shift+left,right Select next word / previous word
Ctrl + left,right Move left / right by one word
Ctrl + up,down Scroll up / down by one line
Ctrl+Shift+up,down Go to previous member / go to next member
Alt + left, right Go forward and backward in history (really useful if you’ve been following links exploring the code)
Alt+up,down Hidden treasure! Move the current line or selected lines up and down
Ctrl+Alt+up,down Duplicate line above / below

Other useful keys (standard cut/copy/paste etc. not included as I assume you are already familiar with these):

Key combinationResult
Ctrl+O In-place outline view
Ctrl+T In-place quick type hierarchy
Ctrl+M Maximize/restore current window/view
Ctrl+D Delete line
Ctrl+Shift+delete Delete to end of line
Shift+Enter Insert new line below
Alt+Shift+R Rename
Alt+Shift+T Quick refactoring menu
F3 Open declaration
F4 Open in type hierarchy
F11 Debug last launched
Ctrl+F11 Run last launched
Ctrl+Shift+T Open type
Ctrl+Shift+R Open resource
Ctrl+Shift+G Search for references in workspace
Ctrl+F6 In-place editor selection
Ctrl+F7 In-place view selection
Ctrl+F8 In-place perspective selection
F12 Activate editor (useful if you’re following my suggestion of using fast views :- F21 will dismiss the fast view and return you to the editor)

AspectJ hints and tips

Eclipse has great support for aspect-oriented programming with AspectJ through the AJDT plugins. In this section I’ll show you how to set up projects using linked source folders, how to run JUnit tests with an aspect library using load-time weaving, and some things you can do with the Visualizer that you may not realise. See the AJDT site on Eclipse.org for a tutorial on using AJDT – this section just contains a few less-well-known pointers.

Before we start, if you’re trying out using fast views and closing all the views on the right-hand side, one of things you’ll be missing is the cross-references view from AJDT (with all of the advises and advised-by relationship information). You can either dock this at the bottom (alongside the console for example), or use the in-place quick cross references view (not a lot of people know about that!). It has a not-very-useful default key binding of Alt-Shift+P, I remap it to Ctrl+U (not many letters left to choose from!). Now just press Ctrl+U anywhere in the editor and get a pop-up view of the cross-references for the selected element. Press it again to get cross-references for the whole file. Perfect!

Linked source folders:

Linked source folders can be a great way to experiment with aspects on a project without touching the main (Java) project at all. Create an AspectJ project alongside the Java project that you want to work with. Put all of your aspects into the “src” folder of the AspectJ project. Now, inside the AspectJ project open the project properties (Alt+Enter, since we’re trying to wean you off the mouse!) and go to the Java Builder page, and then the “Source” tab. Click on “add folder” to create a new source folder, and then the “Create new folder…” button. Here you’ll see an “Advanced” button. If you press this, you’ll get the option to create a folder that is actually a symbolic link to another location. Use this technique to create source folders inside the AspectJ project that are symbolic links to the source folders inside the Java project you want to work with.

Now whenever you build the AspectJ project, you will get (in the bin directory of the AspectJ project) a woven version of the project files. You will also see any warnings or errors arising from your use of declare warning / declare error in this project. The “donor” Java project remains completely unaffected by this exercise. Any changes to the source files in the Java project are of course instantly visible in the AspectJ project (and vice versa). To get the best performance, you can now turn off incremental building for the AspectJ project (Project menu -> build automatically) and just build that project on demand using the AspectJ build button in the taskbar.

Load-time weaving

(See my entry on using an aspect libraryfor a more in-depth treatment of this)

If you’re using an aspect library (such as spring-aspects.jar that ships with Spring 2.0) it is very convenient during development to use “load-time weaving”. Load-time weaving is the name given to weaving your application classes at runtime as they are loaded into the virtual machine. In this way there is no need to convert your project to an AspectJ project, or to do anything special at all other than put the aspect library on your build path.

Say you have some integration tests that you kick off via JUnit, and that rely on the behaviour contributed by the aspects in order to pass. For example, you’re using @Configurable to dependency inject domain objects. With the needed library on the build path, create a launch profile for the test run. The easiest way to create a launch profile is just to highlight the test or set of tests that you want to run and run them with Ctrl+R (if you’ve followed my advice and remapped that key binding). This run will fail, because you haven’t activated load-time weaving yet. Now select “run…” from the launch menu and you’ll see the launch profile that was created for the tests you just ran. Click on the arguments tab, and in the “VM arguments” box add the following:

-javaagent:aspectj-install-dir/lib/aspectjweaver.jar

Where aspectj-install-dir is the directory where you installed AspectJ (it’s best to download the standalone compiler and associated tools from www.eclipse.org/aspectj when using load-time weaving, ant building etc..).

Now you can rerun the tests and you’ll find that AspectJ picks up and uses the aspect libraries that you have on your classpath. It really is as simple as that.

Visualizer:

One of the tools that you get when you install AspectJ is the Visualizer. In fact, you get a whole “Aspect Visualisation” perspective which is the best way to use the visualiser. By default, the visualiser shows you cross-cutting information such as advises and advised by relationships. The visualizer plugin actually provides a generic capability that is not tied to AspectJ though. Open the visualizer perspective, and from the drop-down menu of the main visualizer view select “preferences”. Here you can choose the data provider that the visualizer will use to display information. By default it uses the AspectJ provider as I said. If you change to the “JDT Search Results Provider” you can use the view to visualize the results of the current search (all the matches, and where they are in the project).

The following image shows the results of searching for references to “InitializingBean” in Spring. Just as with the advice visualization, you can navigate from the visualization to the source code locations simply by clicking on the bars.

If you select “Resource and Markers Provider” you can see where all of your breakpoints, Task markers, warnings and errors are. Clicking on the options in the visualizer menu enables you to choose the subset of these that you are interested in at the time:

And finally

If you haven’t done so already, you should check out the Mylar plugin for Eclipse (developed by one of the AspectJ team, Mik Kersten) (see www.eclipse.org/mylar, and then go to the getting started page and watch the flash videos there). Especially if you’re working with bugzilla, Mylar is fantastic. Mylar deserves an article all of its own so I won’t attempt to describe it here – just go and try it out for yourself, you’ll be glad you did!




Click here to View more...

Eclipse: Shortcut keys

You should try to keep your hands on keyboard. The less you touch the mouse, the more code you can write. I am trying to keep the mouse laying still and control the IDE completely using keyboard. What do you think is faster: pressing ALT + C or right clicking the project, selecting Team -> Commit?

It is said, that if a function does not have a key binding, it is useless. Below you will find a set of essential keyboard shortcuts that I love. These shortcuts are set up by default, they should all work.

CTRL + D

Delete row. Try it! You no more need to grab the mouse and select the line, no more Home, Shift + End, Delete. Quick and clean.

ALT + Up/Down Arrow

Move the row (or the entire selection) up or down. Very useful when rearranging code. You can even select more rows and move them all. Notice, that it will be always correctly indented.

ALT + Left/Right Arrow

Move to the last location you edited. Imagine you just created a class Foo, and now you are working on a class Boo. Now, if you need to look at the Foo class, just press Alt+Left Arrow. Alt+Right Arrow brings you back to Boo.

CTRL+SHIFT+O

Organize imports. What happens when you first use a class you have not yet imported? You will see an error. But when you press this magical combination, all your missing classes will be imported, and the unused imports will vanish.

CTRL+1

Probably the most useful one. It activates the quick fix. Imagine you create a class, which implements some interface. You will get an error, because the inherited methods are not yet implemented. While you are on line where the error occurs, press this combination to activate the quick fix. Now, select the "Add unimplemented methods" option. You can use the quick fix at every error you ever receive.

Quick fix comes handy in other situations too. My favorite is the "Split variable declaration". Sometimes I need to broaden the scope of a variable. I activate the quick fix, split declaration, and use alt + arrow to put it where it belongs. You can find even more usages: Convert local variable to field, rename in file, Inline local variable..

You could use the "Split variable declaration" on the bar variable, and then move it with Alt+Arrows above the try block..

Or you could use the "Add unimplemented methods" fix here.

The best thing you can do if you see an error is to use the quick fix.

CTRL+SHIFT+T

Open Type. Imagine, that you need to have a look at the Foo class. But, where is the Foo class? Is it in the Boo project and in the foo.bar package? Or somewhere else? With this shortcut, you don't need to know. Just press it, type Foo and you are in.

CTRL+E

Shows you a list of all open editors.



CTRL+F6

Use to move between open editors. This is an slower alternative to Ctrl + E. Comes handy in a situation when you want to periodically switch between two editors, something, what is nearly impossible with Ctrl+E as it sorts entries quite randomly. Or you might just use Alt+Arrows..

CTRL+F7

Move between views. When in editor, press Ctrl+F7 to switch to the Package Explorer, or hold Ctrl and press F7 multiple times to switch to other views.

CTRL+F8

Move between perspectives. The same as previous.


CTRL + F11


Runs the application. What gets launched depends on your settings. It will either launch the last launched class (my preffered way) or it will launch currently selected resource (the default way). If you want to change its behavior read the previous post.

CTL + N


Open new type wizard. This is not very quick because you have to select the wizard type (weather you want to create new class, jsp, xml or something else) in the next step. Much faster way would be if you could just hit the shortcut and invoke the particular wizard. It is possible, just keep reading..

CTRL + M

Maximize or umaximize current tab.

CTRL + I


Corrects indentation.

CTRL + SHIFT + F

Formats code. You can make a beautiful looking code out of a mess with this. It requires a bit of setup, but it is well worth it. You can find its settings under Window->Preferences->Java->Code style->Formatter

CTRL + J


Incremental search. Similar to the search in firefox. It shows you results as you type. Don't be surprised, if you hit this combination, nothing happens - at the first glance. Just start typing and eclipse will move your cursor to the first ocurence.

CTRL + SHIFT + L

Shows you a list of your currently defined shortcut keys.

I don't like your shortcuts

Such is life nowadays. Remember, you can always change those bindings to match your preferences. Open Windows->Preferences->General->Keys. Now you can use the filter to find your shortcut and change its binding.
The real fun begins when you cannot find the command you are looking for. The key here, is to have the "Include unbounds commands" checkbox checked. It will show you all commands, even those, which have no keys bound.

While you are here, I recommend to add the following bindings:

CTRL+SHIFT+G

Bind this to "Generate getters and setters". This is a "must have".

ALT+C

Bind this to SVN/CVS "Commit".

ALT+U


Bind this to SVN/CVS "Update".

Now, type "new" (without quotes) in the filter text. You should see a list of all new type wizards. Choose the most frequently used and assign them a shortcut. For example, the most used wizard for me is the new class wizard. Thus I assigned it the CTRL+SHIFT+N keys.

Let me demonstrate a quick way to create new class now.

Hit CTRL + SHIFT + N (or the combination you assigned in the previous step). This should bring up new class wizard. Type in the name and press ALT+E. You can now select a class which will be a superclass for the newly created class. Hit ALT+A and select all implemented interfaces . Now hit ALT+F and your class will be generated. Eclipse will also provide the default implementation for all abstract and interface methods you inherited.

Did you notice the weird underscores everywhere in the dialog? They give you a hint about the shortcut key. Hit ALT and the underlined letter to press the button, check the checkbox or get focus for a textfield.

Did you notice the underscores?

I think that using shortcut keys is the fastest way to productivity and if not, then at least your wrists will say you a silent thanks. Now, don't wait, go on and assign keys to the features you use most.

One final tip from Andriy:

The problem is that there are so many keyboard shortcuts. I used to keep a printout with all the shortcuts I wanted to use. Finally I wrote an Eclipse plugin MouseFeed, which reminds the keyboard shortcuts for the actions called with mouse. You can even tell it to enforce some shortcuts - the action will run only if called with a keyboard shortcut.

So if you are struggling with yourself, if you want to use shortcuts, but always subconsciously touch the mouse, insta Click here to View more...

Eclipse Navigation Shortcuts

Man, I’m such an impatient guy. I cringe whenever I see somebody squint and frown, looking for a JSP file in Eclipse by browsing painfully through the gazillion JSPs in multiple folders in the Package Explorer. I squirm whenever I see somebody looking for a Java class by clicking through packages, one by one, backtracking if it’s the wrong package, and so on, until he sees the correct Java class.

I mean, any resource in the workspace is literally seconds away. Ditto to classes (and interfaces, and members, and so on). Why waste time and brain cycles to wade through countless lines in countless files? I thought that every Eclipse user knows this, in fact, if you’re reading this, most probably you already know this too. But thousands of Eclipse JDT users who never bother to read tech blogs in all probability will also never bother to find out what Eclipse can do for them. And it’s a pity, really, because they’re really missing out a lot. So maybe if you know one, you can forward this to them or something. Make them more productive or something, ya know. 30 seconds saved for every file can add up to really a lot!

So without further ado, let’s say you want to:

  • Open any file quickly without browsing for it in the Package Explorer: Ctrl + Shift + R. This shortcut opens a dialog box that accepts the name of the file you’re looking for. It even accepts wildcard characters, yo. Typing *-conversion.properties will give you the list of all files that ends with -conversion.properties. So everytime you want to open a file–stop that hand from going to the mouse, and press Ctrl + Shift + R instead!

Opening a resource in Eclipse

  • Open a type (e.g.: a class, an interface) without clicking through interminable list of packages: Ctrl + Shift + T. If what you want is a Java type, this shortcut will do the trick. Unlike the previous shortcut, this even works when you don’t have the Java source file in your workspace (e.g.: when you’re opening a type from the JDK).

Opening a type in Eclipse

  • Go directly to a member (method, variable) of a huge class file, especially when a lot of methods are named similarly: Ctrl + O. Say, you’re browsing through a file which has 500+ lines of code. How do you look for a method? Don’t use Ctrl + F and then type the method name. Use Ctrl + O, which gives you a list of candidates that match what you’ve typed so far. Select the member you want using the arrow keys, and press Enter. (Alternatively, if you just want to jump from one member to the next (or previous), you can use Ctrl + Shift + ↓ or Ctrl + Shift + ↑, respectively.) UPDATE: As Nick pointed out in the comments section, pressing Ctrl + O again shows the inherited members. Thanks Nick! :)

Browse Member

ctrl_o_2.jpg

  • Go to line number N in the source file: Ctrl + L, enter line number. Of course if the stack trace is in the Eclipse console, you can just click the hyperlink. But if it’s in a log file or something, just use this shortcut to go to the line in a jiffy.

Go to a line number

  • Go to the last edit location: Ctrl + Q for . If you have a big file, it’s annoying to jump from one location in line 1000+ to 2000+ only to realize after looking at line 2017 that you’ve made a mistake in that location near line 1000+ just now. This shortcut brings you right to where you last edited a file. Very handy in a big file. Gone are the days of “let’s see… where did I edit it again… nope, nope… ah there it is”. (This even works when you’re already looking at a different file.)
  • Go to a supertype/subtype: Ctrl + T. Before I found this, if I want to go to the superclass of a class, I’d go the the very top of the file, hover my mouse over its superclass, hold Ctrl, and click. Disgusting. Now I just press Ctrl + T and I get this dialog below, which toggles between supertypes and subtypes when you press Ctrl + T again.

Subtype hierarchy view

Supertype hierarchy view

  • Go to other open editors: Ctrl + E. I know you can cycle through the editors using Ctrl + F6 as well, but I prefer Ctrl + E because Ctrl + F6 has this annoying behaviour of requiring you to keep the Ctrl key down, and the distance between Ctrl and F6 is so far I have to twist my left hand to do that. Just press Ctrl + E, and either use the arrow buttons, or type the name of the file you’re editing.

Open editor

  • Move to one problem (i.e.: error, warning) to the next (or previous) in a file: Ctrl + . for next, and Ctrl + , for previous problem. No need to lift your hands off the keyboard to click on that red or yellow stripe.
  • Hop back and forth through the files you have visited: Alt + ← and Alt + →, respectively. I have to admit I don’t find myself using these two often, though.
  • Go to a type declaration: F3. Alternatively, you can hold Ctrl down and click the hyperlinked variable or class or whatever it is the declaration of which you want to see–but why lift your hand off the keyboard? Just press F3 and Eclipse will bring you to the declaration of whatever is at the cursor at that moment.

OK, that’s it for this post. There are tons of other Eclipse shortcuts not covered by this article. To see the whole list, just open up your Eclipse (I’m assuming Eclipse 3.2 here–in older or more recent versions this may differ slightly), go to Help → Help Contents → Java Development User Guide → Reference → Menus and Actions. The whole motherload is there, from generating comments, correcting indentations, surrounding with, and so on.

The point I’m trying to get across is: Eclipse has a LOT of shortcuts to make things real easy for you. Java (or heck, any software) development is hard. We shouldn’t make it harder on ourselves by fighting our tools! Let our tools help us as much as possible, so we all can go back on the dot and spend more time with our family, lovers, or whatever it is we want to spend more time on. There’s no honour in working hard inefficiently. Only disgrace.

Click here to View more...

Enable Debugging in Eclipse IDE

Programmer many times try to locate and fix the bugs (errors) in the program code. Basic meaning of debugging a program is to start with a problem, isolate the source of the problem, and then fix it. When a programmer says he has debugged the program it means bugs has been worked out and bugs no longer exist.

From a Java Programmer there are a varieties of tools but if you are using an editor then it becomes quite easy for you to debug the program.

Here are the steps to be followed to debug a Java program using Eclipse.

  1. The very first step in debugging is to load your application and open the “java” file that you wish to debug. Do not forget to switch to Debug perspective.
  2. Now a window will be appeared where you can find several options for debugging the project. It is better to describe about the options available here. Using Eclipse you can debug not even a Java application but also a Remote Java Application (by specifying the remote port number), Junit Test Case, SWT Application etc. After supplying the arguments. Click the “Debug” button. Debugger will connect to your java application under debugger perspective.
  3. Now you can enable or disable debug points in the application by just making a double click on the line where you want to stop execution of the program.
  4. Now you can enable or disable debug points in the application by just making a double click on the line where you want to stop execution of the program.
  5. Now use can use either keys F5-F8 or bar shown below debug window for debugging the application.
    Here is the use of each button for debug purpose.
    • Step Into: To go inside a Method.
    • Step Over: To bypass the next line of code.
    • Step Return : To come out of the current execution of method to original from where you stepped in.
    • Resume: to resume from the current breakpoint.
Click here to View more...

Using Eclipse

Using Eclipse


Introduction

Throughout the semster, you have the option of using Eclipse as your IDE. While learning Eclipse can be frustrating, it has some very nice features that make up for it in the end. This tutorial will go over creating a new project, using JUnit 3.8.1, setting up the Test-Tracker project, using Subclipse, and generating Javadocs with Eclipse.


Creating a Project

The following will walk you though creating a project from scratch:

  1. Select File->New->Project
  2. Select "Java Project"
  3. Enter the name of your project
  4. Make sure that the JDK compliance is at 5.0
  5. You can specify to have the class files in a sepearte folder in the "Project Layout" section
  6. Click Finished

Creating a Project from an Existing Source Tree

To use the test-tracker source folder, follow these steps

  1. Put the source tree in the project workspace
  2. Select File->New->Project
  3. Select "Java Project"
  4. Enter the name of your project: test-tracker
  5. Click Finished
  6. To make sure that the project is using JDK 5.0:
    1. Select Project->Properties
    2. Select Java Compiler
    3. Check "Enable Project Specific Settings"
    4. Set "Compiler Compliance Level" to 5.0

Adding a JAR File to Your Project Build Classpath

Sometimes you need to include an external JAR file as a library. Test-Tracker is already set up to use the JavaMail API because we have included the JAR files already. If you want to do this with other projects you are working on, follow these steps:

  1. Go to the project properties
  2. On the left column: click on "Java Build Path"
  3. Click on the "Libraries" tab
  4. Click on "Add JARs..."
  5. Select the Jar

Running Ant build.xml tasks

Running Ant tasks from the command line may not always be what you want. If you want to run them in Eclipse, you will need to follow these steps:

  1. Go to Run -> External Tools -> External Tools
  2. Right Click on Ant Build and select New
  3. Select the targets you wish to run in the Targets Tab
  4. If the build file needs to be specified or changed, do so in the Main tab
  5. If you need to add classpath jar files, do so in the Classpath tab
  6. You may need to add the tools.jar from ...\jdk1.5.0_0#\lib\tools.jar
  7. You may need to add the JUnit3.8.1.jar file to the classpath to run test or coverage
  8. Select build.xml -> Run As -> External Tools -> [the name of your ant task command]

Adding jar files to the Ant general classpath can be done by selecting: Window > Preferences > Ant > Runtime


Setting Up JUnit

JUnit should already be set up for test-tracker. We are currently using JUnit 3.8.1 as a class. To use JUnit on other projects, follow these steps in Eclipse:

  1. Right click on your test-src folder
  2. Select new->JUnit Test Case
  3. If a popup asks you if you want to add junit.jar to the build path, select yes
  4. Remember, Each test case you add should end with "Test". (i.e. SomethingTest.java)

Generating Javadoc Files

For this class, we already have the Ant target (ant doc) set up so that you should not have to do anything extra to generate your documentation. If, in other projects, you wish to use Javadoc with Eclipse, follow these steps:

  1. Select Projects -> Generate Javadoc...
  2. Click on configure
    • The javadoc.exe is located in the Java\bin folder on your computer
    • For example: C:\Program Files\Java\jdk1.5.0_02\bin\javadoc.exe is where I found mine.
    • The linux labs use: /usr/java/jdk1.5.0_08/bin/javadoc
  3. Select the source folder of the project you wish to document
  4. Enter the destination for the documentation files
  5. Click on next twice.
  6. In the large text area (extra javadoc options), enter the extra tag information:
    • -tag invariant:t:"Class Invariants:"
    • -tag pre:cm:"Pre-Conditions:"
    • -tag post:cm:"Post-Conditions:"
  7. Click finish.

Setting Up Subclipse (Subversion on Eclipse)

Follow the steps here: subclipse.tigris.org/install.html for directions on how to install the Subversion plugin for Eclipse

After setting up Subclipse, follow these steps:

  1. Select "Window -> Open Perspective -> Other"
  2. Select "SVN Repository Exploring"
  3. Select "Window -> Preferences"
  4. Select "Team -> SVN"
  5. Change the "SVN Interface" radio-button to "JavaSVN (Pure Java)"

While in the SVN Repository perspective, you can add a repository. The URL you use will follow this schema:

  • svn+ssh://cs.byu.edu/users/groups//test-tracker

Using Subversion

If you want to use Eclipse in syncronizing your Subversion repository:

  1. Create your repository and check it out into your workspace.
  2. Open eclipse and follow the steps to create a project from the existing source tree
  3. On the Package Explorer, notice the icon for your project has a maroon astrix on your project folder
    • This icon shows that the folder has out of date content
  4. Adding a new package will show a question mark on the new package icon.
    • This icon shows that the folder is unrevisioned content
  5. Lets add a few classes and look at the different options that we can perform with right-click
  6. Notice when we "Add to Version Control", a blue plus icon is shown on each item added.
  7. When we want to commit, a dialogue will ask to verify each operation.
    • Only checkmark items you want under version control. Eclipse will make some files you may not want in your repository.
    • Having extra files under version control can create a large overhead and wasted space in your account. You may have Eclipse/Subversion ignore items by selecting "Team->Add to svn:ignore"
    • You must input a log message to explain the changes. This log can be viewed by selecting "Team->View in Resorce History".
  8. When we commit, the items will now have yellow tube icons for each item that is up to date
    • Each item has a name, revision number, revision date, and revision author.
  9. When you edit a file, it will have the maroon astrix afixed to it to show that it is now out of date. If you wish to revert the file to the state it was when you last updated, you may select "Team->Revert".

  10. There are many other Subversion commands that can be used from Eclipse. Use your sandbox repository and experiment with your team mates to learn more.
Click here to View more...