I would like to learn how to make a simple java library containing various functions usable in Processing.
A simple jar file that I put in a sketchbook directory called code …
Does a tutorial exist somewhere?
Thanks for your help.
Have a good day.
I don’t know about a tutorial, but I know that you can put libraries as singular .jar files in Sketchbook\libraries\(library name)\library\(library name).jar - and on next Processing startup it will recognize it being there - then you use the “Sketch” drop-down menu in Processing, then “Libraries”, and it should be at the bottom of the list.
If I remember correctly, (library name) should be the same for folder name and .jar file name, for it to work. That might not be true though, but still is a good practice for organization.
I already use external libraries that I put in a directory of the sketchbook. It works fine.
Today, I would like to learn how to do this kind of libraries. A simple library containing functions.
Why ? to lighten the code in Processing.
If I understood you correctly, you want to make a .jar file that could be used as a library, with Processing. Is that correct?
I feel like a thing that would work fine is to take the Processing sketch you want to export into a library, then export it as executable, go into its path, then “lib” directory, and snatch the .jar file that’s named as that sketch - and use it as your library.
For understanding purposes, you can look into a .jar file as an archive and see how .class files are arranged there, figure out how these in some other library relate to the import and usage procedures you do with that library, and then use the same relation to figure out how to use your own library.
No problem!
I just checked, and it seems to work completely. Had to find out how to import a library to just one sketch though, but it does work as a library now!
Then, I made a new sketch and simply drag-n-dropped the .jar file right onto the Processing window. And it just instantly worked.
Now, it would have probably been better if I were to make f1 and banana definitions static though, so that I wouldn’t have to define a testBanana to get the function and the class off it, but, hey, it works.
To conclude,
How to make your own library using Processing ?
1-Create a sketch (here myLib) containing your functions
In my example below 2 functions numDayOfYear() and date2sec1970()
Yeah… With other, big IDEs and their trees of trees everywhere - even in settings, the idea of just dragging and dropping libraries right onto your code and have them magically work, it’s unusual! But awesome!
I’m in awe of the combination of simplicity and power of Processing… I think I wouldn’t have been able to jump to start making real Java code completely knowing what I’m doing without it.
However, it says that the library cannot be found.
No library found for GOLDSTAR_LIBRARY1
Libraries must be installed in a folder named 'libraries' inside the sketchbook folder (see the Preferences window).
The package “GOLDSTAR_LIBRARY1” does not exist. You might be missing a library.
I normally just use the processing library template and the process highlighted by Dan Shiffman in his videos on the topic but if you have a .java file I can produce the lib file for you.