# Importing a "Java Processing" library into py5

**URL:** https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444
**Category:** py5
**Created:** [May 18, 2024, 7:21pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444 "2024-05-18T19:21:13Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Gof](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gof/32/19588_2.png) [@Gof](https://discourse.processing.org/u/Gof)
#### Post date: [May 18, 2024, 7:21pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/1 "2024-05-18T19:21:13Z")

</div>

Hello,  
I’m a beginner and a new py5 user and I’m very enthusiastic about this new version of Processing.  
I read in the tutorial that you could use “Processing java” libraries with the camera3D example. Is it possible to import other libraries? I’m trying to import “Peasycam” but without success.

I’m trying

import jpype.imports  
import py5\_tools  
py5\_tools.add\_jars(‘/jars’)  
from peasycam import Peasycam

Thanks for your help

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 18, 2024, 9:54pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/2 "2024-05-18T21:54:33Z")

</div>

As far as I know, users can’t run py5 code in the Processing 4 editor. Usually you would need to use an editor called ‘Thonny’ with a py5-plugin. An alternative would be to use an older version of Processing (3.5.4); it does support Python, but not py5, which is new and hopefully will be added to the Processing editor in the future.

---

<div class="post-metadata">

### Author: ![Gof](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gof/32/19588_2.png) [@Gof](https://discourse.processing.org/u/Gof)
#### Post date: [May 19, 2024, 9:18am UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/3 "2024-05-19T09:18:13Z")

</div>

Yes, I use py5 with ‘Thonny’. In the py5 references they explain a way to import ‘java’ libraries, and it works with the ‘camera3D’ library. But I try with other libraries but without success…  
Yes, I hope that in the future py5 will be added to the Processing editor. 👍

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 19, 2024, 11:30am UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/4 "2024-05-19T11:30:12Z")

</div>

@tabreturn can you shed any light on this?

---

<div class="post-metadata">

### Author: ![mcintyre](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mcintyre/32/14561_2.png) [@mcintyre](https://discourse.processing.org/u/mcintyre)
#### Post date: [May 19, 2024, 2:19pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/5 "2024-05-19T14:19:46Z")

</div>

@hx2A what do you think?

@Gof you can import other Processing libraries without needing `py5_tools` if your sketch folder is organized like so:

```auto
sketch001/
├─ sketch.py
├─ jars/
│ ├─ library01.jar
│ ├─ library02.jar

```

That said, the code snippet you shared seems to have one minor bug: the path to your `jars` folder is absolute `/jars` instead of relative `./jars`. Try `py5_tools.add_jars('jars')` or `py5_tools.add_jars('./jars')`.

---

<div class="post-metadata">

### Author: ![Gof](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gof/32/19588_2.png) [@Gof](https://discourse.processing.org/u/Gof)
#### Post date: [May 19, 2024, 2:55pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/6 "2024-05-19T14:55:41Z")

</div>

Thanks @mcintyre, you’re right I don’t need to use py5\_tools !  
It works like this

```auto
import py5
import jpype.imports

from peasy import PeasyCam

def setup():
    global cam
    py5.size(400,400,py5.P3D)
    sketch = py5.get_current_sketch()
    cam = PeasyCam(sketch,500)
      
    
def draw():
    py5.background(200,0,50)
    py5.rotate_y(py5.frame_count*0.01)
    py5.box(100)
    

py5.run_sketch()

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 19, 2024, 4:11pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/7 "2024-05-19T16:11:08Z")

</div>

I’ve also tested in Win10 + Python 3.12.2 + Java 21.0.2 using just the terminal (no Thonny).  
This import line isn’t needed: `import jpype.imports`:

```auto
import py5
from peasy import PeasyCam

BG = 0xff_C8_00_32 # medium-dark pink-red
FG = 0xff_FF_FF_00 # yellow

ROT_STEP = .05 # rotation speed

def setup():
    py5.size(400, 400, py5.P3D)

    py5.fill(FG)
    py5.stroke_weight(2)

    PeasyCam(py5.get_current_sketch(), 500)

def draw():
    py5.background(BG)
    py5.rotate_y(py5.frame_count * ROT_STEP)
    py5.box(50)

__name__ == ' __main__' and py5.run_sketch()

```

---

<div class="post-metadata">

### Author: ![hx2A](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hx2a/32/14322_2.png) [@hx2A](https://discourse.processing.org/u/hx2A)
#### Post date: [May 19, 2024, 4:18pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/8 "2024-05-19T16:18:55Z")

</div>

Thank you everyone for your responses here! I’m really excited to see the community coming together to answer this.

One thing I’ll add is that you don’t need `import jpype.imports` is because `import py5` takes care of that for you. Early versions of py5 did not, but this has been in py5 for a while now.

Putting the jars in a “jars” subdirectory is the easiest way to add Jars to a Sketch. Jars must be added to the Java Virtual Machine (JVM) before the JVM is started, which happens when `import py5` is called. For folks using py5 in imported mode via Thonny, you can’t use py5\_tools to add the Jars because the JVM will always be already running by that time. Instead, put the Jars in a “jars” subdirectory relative to the current working directory (`os.getcwd()`). py5 will always check for this before starting the JVM.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 19, 2024, 4:55pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/9 "2024-05-19T16:55:49Z")

</div>

Could you explain the procedure that you use to run this demo using Terminal? I’d like to try it on a Mac.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 19, 2024, 8:59pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/10 "2024-05-19T20:59:57Z")

</div>

Turns out it’s not too difficult to run py5 files from terminal: just type ‘_python3_’ followed by the name of the file.

**Example:**

 ![py5](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/b/fb5fa568d538f3579839af67aff727c5641d171a.png)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2024, 4:38am UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/11 "2024-05-20T04:38:10Z")

</div>

> [@svan](#):
>
> : just type ‘_python3_’ followed by the name of the file.

- We can also do it shorter w/ just `"py"`. 😉
- And if we name our sketch `" __main__.py"`, we can run it w/ just 1 dot: py . 🧙
- Also, if we are 1 folder above our sketch project, we can run it passing the name of that folder if it has a `" __main__.py"` inside! 🐍

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 20, 2024, 5:37am UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/12 "2024-05-20T05:37:18Z")

</div>

My mac doesn’t much care for ‘py’; it responds with ‘command not found’.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2024, 6:14am UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/13 "2024-05-20T06:14:10Z")

</div>

A pity, since just “py” is much quicker to type in. 🏎

BtW, I’ve found this undocumented `_instance` property in py5 which allows us to access Processing’s full API beyond those provided w/ “snake\_case” names. 😁

As long as we don’t pass numbers larger than MAX\_INT we can invoke all PApplet’s functions w/ their “originalCamelCaseNames”: 😇

```auto
import py5
from peasy import PeasyCam

ROT_STEP = .05 # rotation speed
FILENAME = 'cube_####.png'

this = py5._instance

def settings(): this.size(400, 400, this.P3D)

def setup():
    this.fill(255, 255, 0) # yellow
    this.strokeWeight(2)

    PeasyCam(this, 500)

def draw():
    this.background(200, 0, 50) # medium-dark pink-red
    this.rotateY(this.frameCount * ROT_STEP)
    this.box(50)

def key_pressed():
    this.key == ' ' and this.saveFrame(this.dataPath(FILENAME))

__name__ == ' __main__' and py5.run_sketch()

```

P.S.: Method **dataPath()** doesn’t exist in py5’s API, not even as **data\_path()**!

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 20, 2024, 12:16pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/14 "2024-05-20T12:16:03Z")

</div>

When trying to run the demo that you posted either in Thonny of from the command line I get this error:

` ModuleNotFoundError: No module named 'peasy'` How did you get your system to recognize ‘peasy’?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2024, 2:29pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/15 "2024-05-20T14:29:34Z")

</div>

> [@svan](#):
>
> How did you get your system to recognize ‘peasy’?

Well, I didn’t know how. I’ve just followed @mcintyre’s instruction from his 1st reply in this thread:

> [@mcintyre](#):
>
> ```auto
> sketch001/
> ├─ sketch.py
> ├─ jars/
> │ ├─ library01.jar
> │ ├─ library02.jar
> 
> ```

In my case, I only had those 2 “.jar” files from my Processing 3’s “libraries” subfolder:

1. “peasycam.jar”
2. “peasy-math.jar”

They’re located at “sketch’s folder”\libraries\peasycam\library\

So apparently, although py5 is using Processing 4, some Processing 3’s libraries are still compatible!

Anyways, here’s my actual “PeasyCam/” py5 project folder:

```auto
PeasyCam/
├── __main__.py
└── jars/
    ├── peasycam.jar
    └── peasy-math.jar

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2024, 4:16pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/16 "2024-05-20T16:16:09Z")

</div>

Found out how to convert Python’s `int` values greater than MAX\_INT to Java’s 32-bit signed `int` using “jpype”'s **JInt()** function:

```auto
import py5

from jpype import JInt
from peasy import PeasyCam

BG = JInt(0xff_C8_00_32) # medium-dark pink-red
FG = JInt(0xff_FF_FF_00) # yellow

ROT_STEP = .05 # rotation speed
FILENAME = 'cube_####.png'

this = py5._instance

def settings(): this.size(400, 400, this.P3D)

def setup():
    this.fill(FG)
    this.strokeWeight(2)

    PeasyCam(this, 500)

def draw():
    this.background(BG)
    this.rotateY(this.frameCount * ROT_STEP)
    this.box(50)

def key_pressed():
    this.key == ' ' and this.saveFrame(this.dataPath(FILENAME))

__name__ == ' __main__' and py5.run_sketch()

```

Now we can pass big values like `0xff_C8_00_32` or `0xff_FF_FF_00` to Java methods in Processing, such as **fill()** and **background()** via _py5.\_instance_. 🤩

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 20, 2024, 5:24pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/17 "2024-05-20T17:24:34Z")

</div>

I’m missing those two jars which I now know where to find them. Very helpful. Thanks, I’m going to try it.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 20, 2024, 6:02pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/18 "2024-05-20T18:02:41Z")

</div>

Works like a charm. I copied those two jar files and pasted them in a ‘jars’ folder in another folder that I created to hold all of my Python files. There is no sketch folder created by py5. It worked just the same. I ran it in both Thonny and from the cmd-line.

 ![peasy](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/2/629875689308774a6dd516118f3dc47d1adbbbe2.png)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 20, 2024, 6:21pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/19 "2024-05-20T18:21:16Z")

</div>

> [@svan](#):
>
> … and pasted them in a ‘jars’ folder in another folder that I created to hold all of my Python files.

Is that a folder junction or hardlink? Perhaps a Thonny special folder?

> [@svan](#):
>
> There is no sketch folder created by py5.

Well, py5 is just a Python package we can install via pip.  
It’s not supposed to create any folders.  
BtW, I have a folder called py5 where I create subfolders for py5 related projects/sketches.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 20, 2024, 6:27pm UTC](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444/20 "2024-05-20T18:27:22Z")

</div>

> [@GoToLoop](#):
>
> BtW, I have a folder called py5 where I create subfolders for py5 related projects/sketches.

I use the same technique except I call it ‘Python’ and the ‘jars’ folder is in there. There is no subfolder system, and there’s all types of .py files in the ‘Python’ folder including py5. I’ve thought of splitting those out like you did, but haven’t done it yet.

[Next page](https://discourse.processing.org/t/importing-a-java-processing-library-into-py5/44444.md?page=2)
