0

I'm using StdDraw jar file to show pictures (StdDraw.picture("path")) in my project, and want to export it to a runnable jar file. I use eclipse to do it.

I have added the folder of images in build path. I know there are some solutions to add images to jar file without errors but I can't find the right one for a java project using StdDraw. StdDraw.picture() requires a string of filename(path of image).

This is some code in my main class:

public static void main(String [] args) {
    StdDraw.setXscale(-50, WIN_WIDTH+50);
    StdDraw.setYscale(0, WIN_HEIGHT);
    do{
        StdDraw.clear(StdDraw.BLACK);
        StdDraw.setPenColor(StdDraw.WHITE);
        StdDraw.picture(WIN_WIDTH/2,  WIN_HEIGHT/2+80, "images/logo.png");
        StdDraw.text(WIN_WIDTH/2, WIN_HEIGHT/2-40, "Press SPACE to start !" );
        StdDraw.show(20);
    }while(!StdDraw.isKeyPressed(KeyEvent.VK_SPACE));
...

When I run created jar file, there are some errors.

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.IllegalArgumentException: image images/logo.png not found
    at edu.princeton.cs.introcs.StdDraw.getImage(StdDraw.java:1266)
    at edu.princeton.cs.introcs.StdDraw.picture(StdDraw.java:1331)
    at Pacman.Jeu.main(Jeu.java:290)
    ... 5 more
  • This has got to be some variant of `getResource()`: https://stackoverflow.com/questions/2593154/get-a-resource-using-getresource – Bob Kuhar May 23 '19 at 02:33
  • But I don't know how to make it work with StdDraw. StdDraw.picture() requires a string of filename, not a url. – Zhenqi Chai May 23 '19 at 03:20
  • I figured it out, just changed the path. I unzipped the jar file and found the right path. – Zhenqi Chai Jun 06 '19 at 02:47

0 Answers0