'How to export to a runnable jar with Proccessing (Eclipse)
I have tried to export my processing applet to a runnable jar file from eclipse (which I am using to code it) and it exports successfully but when opened just causes a blank (grey) screen. If I run it with command prompt I get this error:
java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "jarPath" is null
When I extracted the jar sample the folders and directories seem to be incorrect too.
before (the dependencies are in the dependencies folder)
after (the dependencies are outside of the now missing dependencies folder)
I'm sure its an issue with the file structuring on generation, more specifically the dependencies. when I run it as an application eclipse it runs perfectly fine with no exceptions.
Full message:
java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "jarPath" is null at processing.core.PApplet.dataFile(PApplet.java:7673) at processing.core.PApplet.dataPath(PApplet.java:7650) at processing.core.PApplet.createInputRaw(PApplet.java:6741) at processing.core.PApplet.createInput(PApplet.java:6659) at processing.core.PApplet.loadBytes(PApplet.java:6959) at processing.awt.ShimAWT.loadImage(ShimAWT.java:384) at processing.core.PSurfaceNone.loadImage(PSurfaceNone.java:61) at processing.core.PApplet.loadImage(PApplet.java:5311) at processing.core.PApplet.loadImage(PApplet.java:5296) at net.turke1034.shootergame.game.ShooterGame.draw(ShooterGame.java:55) at processing.core.PApplet.handleDraw(PApplet.java:2201) at processing.awt.PSurfaceAWT$10.callDraw(PSurfaceAWT.java:1422) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:354) java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "jarPath" is null at processing.core.PApplet.dataFile(PApplet.java:7673) at processing.core.PApplet.dataPath(PApplet.java:7650) at processing.core.PApplet.createInputRaw(PApplet.java:6741) at processing.core.PApplet.createInput(PApplet.java:6659) at processing.awt.ShimAWT.loadImageIO(ShimAWT.java:454) at processing.awt.ShimAWT.loadImage(ShimAWT.java:439) at processing.core.PSurfaceNone.loadImage(PSurfaceNone.java:61) at processing.core.PApplet.loadImage(PApplet.java:5311) at processing.core.PApplet.loadImage(PApplet.java:5296) at net.turke1034.shootergame.game.ShooterGame.draw(ShooterGame.java:55) at processing.core.PApplet.handleDraw(PApplet.java:2201) at processing.awt.PSurfaceAWT$10.callDraw(PSurfaceAWT.java:1422) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:354)
I have tried the same thing with a test project that requires no dependencies, and it runs as expected (when run in command prompt)
Solution 1:[1]
had the same issue, so troubleshooted for a while until i found the following solution. i'm assuming that you are using a data file similar to the data file in Processing projects to contain your dependencies.
in Eclipse, export as a runnable jar and select the option "Extract required libraries into generated JAR". once the jar is created, put this jar into a new folder. put your data folder into this folder as well. this worked for me.
no idea why this works - just threw stuff at the wall until this stuck. one troubleshooting technique i used was making a printwriter before loading any data so i could see where the computer was searching for the dependencies. the snippet below outputs the file to the same place that Processing looks for data.
PrintWriter pw = createWriter(dataPath("test.txt"));
pw.print("over here");
pw.close();
i used this in combination with dataPath("") to find that it was looking for dependencies outside of the jar.
Solution 2:[2]
In the past (older answer here) I had success exporting runnable .jar projects from eclipse which use Processing's libraries by using the Copy required libraries into a sub-folder next to the generated jar option in Runnable Jar File Export options:
This made it easier to debug java classpath issues (-cp
flag when running from command line) and native library paths(-D.java.library.path
command line flag).
If you're using java packages remember to specify them in PApplet.main()
:
public static void main(String[] args) {
PApplet.main(ShooterGame.class.getCannonicalName());
}
The above is useful only if you can't execute the jar files due to missing libraries (class not found, missing native libraries, etc.)
Regarding loading external assets, as Shivoum B mentions, you can make use of dataPath()
but also sketchPath().(See this similar answer).
Without seeing the path to the loadImage()
call you're making I can only make assumptions, but you might be able to get away with something like this in ShooterGame.java:
loadImage(sketchPath("data" + File.separator + "yourImageName.png");
(Off-topic, if I read this correctly you're trying to load images in draw()
?
I'd recommend loading all assets in setup()
once instead of multiple times in draw()
. A special case might be with the loading large assets that take time and using P2D
or P3D
where the OpenGL context setup might time out (in which you can load in draw()
but should use a "debouncing" boolean to ensure assets are loaded only once)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shivum B |
Solution 2 | George Profenza |