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

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:

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

which I compile and jar via the command line:

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

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

2 Likes

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

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

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/”:

I’ve placed my “.java” file “Palette.java” & the “.bat” file “CreateJarFile.bat”.

After running the “.bat” file, it generated file “Palette.jar” and subfolder “malcolm/”, plus file “Palette.class” inside the subfolder.

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

3 Likes

Hi @Svedblen,

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

Cheers
— mnse

1 Like

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

1 Like

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!

1 Like