# How to create a library from projects

**URL:** <https://discourse.processing.org/t/how-to-create-a-library-from-projects/41151>\
**Category:** Libraries\
**Created:** [March 5, 2023, 4:47pm UTC](https://discourse.processing.org/t/how-to-create-a-library-from-projects/41151 "2023-03-05T16:47:04Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [March 6, 2023, 8:48am UTC](https://discourse.processing.org/t/how-to-create-a-library-from-projects/41151/4 "2023-03-06T08:48:59Z")

</div>

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:

> **[Library Basics](https://github.com/benfry/processing4/wiki/Library-Basics)**
>
> Processing 4.x releases for Java 17. Contribute to benfry/processing4 development by creating an account on GitHub.

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

_ **Just a demonstration not a recommendation 🙂** _

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

> _In a command prompt this could look like:_

> ```auto
> 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_

> ```auto
> 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_

> ```auto
> 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

> ```auto
> c:\mylib>c:\processing\java\bin\javac.exe -d bin my\tools\*
> 
> ```

- The second command build a structured `.jar` library from your `.class` files

> ```auto
> 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

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

And should look like:

> ```auto
> 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):

```auto
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

---

_[View the full topic](https://discourse.processing.org/t/how-to-create-a-library-from-projects/41151)._
