How to use CheckStyle plugin in Eclipse

Problems found by CheckStyle in Editor
Problems found by CheckStyle in Editor

CheckStyle is a popular Java tool to ensure that source code adheres to a coding standard and is useful to enforcing style in a team or company. It is available for Eclipse as the Elipse CheckStyle plugin. CheckStyle is packaged along with this plugin and does not need to be separately installed.

  • Install: To install CheckStyle in Eclipse, go to Help -> Install New Software and add the URL http://eclipse-cs.sf.net/update to add the plugin.

  • CheckStyle comes pre-installed with the style of Google and other configuration files. You can view these listed in Window -> Preferences -> CheckStyle.

  • Add style: You can also add your own style configuration files by choosing New -> External Configuration File in the CheckStyle preferences dialog.

  • To set the styles to be applied on a project, right-click on it and choose Properties -> Checkstyle and choose the style you want from the dropdown.

  • Activate style: Activating CheckStyle for a project makes it check the project source files whenever they are saved. To do this, right-click on the project and choose CheckStyle -> Activate Checkstyle.

  • To manually check the style, right-click on the project and choose Properties -> Check Code with Checkstyle.

Tried with: CheckStyle 6.2, Eclipse 4.4.1 (Luna) and Ubuntu 14.04

How to include or exclude files in Eclipse project

Directories excluded from a project in Eclipse
Directories excluded from a project in Eclipse

You may sometimes need to selectively include or exclude certain files or directories in an Eclipse project. For example, I had a data subdirectory in my Eclipse project with thousands of files. Eclipse would spend lots of time needlessly indexing these files. This can be prevented by just excluding that directory from the project.

  1. Right-click on the project and choose Properties. Choose Resource -> Resource Filters.

  2. In the Resource Filters dialog, click Add and choose which files and directories to either Include Only or Exclude All. Only provide the file or directory name, not a relative path or absolute path!

  3. Refresh the project to update these settings. The excluded files or directories should disappear from the project.

If you are curious, Eclipse puts this setting in the .project file for your project. For example, excluding a subdirectory named zoomba in a project, resulted in Eclipse adding this XML snippet to the .project:

<filteredResources>
<filter>
<id>1433235573506</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-zoomba</arguments>
</matcher>
</filter>
</filteredResources>

view raw
eclipse_exclude.xml
hosted with ❤ by GitHub

Tried with: Eclipse Luna 4.4.2 and Ubuntu 14.04

How to install DLTK Ruby plugin in Eclipse

20150121-ruby

The Dynamic Languages Toolkit (DLTK) plugin is a popular method to work with Ruby in Eclipse. Aptana has a Ruby/Rails plugin, but most programmers seem to recommend the DLTK plugin over it.

To install the DLTK Ruby plugin:

  • Install Eclipse, if you do not already have it. I like to start off with a minimal Eclipse installation.

  • Go to Help -> Install New Software. From the dropdown choose All Available Sites. After the list below fills up, choose Programming Languages -> Dynamic Languages Toolkit – Ruby Development Tools. Install it and restart Eclipse.

  • Configure the Ruby or JRuby interpreter you want to use in Window -> Preferences -> Ruby -> Interpreters.

You can create Ruby projects and edit, run and debug .rb files now in Eclipse! 🙂

Tried with: DLTK Ruby 5.0.0, Eclipse 4.4.1 and Ubuntu 14.04

How to add Python 3 as interpreter in Eclipse

Python 3 as interpreter in Eclipse
Python 3 as interpreter in Eclipse

The PyDev plugin adds support for working with Python files to Eclipse. However, it is only auto-configured to work with a Python 2.x interpreter. Furthermore, Ubuntu installs Python 2.x by default and needs some manual work to work with Python 3.x.

To use Python 3.x for your Python files in Eclipse:

  • First install Python 3.x:
$ sudo apt install python3
  • This installs the Python 3.x interpreter as /usr/bin/python3. We will use this in Eclipse.

  • In Eclipse, open Preferences -> PyDev -> Interpreters -> Python Interpreter. Click Add and add /usr/bin/python3. The Libraries and other settings will be auto-discovered by PyDev.

  • That is it! When you create a new Python project, in its dialog remember to pick Grammar version as 3.0 and the Interpreter as Python3.

Tried with: Python 3.4.0, PyDev 3.9.0, Eclipse Luna 4.4.1 and Ubuntu 14.04

How to ignore PEP8 error or warning in PyDev

Ignore PEP8 warning or error in PyDev
Ignore PEP8 warning or error in PyDev

PyDev can be configured to analyze your code to check if it complies with the PEP8 guidelines. It generates errors and warnings if the code does not comply. For example, E501 complains if a line is longer than 80 characters.

Sometimes, you may want to ignore some specific errors or warnings of PEP8. To do this, go to Windows -> Preferences -> PyDev -> Editor -> Code analysis -> PEP8.py. Here add an --ignore string with the error and warning numbers you want ignored. For example, --ignore=E501. Restart Eclipse for changes to take effect.

Tried with: PyDev 3.9.0, Python 3.4.0, Eclipse Luna 4.4.1 and Ubuntu 14.04

How to set guideline in Eclipse

Set print margin in Eclipse
Set print margin in Eclipse

A vertical guide line can help the programmer be aware of and comply with the line width of code. The guideline is called print margin in Eclipse.

It can be found in Windows -> Preferences -> General -> Editors -> Text editors section. You can also set the line width you want for the guideline.

Tried with: Eclipse Luna 4.4.1 and Ubuntu 14.04

PyLint not running in Eclipse

Problem

PyLint in Eclipse is not being executed when you save a Python file open in the Editor. PyLint worked fine for me with Python 2.x. But, when I used a Python 3.x interpreter, PyLint gave this problem.

Solution

20150102-pylint-error

Add the directory that contains the source file to Project -> Properties -> PyDev – PYTHONPATH -> Source Folders. Restart Eclipse. PyLint should work after this.

Tried with: PyLint 1.4.0, Python 3.4.0, Eclipse 4.4.1 and Ubuntu 14.04

How to use PyLint for Eclipse

Problems found and listed by PyLint in Eclipse
Problems found and listed by PyLint in Eclipse

Eclipse can be used to work with Python files by installing the PyDev plugin (by pulling it online or manually. PyLint can be used to improve the quality of Python code and to ensure it complies with PEP 8 guidelines.

To enable and use PyLint in Eclipse:

  • Install PyLint. For Python 2.x, this can be done from Ubuntu repositories. If you are using Python 3.x, then see this post.

  • Go to Window -> Preferences -> PyDev -> PyLint.

  • Enable the Use PyLint option.

  • By default, the output of PyLint is displayed as text in the Console view. Instead, the problems found by PyLint can be marked as errors or warnings in your editor window and listed in the Problems view. These options can be set in the same dialog.

  • Add the path to the PyLint executable.

  • Choose how you want the various problems reported by PyLint to be reported as: error, warning or information.

  • Click Apply, then OK. Restart Eclipse.

  • Now whenever you save a Python file, PyLint is executed. Its output is either shown in the Console view or marked out in the Editor and shown in the Problems view.

Note: If PyLint does not execute when you save a Python file, see this post on how to fix this issue.

Tried with: PyLint 1.4.0, Eclipse Luna 4.4.1 and Ubuntu 14.04

How to piece together your own Eclipse

Compared to Eclipse CDT package, my Eclipse C++ IDE has very few features and plugins
Compared to Eclipse CDT package, my Eclipse C++ IDE has very few features and plugins

Eclipse is developed as a modularized system with a central core platform and additional features and plugins that provide functionality required for a certain language (like Java or C++). These packaged versions of Eclipse that are put together for different languages is what is provided on its download page.

You may feel that the Eclipse packages available for Java or C++ (CDT) are extremely bloated with hundreds of features and plugins that you may never use. For example, Eclipse CDT includes hundreds of MyLyn features and cross-compiler support, none of which I may require right now. The beauty of Eclipse is that everything is a plugin, so you can actually build yourself a lean Eclipse that only includes what you want. This way you can build a minimal Eclipse that has only the features you want.

Step 1: Get the platform runtime binary

  • The core of Eclipse is provided for download as Eclipse Platform Runtime Binary. To get it, go to this page and click on the build number you want. It opens a new page, where you scroll down to the section named Platform Runtime Binary and download the package matching your system.

  • Unzip the zip file you downloaded and run the eclipse binary file. The IDE opens and it works.

Step 2: Add the plugins

  • Now add the plugins you want from Help -> Install New Software. For plugins provided by Eclipse, choose Luna from the dropdown. For external plugins, add their URL.

  • For creating a Eclipse C++ IDE of my own, I installed these plugins:

  • For creating a Eclipse Python IDE of my own, I installed these plugins:

Tried with: Eclipse 4.4.2 and Ubuntu 14.04