Eclipse -> Export to JAR | Images don't load?

Hello everyone! o/ I hope I'm in the right category. Well, I have a problem with my exported JAR file. I load some Images with PApplet.loadImage(). In eclipse it works without problems. But if I try it to export them into a JAR file, I got some exceptions.

My project structure:

Well, I load the Images from my Background.java with PApplet.loadImage("img/galaxy.gif") for example. It works in eclipse, but if I try it to export them to a JAR file and run it after, it gives me some exceptions:

java.lang.NullPointerException at processing.core.PApplet.dataFile(PApplet.java:8016) at processing.core.PApplet.dataPath(PApplet.java:7993) at processing.core.PApplet.createInputRaw(PApplet.java:7093) at processing.core.PApplet.createInput(PApplet.java:7011) at processing.core.PApplet.loadBytes(PApplet.java:7301) at processing.core.PApplet.loadImage(PApplet.java:5439) at processing.core.PApplet.loadImage(PApplet.java:5363) at make.object.Background.setup(Background.java:47) at make.Applet.setup(Applet.java:16) at processing.core.PApplet.handleDraw(PApplet.java:2412) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Well, this line here: at make.object.Background.setup(Background.java:47)

It tells me it can't find any images. I had check my JAR file and the structure looks okey. I have my img folder here, my make folder (with the other stuff inside) and the imported core library. But why it doesn't load my images now?

Sorry for my bad english. I hope you understand my problem. ^^

Answers

  • Well, I find a solution for everyone who have this problem too. I added a static method to one of my classes:

    public static PImage getImage(String url) throws Exception { BufferedImage image = ImageIO.read(Applet.class.getResourceAsStream(url)); return new PImage(image); }

    I use the class loader instead PApplet.loadImage() - For anyone who want to use this variant > add a "/" to the front of your path. In my case: "/img/galaxy.gif".

    But the question is the same: Why loadImage() don't work here? Or how I need to change it, that it works?

  • edited December 2017

    I'd give you the same tip I just did below today:
    https://Forum.Processing.org/two/discussion/25490/where-to-put-a-jpg-using-bluej#Item_2

    PApplet.println(applet.dataPath("") + '/'));

  • edited December 2017

    Well, that don't work really well. It forces me to some special prject structure. First of all I need to import the processing library completly in my project as copy (I just added a reference to them in my file system). The second thing is, that I need to create a data Folder outside of my project (the src folder). But if I export the project as JAR, eclipse don't create me a data folder because it's not in my src folder. Instead I got my images in the root archiv (or the jar archiv itself):

    Well, a other problem is that eclipse can just find the images, if I use this kind of a file path: PApplet.dataPath("") + "/src/galaxy.gif")

    but in the exported JAR file isn't a src folder anymore. The question is, how I deal with it? The documentation from Processing tells me not to use new PImage and not to use dataPatch or sketchPath. And loadImage isn't obvious working. What is now the right solution?

  • edited December 2017 Answer ✓

    Well, I had some troubles with the alpha channel on my first solution. The *.gif images showed me a white background for the transparent. I changed something for my method:

    public static PImage getImage(String url) throws IOException {
            BufferedImage bi = ImageIO.read(Applet.class.getResourceAsStream(url));
            PGraphics g = applet.createGraphics(bi.getWidth(), bi.getHeight());
    
            g.beginDraw();
            Graphics2D g2d = (Graphics2D) g.getNative();
            g2d.drawImage(bi, 0, 0, bi.getWidth(), bi.getHeight(), null);
            g.endDraw();
    
            return g.copy();
        }
    

    Well, this works fine for me. I had look up in the source code from precessing and figured out that they don't load the images over the classpath. Well, I hope this help someone. Ah, before I forget it, it don't work in the FX2D mode. So be carefull.

  • edited February 2018

    Hey! I have this problem similar to yours. Would like to ask if you can explain a bit more about a static method. How I should use it in exactly?

    public static PImage getImage(String url) throws Exception{
        BufferedImage image = ImageIO.read(Applet.class.getResourceAsStream("assets/vase.jpg"));
        return new PImage(image);
    }
    public void setup(){
    
        img = loadImage(("assets/vase.jpg"));
        img.resize(400, 0);
        // Create a new empty image;
        resultImg = createImage(img.width, img.height, PConstants.RGB);
        convolution(embossKernel);
    }
    

    I tried to called in around this code but did not work!?

    Right now, the picture is called fine in Eclipse environment but with export to jar it could not find the image. So I'm trying to write the correct path to the file for jar.

Sign In or Register to comment.