Blog

How I made my own Processing Java Library

19 Jul 2022 • 4 min
A generative scribble as a cover image

The spark

Since launching my own shop I’ve noticed that I wanted to spend more time improving my generative art making workflow. By writing better code my work files become hopefully more stable and easy to work with. During the years I’ve created quite a bunch of helper classes and methods that help me with f.e. picking random numbers, color palettes, vector math, collision detection and much more. This meant that for each new project I needed to copy and paste all these classes & methods. Occasionally once of these classes or methods could have a small bug or improvement had a small bug I needed to replace this in all my projects. It was time to look into creating my own library. One place to store all my helper classes & methods which can be imported in all my projects. But how was I going to create own?

int randomInt (int min, int max) {
	return floor(random(min, max));
}

Code example of a helper function that returns a random integer

Heads up! I don’t call myself a programmer so I’d probably use all kinds of wrong programming terminology. So bear with me ;)

Failing with Eclipse

When I have a creative coding question I most of the time turn to my favourite Youtube teacher Daniel Shiffman. And for this question Daniel Shiffman had a tutorial about creating your own Processing Java library with Eclipse. I watched these tutorials, started with following along and created a first test library which worked to some degree. I ran into some random errors, had no idea what the Eclipse build was doing and I found the Eclipse interface way too complex and outdated for my taste. I just wanted to write a couple of Java classes and compile these into a .jar file which I can use as a library within Processing. I liked coding in the Visual Studio Code editor, that’s also where I’ve coded this blog with, so I started looking if I could code a java project inside of Visual Studio Code.

The solution: Visual Studio Code

After doing some quick research I found out that you could code java projects with Visual Studio Code, see docs! I followed the tutorial and got my first Hello World! program to work. Now it was time create my own library. I first needed to add the Processing’s core.jar file as a referenced library so I could access Processing’s functionalities. Then I ported my .pde helper methods into pure .java classes and built these into a .jar file to use as a library in Processing. Building a .jar file is as simple as pushing a button inside Visual Studio Code. Note: Be sure to uncheck Processing’s core.jar inside the built (see screencapture below) otherwise Processing will give an error. The only disadvantage to this approach is that I need to copy the built .jar file to the library directory within Processing each time I build a new .jar file. So it’s a bit more manual labor but it works! ;)

Screencapture of building my library in Visual Studio Code

Step-by-step

  1. Write code in .java inside Visual Studio Code
  2. VS Code recognises that you’re writing Java and creates a Java project
  3. Be sure to add the core.jar of Processing as a referenced library
  4. Write code and build your library by pressing the arrow icon (see screencap)
  5. Uncheck the core.jar in the build
  6. Copy the built .jar file and add it inside a folder that has the same name as the jar file inside to the Processing libraries directory
  7. Repeat steps 4, 5 and 6 while developing

This process ofcourse feels a bit tedious and could probably be optimised a lot. But I don’t know a lot about Java projects so if it works - it works ;)

In use

Below a small example of how I’m using my own library within Processing Java. I like to use static classes so I can call my classes method directly within my code without initialising objects of that class.

import studio.misha.MUtils; // import specific library class

void setup () {
	size(400, 400);
	MUtils.p5 = this; // pass reference to this sketch to the p5 variable
}

void draw () {
	// Draw stuff...
	println(MUtils.randomInt()); // these methods can be called from anywhere
}

Example of using my MUtils helper class

Alright that’s it for this post. Hope this gives somebody some insights. If you have any questions or remarks please let me know via e-mail or via DM ;)


Misha Heesakkers

Digital designer by day / Generative artist by night