# Drag and drop a jar file onto a sketch - cannot get it working

**URL:** <https://discourse.processing.org/t/drag-and-drop-a-jar-file-onto-a-sketch-cannot-get-it-working/41253>\
**Category:** Development\
**Created:** [March 12, 2023, 8:41pm UTC](https://discourse.processing.org/t/drag-and-drop-a-jar-file-onto-a-sketch-cannot-get-it-working/41253 "2023-03-12T20:41:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Svedblen](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@Svedblen](https://discourse.processing.org/u/Svedblen)\
**Post date:** [March 12, 2023, 8:41pm UTC](https://discourse.processing.org/t/drag-and-drop-a-jar-file-onto-a-sketch-cannot-get-it-working/41253/1 "2023-03-12T20:41:52Z")

</div>

Hi!

I’m trying to reuse the same code in a few different sketches and decided that I did not want to just copy the source code. I wanted something slightly better and read about the ability to drag and drop jar-files onto a Processing sketch. So I tried that but cannot make it work (I’m using Processing 4.0.1 on a Mac). I keep getting a “ **The package “mypackage” does not exist. You might be missing a library.** ” error message.

Here is my simple test class:

```auto
package mypackage;
public class MyClass {
  public int i;
}

```

which I compile and jar via the command line:

```auto
javac MyClass.java
jar cvf MyClass.jar MyClass.java

```

I then drag and drop the jar file onto my sketch. The jar file lands in the code directory of the sketch. Then I tried to reference the class in some sketch code, like this

```auto
import mypackage.MyClass;

```

When I then try to run the sketch I get the above error message,

Is this not supposed to work? Any hints appreciated.

/Lennart

---

<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:** [March 13, 2023, 2:18am UTC](https://discourse.processing.org/t/drag-and-drop-a-jar-file-onto-a-sketch-cannot-get-it-working/41253/2 "2023-03-13T02:18:02Z")

</div>

> [@Svedblen](#):
>
> Is this not supposed to work? Any hints appreciated.

I’m also very noob related to Java compiling & packaging.

But I once did 1 attempt experiment and then posted it here:

> **[GitHub - GoToLoop/Processing\_Jar\_Creation: Example for compiling a ".java"...](https://github.com/GoToLoop/Processing_Jar_Creation)**
>
> Example for compiling a ".java" file and package it as a ".jar" for Processing. - GitHub - GoToLoop/Processing\_Jar\_Creation: Example for compiling a ".java" file and p...

> [@Svedblen](#):
>
> I keep getting a “ **The package “mypackage” does not exist.**

As far as I can understand on how Java package works, if a “.java” file contains the keyword `package`, then its corresponding “.class” file inside a “.jar” file gotta be inside a subfolder which matches the name specified by the `package` command.

So if you have `package mypackage;` inside file “MyClass.java”, when you check the contents of its “.jar” file, you have to see a subfolder called “mypackage/” and file “MyClass.class” within it.

On my repo example, in its folder “PaletteJarCreationFolder/”:

> <https://github.com/GoToLoop/Processing_Jar_Creation/tree/master/PaletteJarCreationFolder>
>
> //github.com/GoToLoop/Processing\_Jar\_Creation/tree/master/PaletteJarCreationFolder

I’ve placed my “.java” file “[Palette.java](https://github.com/GoToLoop/Processing_Jar_Creation/blob/master/PaletteJarCreationFolder/Palette.java)” & the “.bat” file “[CreateJarFile.bat](https://github.com/GoToLoop/Processing_Jar_Creation/blob/master/PaletteJarCreationFolder/CreateJarFile.bat)”.

After running the “.bat” file, it generated file “[Palette.jar](https://github.com/GoToLoop/Processing_Jar_Creation/blob/master/PaletteJarCreationFolder/Palette.jar)” and subfolder “[malcolm/](https://github.com/GoToLoop/Processing_Jar_Creation/tree/master/PaletteJarCreationFolder/malcolm)”, plus file “[Palette.class](https://github.com/GoToLoop/Processing_Jar_Creation/blob/master/PaletteJarCreationFolder/malcolm/Palette.class)” inside the subfolder.

That’s all I have for now. Hope it helps. Ask again if you haven’t solved it yet.

---

<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 13, 2023, 6:39am UTC](https://discourse.processing.org/t/drag-and-drop-a-jar-file-onto-a-sketch-cannot-get-it-working/41253/3 "2023-03-13T06:39:31Z")

</div>

Hi @Svedblen,

This can’t work because you missed out to have the correct structure in you jar … read here…

> [@How to create a library from projects](https://discourse.processing.org/t/how-to-create-a-library-from-projects/41151/4):
>
> 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 create a directory structured …

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Svedblen](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@Svedblen](https://discourse.processing.org/u/Svedblen)\
**Post date:** [March 13, 2023, 7:50am UTC](https://discourse.processing.org/t/drag-and-drop-a-jar-file-onto-a-sketch-cannot-get-it-working/41253/4 "2023-03-13T07:50:57Z")

</div>

Thanks both to @GoToLoop and @mnse. Of course I missed on the structure. I fixed that and now it works just fine! Once again - thanks a lot.

/Lennart

---

<div class="post-metadata">

**Author:** ![Svedblen](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@Svedblen](https://discourse.processing.org/u/Svedblen)\
**Post date:** [March 13, 2023, 11:26am UTC](https://discourse.processing.org/t/drag-and-drop-a-jar-file-onto-a-sketch-cannot-get-it-working/41253/5 "2023-03-13T11:26:43Z")

</div>

I just wanted to report some significant progress on my behalf. The reason I wanted to have a class or two in a jar file and being able to drop it into a sketch was the following.

I was playing around with some socket program, using one sketch as server and another one as a client. That went well when sending e.g. strings through Data Output and Data Input streams. I then tried to send serialized objects between them, through Object Output and Object Input streams. That went well for “standard” java classes but not so much for my own classes, although they implemented Serializable. They serialized fine but the receiving end did not recognize them, because they were built in a not recognized “context” (that of the PDE of the sending sketch).

So I realized that both the client and the server needed to have the same “compiled version” (or whatever you call it) of the objects to serialize and deserialize. In comes the jar, which then both sketches import and use.

And voilá! I passed my own serializable object from one running sketch to another. Cool!
