This is a collection of Eclipse Tips and Tricks which I’ve learnt over time. You will be surprised how you can boost your productivity by learning just a few simple tecqniques!
How to choose the format of deployed applications
In order to run this trick, you need to install JBossAS tool from JBoss tools distribution. Once you have installed this component, select the JBoss AS perspective.
In the lower part of the screen, double click on the server. The jBoss runtime window will open with two tabs.
Select the “Deployment” tab and choose if you want to deploy the application as compressed archive or not.
How to deploy an application to a remote JBoss server.
You need to to install JBossAS tool from JBoss tools distribution in order to run this tip.
1. From the File menu select New > Server and choose a JBoss server from the “JBoss Community” list.
2. In the next window will let you choose the local installation of JBoss server. This will be anyway needed for setting up the classpath of your project.
3. In the next window you will specify the location of the JBoss server. Choose as server behaviour “Remote system deployment” and select the Host using the “New Host” button.
4. Once connected to the host, choose the “Browse” button to browse the remote file system and pick up the JBoss home. If everything was correctly configured the “Test” button will verify the connection to the server.
How to debug JBoss remotely
Remote debugging of a Java application can be done using the Java Debugger (jdb) which is a dynamic, controlled, assignment-based debugging tool. It helps find and fix bugs in the Java language programs both locally and on the server. To use jdb in a J2EE application server you must first launch it with debugging enabled and attach to the server from the debugger through a JPDA port (Default port is 1044).
On JBoss, the debugger can be enabled by uncommenting this line in the file run.conf.bat (run.conf for Linux)
set "JAVA_OPTS=%JAVA_OPTS% -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"
After making sure JBoss is listening for incoming connections on port 8787, what is left is to tell Eclipse to connect to this port and you are ready to debug.
1. In Eclipse, navigate to Run > Debug
2. Select Remote Java Application , on the left column. Click New , on the bottom of the same column.
3. In the Create configuration screen you’ll be prompted to enter some values. Start with a meaningful name. For Project, select the Java project that contains the source code you want to debug. Leave Connection Type in default, i.e. Standard (Socket Attach) . For Host , enter the remote server hostname or IP address. For port, enter 8787 or the port you defined in your JBoss startup script.
4. Click Apply
5. Make sure JBoss instance is running in debug mode. In the same screen click Debug . Eclipse should automatically take you to the Debug perspective and you should see a stack trace in the Debug view.
6. If you are not automatically taken to the Debug perspective, select Window > Open Perspective > Other and then click Debug.
Why do I get the error “This compilation unit is not on the build path of a Java project” ?
If you see the error message “This compilation unit is not on the build path of a Java project.” it’s likely that your project file is missing the Java nature. Open the .project file and check that the Java nature is included in it. As an example, the following .project file holds the correct configuration for a Maven+Java project:
<projectDescription> <name>myproject</name> <comment></comment> <projects> </projects> <buildSpec> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>org.eclipse.m2e.core.maven2Builder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature> </natures> </projectDescription>
How to use Ant to build your project
By default your project is rebuilt using the settins defined into your Properties | Build Path.
Sometimes you might find useful to use an Ant script to rebuild your project, so that you can use it even from outside the development environment. (A typical example of it could be using an Ant script for building both the development project and the production project).
At first open up the project properties to the ‘Builders‘ section. Select the ‘New‘ button and then in the next dialog choose ‘Ant Build‘.
In the ‘Name’ field enter the name you want to give the builder (Ex. Ant builder). Use the ‘Browse Workspace‘ buttons to choose the location of the build file and the project root.
In the Targets tab you can configure the ‘Clean’ and ‘Build’ targets for your ant script. By convention, these targets are ‘clean’ and ‘build’. For ‘Clean’ you will want the targets to clean the binaries and then rebuild the project. Once you have completed configuration, select ‘OK’
Eclipse will now return you to the Builders section of the project properties dialog. Make sure that ‘Java Builder‘ is is not checked in the list. Select ‘OK’ to apply the changes.
Now you have a completely Ant based project, no Eclipse compilation involved.
How to add quickly all imports to your project
By default, when Eclipse encounter a class which has not already been imported, marks it with a red (+) to the user which can do a set of options (importing the class if it’s in the classpath, define a new one, rename the class to one which is visible to the class).
Fixing this issue for every class reference is time consuming and often causes Eclipse to hangs for some seconds while it’s elaborating all possible suggestions.
However there’s a wonderful option called Organize Imports (Source > Organize Imports or Ctrl + Shift + O ) which automatically adds all imports to your classes.
If there are multiple Classes with the same name, a dialog will prompt to ask which Class you want to import.
How to find quickly anything in Eclipse
One of the new-and-noteworthy features in Eclipse is Quick Access . Even if you’re a fan of the mouse, this keyboard shortcut can save time and money.
The Quick Access -which is activated by Ctrl + 3– is your window to anything; perspectives, views, open editors, commands, menus … just start typing, and it will show you the possible matches. It filters the most likely ones first, but if you press it again, will show all the possible matches that it knows about.
This is the quickest way to fine ANYTHING oin Eclipse.
How to open an Eclipse file from Windows Explorer
Sometimes it can be necessary to operate on your project files from the Windows Explorer. The less efficient way to achieve it is by
selecting the file properties, copying and pasting the file name in an Explorer window.
There’s an easy way to do it, by configuring Explorer as External Tool.
In order to do it: Create an external tool with location ${env_var:SystemRoot}\explorer.exe and arguments /select,${resource_loc} .
Any selected file or directory can now be shown in the windows explorer.
How to check the history of a file
Well, every real world project should be committed on a repository like SVN or CVS. Eclipse however has its own repository containing the history of your files.
This can be very useful to revert changes when necessary….or just in case you forgot to commit the file on the repository!
To use this feature, right click on the file of your choice and click Compare With -> Local history. That should open up the entries in the €˜History€™ view.
Clicking on any of the history entries will open a diff window telling you the difference between the current entry and the previous edits.
Use Templates !
Templates are one of the greatest time savers for Eclipse developers, yet not all users are aware of them. A template is a block of text which can be inserted in your code. It’s much more powerful concept than cut and past since it may contain some dynamic pieces of code as well.
To see the list of built-in templates and to define new ones go to Window -> Preferences. Select Java -> Editor -> Templates. You can add your own templates here.
How to trigger a template: simply type in your editor the template code and press ctrl+space. For example type if you have defined a template named sql, just type sql and ctrl+space to activate the template.
Code template variables:
Eclipse can automatically fill out dynamic code by sniffing out variables in a template. Here are a few you should make yourself aware of
${cursor} - Place the cursor here after code completion ${enclosing_method} Replace this with the method name where the code completion was called. ${enclosing_method_arguments} Replace this with the enclosing method arguments ${enclosing_type} The enclosing class name ${import} Dynamically import some classes ${importStatic} Same as above but with static imports ${time} Current time
And here are a few interesting templates which can be imported in your project:
Singleton Template:
private static ${enclosing_type} singleton = new ${enclosing_type}(); private ${enclosing_type}(){ } public static ${enclosing_type} getInstance(){ if(singleton == null){ singleton = new ${enclosing_type}(); } return singleton; }
Log4j Template: (Defines a logger for the current class)
${:import(org.apache.log4j.Logger)} private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);
How to set the proxy address in Eclipse
Well, most developers use Eclipse at work behind a proxy so it’s absolutely a must to configure the proxy for keeping your libraries updated. Well the good news is that newer releases of Eclipse are able to read your browser’s Proxy configuration, so it should work automatically.
If you have got an earlier Eclipse release or simply you want to customize your Proxy configuration, just point to Window > Preferences. Under the General element you can find the Network Connections panel which contains the proxy configuration.
Choose “Manual” to provide a custom proxy configuration. In the bottom part of the screen, you will be able to exclude the addresses which you don’t want to hit with your proxy.