How to create a library from projects

hello, when creating a larger project, I ran into the problem that everything is in one project. I would like to create classes and use it in another project as a library. If there is such an opportunity in “Processing”, then tell me how to do it.

Hello,

Take a look here:

Take a look at the use of tabs in the Processing IDE:

I have a number of files that I simply drag and drop into Processing sketch that contain methods and classes for reuse. These will be separate tabs.

Related:
https://github.com/processing/processing4/issues/248

:)

Hi

Some hints

Hi @urlxd,

imo this is not really a processing question, but rather a java one…
Also don’t know what OS you are working on so assuming Windows by using processing java mode.
Further not knowing if you want to create a contributed library or only some function you want provide for yourself.

if first one, look here:

if the latter you can build a simple library depending on your java knowledge yourself.

Just a demonstration not a recommendation :slight_smile:

  1. create a directory structured for your java files and add some functionallity

In a command prompt this could look like:

c:\> mkdir c:\mylib\my\tools
c:\> cd /D c:\mylib
c:\mylib>

  1. create your java file(s) ie:

first file: c:\mylib\my\tools\MyStaticTools.java
ie: provides static methods for use as an example

package my.tools;

public class MyStaticTools { 
 public static int addValues(int a, int b) {
   return a+b;
 }

 public static int subValues(int a, int b) {
   return a-b;
 }  
}

second file: c:\mylib\my\tools\MyClassTools.java
ie: provides class methods for use as an example

package my.tools;

public final class MyClassTools { 
 public int multValues(int a, int b) {
   return a*b;
 }

 public int powValue(int a) {
   return a*a;
 }  
}
  1. back in the command prompt (assuming you are already in the directory from step 1) type :
    assuming c:\processing is the directory where you’ve installed processing
  • The first command build .class files from you .java files into a bin directory
c:\mylib>c:\processing\java\bin\javac.exe -d bin my\tools\*
  • The second command build a structured .jar library from your .class files
c:\mylib>c:\processing\java\bin\jar.exe -cf mytools.jar -C bin my

As a final result you should see a mytools.jar file in the directory.

The content of the mytools.jar can be verified afterwards by command

c:\mylib>c:\processing\java\bin\jar.exe -tf mytools.jar

And should look like:

META-INF/
META-INF/MANIFEST.MF
my/
my/tools/
my/tools/MyClassTools.class
my/tools/MyStaticTools.class

Afterwards drag and drop that file onto your PDE Window. This will create a folder named code in your sketch folder and loads the jar file for usage. If that all was successfully you can then use the provided functions in your sketch (minimalistic sketch example):

import my.tools.MyStaticTools;
import my.tools.MyClassTools;

println(MyStaticTools.addValues(1,2));

MyClassTools mytools = new MyClassTools();
println(mytools.powValue(2));

exit();  

The console output:

3
4

Cheers
— mnse

EDIT:: fix typo

1 Like