How do I compile a Processing file to a JAR file?

Lets just say i have this code:

void setup() {
size(960,540);
background(255);
fill(0);
text("this works",width/2,height/2);
}

How can i make this code into a standalone jar file, that can be executed to just work.
(i want to use this to put on a server, which only supports jar files)
I already tried compiling processing code to a jar file which didnt work.

I know how to create a java class file & run it but when i try to make processing into one file it doesnt work.

I wish processing would just directly compile in either one exe or just a jar… ;-;

Note that I’m new to this game. Does it have to be a jar file?

How?

I’ve just tested it in Processing 4.2 on a Windows system. I used file → export application and selected both Windows and Linux which resulted in the following (it does not result in a jar file but an executable).

Windows
image

Linux
image

Running the exe on Windows worked as expected. You can remove the source directory as far as I know.

i need a jar file because the server im using only accepts .jar files !

When you export a file in Processing it puts the compiled app inside a folder in your sketch folder. Inside the app folder should be a .java file of your project. Have you tried converting the .java file to a .jar file? The conversion process uses the command line and is found with a Google search.

it just gives me errors on anything processing related

Hi

here’s hints and tips may helps

problem is that both ways documented there dont work.

Hi @nuhuhwy,

would you please elaborate a bit more what you’re trying to achieve ?
If you export for linux your jar is already generated by the exporter beside the dependent libs.

ie:
Dummy example code (ExportExample.pde)…

void setup() {
	size(500,500);
}

void draw() {
  background(0);
  translate(width/2,height/2);
  rotate(radians(frameCount));
  stroke(255);
  line(0,0,width/4,0);
}

…after Exporting it …

$ find ExportExample/linux-amd64/lib -print
ExportExample/linux-amd64/lib/core.jar
ExportExample/linux-amd64/lib/ExportExample.jar        <-------- HERE ---------------
ExportExample/linux-amd64/lib/gluegen-rt.jar
ExportExample/linux-amd64/lib/jogl-all.jar
ExportExample/linux-amd64/lib/libgluegen_rt.so
ExportExample/linux-amd64/lib/libjogl_desktop.so
ExportExample/linux-amd64/lib/libjogl_mobile.so
ExportExample/linux-amd64/lib/libnativewindow_awt.so
ExportExample/linux-amd64/lib/libnativewindow_drm.so
ExportExample/linux-amd64/lib/libnativewindow_x11.so
ExportExample/linux-amd64/lib/libnewt_drm.so
ExportExample/linux-amd64/lib/libnewt_head.so

which contains your stuff:

$ jar -tf ExportExample/linux-amd64/lib/ExportExample.jar
META-INF/MANIFEST.MF
ExportExample.class              <---------------- HERE ------

look into it shows the used function on higher level.

$ javap ExportExample.class
Compiled from "ExportExample.java"
public class ExportExample extends processing.core.PApplet {
  public ExportExample();
  public void setup();             <---------------- HERE ------
  public void draw();              <---------------- HERE ------
  public void settings();
  public static void main(java.lang.String[]);
}

in more detail the draw function (for relevant calls):

  public void draw();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=5, locals=1, args_size=1
         2: invokevirtual #16                 // Method background:(I)V
        20: invokevirtual #27                 // Method translate:(FF)V
        25: getfield      #31                 // Field frameCount:I
        29: invokestatic  #34                 // Method radians:(F)F
        32: invokevirtual #38                 // Method rotate:(F)V
        39: invokevirtual #42                 // Method stroke:(I)V
        53: invokevirtual #45                 // Method line:(FFFF)V
        56: return

Cheers
— mnse

1 Like

What I am trying to achieve is having a processing sketch in a one file jar.

I followed both tutorials which someone sent me In this thread, but none of the worked.

When I try to open the jars, that come when exporting the code to Linux, I go to the lib folder and try running all jars at the same time - doesn’t work. Command line - doesn’t work, gives me errors on class could not be found.

In the file called what you named the sketch is bash code and it opens jogl-all.jar, gluegen.jar, core.jar & nameofthesketch.jar

When running this bash script it will open the sketch fine.

My question is, how do I convert everything in the “linuxamd64” folder to just one beautiful jar file that when klicked opens the windows, just like running the bash file in the command prompt.

It needs to be one jar file. That’s my question, I hope I explained it good enought :slight_smile:

Hi @nuhuhwy,

Ahh! Ok! Understood…
In principle that isn’t really a hard challenge, if you know how java jar’s work. :slight_smile:

Starting on the stage after export application already done
I made an example video to show …
ezgif.com-optimize (1)

Just keep in mind that you need a proper working X11 Environment.
For stuff like fancy opengl etc you need to set the *.so available to your java-lib directory.
But for simple cases the steps in the video should be sufficient …

Cheers
— mnse

2 Likes

Can i just copy the stuff in the build_fat_jar.sh file?

Hi @nuhuhwy ,

Sure! You can use every information I gave…

Cheers
— mnse

@mnse could you theoretically send it to me because i cant see exactly what is in the video!
Thank you!

#!/bin/bash

MAIN=${PWD##*/}}
echo "${PWD##*/}"

rm -rf tmp finished_build.jar 2>/dev/null

mkdir tmp

for file in $(ls -1 linux-amd64/lib/*.jar)
do
    echo "Currently extracting file... ( $file)"
    (cd tmp;jar xf ../$file)
done

jar -cfe finished_build.jar $MAIN -C tmp .

rm -rf tmp

java -jar finished_build.jar &

this is the code, i copied of your. If there is any error please tell me! Thank you for all your time thats wasted mabye because i forgot something :smiley:

I fixed it:!!
This is the corrected code!
This doesnt work on windows for some reason, you need to install java first!

#!/bin/bash

MAIN="${PWD##*/}"

rm -rf tmp finished_build.jar 2>/dev/null

mkdir tmp

for file in $(ls -1 linux-amd64/lib/*.jar)
do
    echo "Currently extracting file... ( $file)"
    (cd tmp;jar xf ../$file)
done

jar -cfe finished_build.jar "$MAIN" -C tmp .

rm -rf tmp

java -jar finished_build.jar &

Hi @nuhuhwy,

Your answer (which you’ve marked as solution :thinking:) is not correct !?

The bash script can be used on Linux but also on windows if you’ve bash installed by ie. WSL, cygwin, git-bash, etc.

That’s also not true as you’ve the required commands (java, jar) already rolled out by processing itself. But for sure you need to point to it, by either use an absolute path or set it in your PATH environment variable…

Also the question marks only needed if your sketch would have any fancy names with blanks or hyphens, etc.

Cheers
— mnse

PS: please consider to mark the post which pointing you to a possible solution as solution and not your “fix”.

So i tried compiling with the P2D renderer, and it doesnt work. Do you have a solution?

Hi @nuhuhwy,

As you not showing me any error message I can only guess you not having the necessary .so libraries accessible via LD_LIBRARY_PATH.

Cheers
— mnse

1 Like