How I can make a sketch with libraries in Eclipse?

edited June 2015 in Common Questions

How I can make a sketch with libraries in Eclipse?

There are already several articles explaining how to develop Processing applications with Eclipse. Starting with the official one: Processing in Eclipse.

Doing a search in the forums can also help in solving some issues. If you use the search at forum.processing.org, it will search only the current forum. The search at processing.org also searches the old forum, the reference, the wiki, etc.

This page is only a visual tour, offering a perhaps more up-to-date view of the screenshots, as procedures change slightly with each new version of Eclipse. The following ones were taken with Eclipse Helios, 3.6.

Once you install Eclipse and run it, you can go to File > New > Java Project menu. You will get a wizard to input some information to create a new (Processing) project.

NewProject

I defined the name of the project (the sketch name), the path where to store it (location), and kept the other defaults.

Add core library

AddLibraries1

In the project generation settings, you can add new libraries. You must always include at least core.jar, found in the lib folder of the Processing installation. You can add it by clicking on the Add External JARs button and navigating there.

Add extra libraries

AddLibraries2

You can add extra libraries. Here, in modes/java/libraries/pdf/library (for Processing 1.5), the PDF libraries are added.

Add OpenGL libraries

AddLibraryOpenGL

If you use OpenGL, you have to add the three (in Processing 1.5.1) jars found at modes/java/libraries/opengl/library. Then you have to click on the arrow on the left of jogl.jar, to see the Native library location item. Click on it, then on the Edit... button. Navigate to modes/java/libraries/opengl/library and choose one folder here, corresponding to your system (windows32 in my case).

You can then add import processing.opengl.*; to your sketch and add OPENGL to the size() call.

Create the main class

MakeMainClass

You end with an empty project. You can go to File > New > Class (green icon) to create a new class file. Eclipse will generate a skeleton class for you, to be changed.

Generated class

GeneratedClass

Base class to edit. Style & formatting can be changed in the Preferences.

A simple sketch

PDFCode

A simple Processing code, taking advantage of some Java facilities, like annotations.

Add new libraries

AddLibraries3

If your code needs another non-Processing library, you can add it to your project, using Project > Properties. Choose Java Build Path, and select the Libraries tab. You can then use the Add External JARs button to add additional library jars.

Tricks

If you type a class name, it will be marked in red, as Eclipse doesn't know where to find it by default. For example, type File f = new File("x");

Hover over the File name. Eclipse will show a tooltip, indicating the error ("File cannot be resolved to a type") and giving possible actions on this error. Choose the first one ("Import File (java.io)") and it will add the import declaration at the start of the class.

Tagged:
Sign In or Register to comment.