Monday, February 27, 2012

Really useful Eclipse shortcuts

I tended to remain skeptical about the advantages of using shortcuts in order to "gain more time". After all, how much time would you really save learning to use them? Well, I gave them a try. Even tough I think I didn't gain more time, I found myself using shortcuts instead of making abuse of "copy & paste" and learning from Eclipse suggestions while developing. So, I am putting here the shortcuts that I mostly use:

CTRL + 1
With this one, the scenarios where it is really useful are:
  • You have a variable in a class, with this shortcut you can refactor rename it.
  • You can switch between a local variable declaration and attribute of the class.
  • Or if you are creating the local variable and assigning a value to it in the same line, you can split it in two lines.
  • You have a call to a function that returns a value, with this you can automatically create a local variable or class attribute and assign the return value to it.
There are other suggestions possible that Eclipse will offer you, like rewriting code in other ways. You can learn a lot from those suggestions too! I really recommend the use of this shortcut.

CTRL + .
With this you can navigate through warnings or errors inside your class.

CTRL + F and CTRL + K
You open the find box with the first one, and look for other occurrences in the same file.

CTRL + SHIFT + O
To organize imports in your class, these are automatically deleted, added or a suggestion box may appear in case there are two classes with same name under different package in the project. This one is a must.

CTRL + SHIFT + R
To open a resource without losing time searching for it in the Package Explorer. This one is a must.

CTRL + O
It will ease the navigation though attributes and methods inside a class.

CTRL + 3
You can choose different commands Eclipse allows you to use, but instead of doing that through a graphical menu choosing your option, you can write the word and the command will show. E. G.: you want to create a new project then: press the shortcut, write "new project" and choose it, in the same way write "servers" to open the "servers" view.

CTRL + SHIFT + LEFT or RIGHT
This is like SHIFT + LEFT or RIGHT to highlight words, but the difference is that with this one you highlight complete words, not one at a time. This one in particular is not from Eclipse, but very helpful in everyday development :)

Hide the "target" folder in Eclipse IDE

If you are using a development IDE like Eclipse and using Maven to manage your project, then you sure have faced the situation where you edit a class which is inside the target folder instead of the real one.

To avoid having this kind of issues again you have to:

Right-click in target folder ---> Properties ---> Resource ---> Check the Derived option


This option will prevent you from opening a resource (Ctrl + Shift + R) that is inside that folder, but you will still see that folder in the Package Explorer. To hide it you should:



Accept and now every target folder in your Package Explorer won't be shown, but they will still exist in your filesystem.

Wednesday, February 8, 2012

Running JBoss as a daemon

The advantage of running a JBoss instance as a service (or daemon) is that it will run in the background, taking advantage of the fact that the JBoss server is a non-interactive process. This way of running processes is particularly useful whenever you need to access a server through a command-line interface, deploy an application and run the server.

This procedure was done under Fedora 16 x64 and JBoss 5.1 community version.

1) Copy the file jboss_init_redhat.sh which is under $JBOSS_HOME/bin into /etc/init.d
2) You should rename it so that it represents the service you are referring to, for example jboss-dev
3) Now you should at least modify the following variables in the jboss-dev file:
#define where jboss is - this is the directory containing directories log, bin, conf etc  
JBOSS_HOME=${JBOSS_HOME:-"/opt/jboss"}

#define the user under which jboss will run, or use 'RUNASIS' to run as the current user
JBOSS_USER=${JBOSS_USER:-"RUNASIS"}

#make sure java is in your path
JAVAPTH=${JAVAPTH:-"/usr/local/jdk/bin"}

#configuration to use, usually one of 'minimal', 'default', 'all'
JBOSS_CONF=${JBOSS_CONF:-"test-standard"}

4) Now you can run these commands to start, restart or stop the servers:
service jboss-dev start
service jboss-dev restart
service jboss-dev stop

Even when stopping JBoss through a service it may fail due to external events and we may not notice it. So, it is always recommended to check the logs...
tail -f /opt/jboss/server/test-standard/log/server.log
...as well as running services:
ps aux | grep java
If you see the process is still there then you should take the pid of that process (say 999), and kill the process with either one of these two commands: (keep in mind that -15 is a clean shutdown and -9 should be used in case the process still does not respond)
kill -15 999
kill -9 999