Including shared pde files across projects


#1

Honey gave me an answer to my question How can I make an include file for my scripts - SOLVED THANKS which to paraphrase said stick your variables in a file called file.pde in the same directory and it is included automagically. And that works nicely BUT

I want to share my variables between projects, so I thought I could just move the file up a level to the projects directory and that might work, but no I get a message variable cannot be resolved.

So I hunted about looking for an answer and there was a bunch of stuff that talked about tabs and classes and classpath, but none of them seemed to give what I expect is a simple answer.

How can I create a file containing variables and put it somewhere that I can then pull it into my multiple different projects i.e like #include ./somefile.pde

It seems to me that if you can stick a file name.pde in the directory and it gets included automagically there must be a nice straight forward way to stick it somewhere else and pull it in at compile/run time.


#2
  • For “.pde” files, I don’t think it is possible to share them globally for all sketches. :cry:
  • Processing’s IDE (PDE) offers a way for us to import “.jar” libraries into our sketches.
  • Therefore, you’re gonna need to create a “.jar” file containing your variables/constants in it.
  • It’s a hard long road. 1st we start w/ a “.java” file, which’s compiled as a “.class”, and then packaged/compressed as a “.jar”.
  • And finally we follow the instructions from the wiki below in order to place that “.jar” file in the “sketchFolder/libraries/”, so it can be imported like any regular Processing 3rd-party library:

#3

As a prelude, create a new sketch w/ its main tab like this:

/**
 * PaletteJarSketch (v1.1)
 * GoToLoop (2018/Jul/13)
 *
 * https://Discourse.Processing.org/t/
 * including-shared-pde-files-across-projects/1701/3
 */

import malcolm.Palette;
import static malcolm.Palette.*;

background(BLUE);

strokeWeight(3.5);
stroke(RED);
fill(YELLOW);

ellipse(width>>1, height>>1, width>>1, height>>1);

See there’s an import statement in the “.pde” file:
import static malcolm.Palette.*;

It expects a library which belongs to package “malcolm”, and a class or an interface called Palette.

Now, create a new tab named exactly as “Palette.java”.
Then copy & paste this file below there:

package malcolm;

public interface Palette {
  int
    WHITE = -1, 
    BLACK = 0xff000000, 
    RED = 0xffFF0000, 
    GREEN = 0xff008000, 
    BLUE = 0xff0000FF, 
    YELLOW = 0xffFFFF00, 
    MAGENTA = 0xffFF00FF,
    CYAN = 0xff00FFFF;
}

The final product is to turn that “.java” file into a “.jar” 1.

You’re gonna need to search on the Internet on how to compile it w/ javac, and then package it w/ jar.

Good luck! :wink:


#4

Thanks so much.

I may have actually seen the link you provided but had not thought it was the answer I wanted. I shall just have to learn to make a jar, I think I will call it vegemite, salty, evidently hard to get used to for some, yet a staple in my daily diet.

Cheers


#5

The proper way to do this is using a library, as GoToLoop described.

If you want the same pde code included in every new pde file, you should consider using the template folder in Processing. If you visit your sketchbook folder, you find the libraries folder and also a templates folder. Inside this folder, create a folder called Java and add a file named sketch.pde. Every time you create a new file in the PDE, this file is used as the template. This might be useful to you or it might not.

Kf


#6

Funny thing I use the PDE’s template feature for both Java & Python modes; and I’ve completely forgot that it could also be applied to what @malcolm_gc is asking here! :upside_down_face:


#7

Actually “libraries/” & “templates/” subfolders inside our sketchbook folder. :wink:


#8

Thanks to the responders ! ! !

kfrajer suggests using the template folder and sketch.pde , useful info, but not exactly what I am after I created the template/java/sketch/pde and yes when in Processing you select File New you get a new file with the content of sketch.pde. Problem is if I change sketch.pde that will not ripple down to other older projects.

It seems to me that the suggestion by GoToLoop is what I am after a library that I can call from any project. If I can get ever get it working

I followed GoToLoop instructions to the best of my ability and created project/malcolm/malcolm.pde and project/malcolm/Palette.java from a cmd shell I then compiled it

Project/malcolm/malcolm.pde

import static malcolm.Palette.*;

background(BLUE);

strokeWeight(3.5);
stroke(RED);
fill(YELLOW);

ellipse(width>>1, height>>1, width>>1, height>>1);

Project/malcolm/Pallete.java

package malcolm;

public interface Palette {
  int
	WHITE = -1, 
	BLACK = 0xff000000, 
	RED = 0xffFF0000, 
	GREEN = 0xff008000, 
	BLUE = 0xff0000FF, 
	YELLOW = 0xffFFFF00, 
	MAGENTA = 0xffFF00FF;
}

myjavapath/javac Palette.java – which gave me a Palette.class

manifest.txt
Main-Class: malcolm

myjavapath/jar cfm malcolm.jar manifest.txt Palette.class – which created malcolm.jar

I got the information about the manifest.txt from youtube which told me how to run javac and jar If I leave the f option out to not use a manifest I get compile errors

I was not sure whether Main-Class: should be Malcolm or Palette tried both later errors are the same regardless

so anyway I now have a malcolm.jar

jar" tf ./malcolm.jar lists the contents of the file
META-INF/
META-INF/MANIFEST.MF
Palette.class

so anyway stuck the malcolm.jar into Processing\libraries and run Processing project\malcolm\malcolm.pde

i get a box with a circle - which is what malcolm.pde is supposed to do BUT

Processing says No library found for malcolm.Palette

Some more reading I came across https://www.tutorialspoint.com/java/java_packages.htm it suggests that $CLASSPATH might be an issue , i didnt have a CLASSPATH so I created one

C:\Users\mnmmc\Documents\Processing\libraries>SET $CLASSPATH
$CLASSPATH = C:\Users\mnmmc\Documents\Processing\libraries

Unfortunately that didnt fix it - I guess that may not be needed in Processing

The preferences in Processing indicate a path to my Project directory and my library malcolm.jar is in the Project/libraries directory

So I can create jar file but I am still missing something, the naming is wrong or there is some path I haven’t set or I am just not holding my tounge between my teeth correctly.


#9

I’ve revisited what I did to create a “.class” file and package it as a “.jar” file. Here comes: :grimacing:

  • Create a folder for the project: “Palette/”
  • Given file “Palette.java” belongs to package malcolm;, let’s create a subfolder w/ that same name for it: “malcolm/”
  • Place file “Palette.java” inside subfolder “malcolm/”.
  • Open the shell in that subfolder and type in: javac *.java
  • It should create a “Palette.class” file.
  • Close shell, and go back to the root folder.
  • Reopen the shell in the root folder.
  • Now type in: jar cf Palette.jar *
  • Use 7-zip or some other compress utility to check the content of the “Palette.jar” file.
  • We should see 2 folders there: “malcolm/” & “META-INF/”.
  • Inside “malcolm/”, we should see: “Palette.class” & “Palette.java”
  • And inside “META-INF/”, we should see: “MANIFEST.MF”
  • For a quick test, drag in the “Palette.jar” file into the PDE w/ the test sketch I’ve posted.
  • The “Palette.jar” should automatically go into the sketch’s subfolder “code/”.
  • Run the sketch. It should see & use the “.jar” file via import static malcolm.Palette.*;
  • Now that the test run had worked, you should follow the wiki in order to make the “Palette.jar” file available for all sketches as a 3rd-party library: :innocent:
    https://GitHub.com/processing/processing/wiki/How-to-Install-a-Contributed-Library

#10

wow and on the seventh day !

All this for RED WHITE and BLUE !!!

I do appreciate very much the response and will give it a go tomorrow .

Obrigado


#11

@GoToLoop It is not clear what is the relationship between malcolm and Pallete folders. Can you clarify in your post plz? Would it follow malcolm.Palette.* scheme? The package name in the java file should be package malcolm.Palette; ?

Kf


#12

This is what I’ve stated: :man_teacher:

Given file “Palette.java” belongs to package malcolm;, let’s create a subfolder w/ that same name for it: “malcolm/”

Keyword package determines the folder a particular “.java” (and its corresponding “.class”) file belongs to. :nerd_face:

At least that’s what I’ve concluded when I tried to create a “.jar” file for Processing the 1s time.
I still can be wrong about it! :roll_eyes:

This is the “.java” file in my 1st attempt: :grinning:

Notice its package statement: package deadpixel.command;

The 1st “.jar” file I’ve generated outta it didn’t work though. :cry:

Only when I’ve placed the compiled “Command.class” file into the folder path: “deadpixel/command/”, matching the “Command.java”'s package deadpixel.command;, the “.jar” output file was accepted by the PDE inside the “code/” subfolder. :open_mouth:

Here’s the “.jar” file btW: :money_mouth_face:

Maybe it was just a superstition I’ve got from my 1st experience on it. :grimacing:
It’d be nice if some1 else would confirm that w/ more tests. :crazy_face:


#13

After my 1st explanation, every1 can see now the whole setup depends solely on the package statement, not the rest of the file. :face_with_monocle:

The only thing the interface Palette adds to the “demands” is that b/c it is public, the “.java” filename must match the name of whatever top interface or class was declared as public. :flushed:

Therefore only 1 top, either a class or an interface, can be declared as public inside a “.java” file AFAIK. :no_mouth:

They might have changed those limitations on recent Java versions. Not sure. :dizzy_face:


#14

Ok I have to change the name of the classes for this demo, but this is what I have:

C:\Users\C\Documents\Processing\CMsketches\customLibrary\customLibrary>jar tf malcolm2.jar
META-INF/
META-INF/MANIFEST.MF
malcolm2/
malcolm2/Gener.class
malcolm2/Gener.java
malcolm2/GenerUp/
malcolm2/GenerUp/GenerUp.class
malcolm2/GenerUp/GenerUp.java
malcolm2/GenerUp2/
malcolm2/GenerUp2/GenerUp2.class
malcolm2/GenerUp2/GenerUp2.java
malcolm2/Pallete/
malcolm2/Pallete/Pallete.class
malcolm2/Pallete/Pallete.java

Each class generated individually at their own root folder. Notice for this test you have to stick public everywhere. Now this is what I have in each corresponding file:

  • Pallete.java: package malcolm;
  • GenerUp.java: package malcolm;
  • GenerUp2.java: package malcolm.GenerUp2.*;

To build the jar : jar cf malcolm.jar malcolm. Placed the jar in the code folder. Now the mcve:

import malcolm2.*;
//import malcolm2.GenerUp.*;   <----Not valis as it is not defined anywhere... expected
import malcolm2.GenerUp2.*;


import malcolm2.*;
import malcolm2.GenerUp2.*;

void setup() {
  Gener x=new Gener();
  println(x.val1000);                      //<----WORKS

  ////GenerUp y=new GenerUp();   <----Unaccessible/Non-existent
  ////println(y.val3000);

  GenerUp2 z=new GenerUp2();
  println(z.val5000);                      //<----WORKS
}

Notice each class has two fields defined, unique to each class. However, I tried testing your interface implementation and it didn’t work.

Kf


#15

Ok so there are some interesting things. if you load your jar file inside your sketch aka inside the code folder, then you do not need to state the import statement. You will be able to use it at your leisure. Notice, as dealing with any interface, one needs to use Pallete.RED as RED will not work.

Now, if you move this library to the Processing’s libraries folder like this:

yourSketchFolder/Libraries/malcolm/Library/malcolm.jar

Notice: I am in Win10 OS. Folder names is case sensitive in other OSes.

Then you need to include at the top of your sketch import malcolm.*; OR import malcolm.Pallete;, the last one is preferred. Notice you don’t need the static modifier for the import.

Notice this layout: yurSketchFolder/malcolm/Pallete.java and the first line in your java: package malcolm;

Another scenario:
Notice this layout: yurSketchFolder/malcolm/Pallete/Pallete.java and the first line in your java should be: package malcolm.Pallete;

When you create the jar via “jar cf malcolm.jar malcolm”, the last term indicates the folder to attach to the jar. This will also attach any other subfolders and their contents. So this jar cmd would work for either scenarios above. Notice the last term is the folder name and it does not have any slash attach to it (backlash for Windows users).

Kf


#16

Due to many conflicting instructions so far, I’ve felt I needed to create a repo just to demo more clearly my own step-by-step howto that I’ve already posted here in this forum thread: :cowboy_hat_face:

There are 2 main folders in that repo: :face_with_monocle:

  1. “PaletteJarCreationFolder/”: where the whole processing of creating the “.jar” file takes place.
  2. “PaletteJarSketch/”: The “.pde” test sketch which uses the “.jar” file created at “PaletteJarCreationFolder/” placed in its subfolder “code/”.

Within “PaletteJarCreationFolder/” we can spot the 2 “.bat” files “Part1.CreateClassFile.bat” & “Part2.CreateJarFile.bat” which contain the command lines I type in to create “Palette.class” & “Palette.jar” respectively: :male_detective:

  1. “Part1.CreateClassFile.bat”: javac *.java
  2. “Part2.CreateJarFile.bat”: jar cf Palette.jar *

Hope it’s now very clear on how I did it and you can mime it if you wish. :innocent:


#17

Again thank you both for your time.

I spent some quite a bit of time trying to make this work trying this that without much success. Most recently I saw GoToLoop had provide some new files , thank you for that.

kfajer indictes a number of different places and options for the library
tried putting the jar in libraries/Malcolm/library (libraries)
tried putting the jar in Processing/libraries
tried putting the jar in Processing/libraries/Palette/library
tried putting the jar in the project/Palette.JarSketch

I can create the java file with the files provided by GoToLoop run the batch files and create the class and jar files but cannot get the program to link (is that the correct term???) to the jar.

If the Palette.java file is placed in the same directory as the pde the program runs and creates output.

This is all getting very wordy and I was finding it very confusing so I have created a pdf of where we are at if you would kindly have a look, the diagram attempts to show that the PaletteJarSketch.pde runs and produces output when Palette.java is in the same directory but when Palette.java is not available Processing is unable to find the jar file.

15 Jul modified jpg


#18

It seemed to me that the jar file should be named malcolm as the pde imports malcolm.Palette, had this vague idea that the jar should have the same name as the package
so I changed the second bat to create malcolm.jar , that worked ok , well it didn’t complain, stuck it in libraries/malcolm/library and restarted Processing, crossed my fingers and groaned, didn’t work ! ! This is so painful!!


#19

For your library folder, then do in a cmd window tree /a /f and post here. Also, post the package line that you are using and the jar command to pack your library. This is what I did in my prev post. I am asking only for the library. Do not include anything relate to your pde that you are using to import this lib of yours.

Kf


#20

Not according to the article for installing libraries in Processing: :no_entry:

The top folder of a library must have the same name as the .jar file located inside a library’s library folder (minus the .jar extension):

Therefore, we’re free to choose whatever name to the “.jar” file, as long as we use that same name for its top folder. :face_with_monocle:

My “.jar” file was named as: “Palette.jar”. So in order to integrate it to the PDE, I’ve gotta name its top folder inside sketchbook’s “libraries/” folder as “Palette/”: :file_folder:

 +---libraries
|   \---Palette
|       \---library
|               Palette.jar

However, after I’ve completed the “.jar” integration into the PDE, when running the “PaletteJarSketch.pde” w/o the “Palette.jar” inside its subfolder “code/”, the statement import static malcolm.Palette.*; didn’t work! :face_with_head_bandage:

I had to use a regular import before being able to use the import static: :crazy_face:

import malcolm.Palette;
import static malcolm.Palette.*;

I’ve updated my “Processing_Jar_Creation” repo to include this extra import statement already. :smile_cat:

And even included the PDE’s “.jar” integration there too: :sunglasses:


Using Qscript in java