Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Java Images via JAR

    I've developed a simple application that I'd like to distribute to some friends. When I export to a JAR, it no longer supports the images. This is the code I'm using to display the images:

    Java Code:
    bgImage = new JLabel(new ImageIcon(getClass().getResource("/images/bg.png"),""));
    bg.png is locate at .../src/images/bg.png

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    How are you doing the 'export to a JAR'?

  3. #3
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    "Package required libraries into generated JAR"

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    Quote Originally Posted by JayF View Post
    "Package required libraries into generated JAR"
    That makes no sense...are you using command line? IDE (which one and how are you exporting)? Ant?

  5. #5
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    Quote Originally Posted by doWhile View Post
    That makes no sense...are you using command line? IDE (which one and how are you exporting)? Ant?

    File-> Export -> Runnable JAR -> Library Handling (Package required libraries into generated JAR)

    I'm using Eclipse
    Last edited by JayF; 04-05-2012 at 11:11 PM.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    That did not answer my question at all, and I do not like guessing

  7. #7
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    Quote Originally Posted by doWhile View Post
    That did not answer my question at all, and I do not like guessing

    Eclipse IDE, Indigo

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    The IDE should export the images into the jar. Break the problem down into basic troubleshooting parts....unjar and make sure they are there. If they are not, you know its the IDE. If they are, your lookup path is most likely incorrect.

  9. #9
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    Quote Originally Posted by doWhile View Post
    The IDE should export the images into the jar. Break the problem down into basic troubleshooting parts....unjar and make sure they are there. If they are not, you know its the IDE. If they are, your lookup path is most likely incorrect.
    I've opened the JAR and the images are inside. How would I determine the correct path?

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    Again, break the problem down...now you know the images are there, its not the jar export that is the problem. So...how do you define "it no longer supports the images"? Is there an exception thrown? If so, what is it? Are the images in the correct location within the jar? Does this location correspond to the path you define when you get the resource? Debugging this like this should be like a decision tree - ask a question then decide which branch to take based upon the answer to that question.

  11. #11
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    An exception is thrown because the path given to the ImageIcon is invalid. The question I'm asking you is how I correctly determine that path. getClass().getResource() simply gives me "rsrc:" which I don't understand.

  12. #12
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    What is the stack trace? If you pass /images/bg.png to the getResource(), then that should be the location within your jar. Do they coincide when you unpackage your jar?

  13. #13
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    I can't see the stack trace because the exception is being thrown in the JAR only, not in the IDE. The image path is JAR/images/bg.png

  14. #14
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    Quote Originally Posted by JayF View Post
    I can't see the stack trace because the exception is being thrown in the JAR only, not in the IDE. The image path is JAR/images/bg.png
    Run the jar from the command line (java -jar myjar.jar) ...it will print out the stack trace. If the path/files name of the image within the jar is identical to what you pass to getResource(), then the image should be found. My guess is that they are not, but the stack trace would confirm either way.

  15. #15
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    Okay, here's the stack trace.

    Jays-Mac:~ Jay$ java board.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: board/jar
    Caused by: java.lang.ClassNotFoundException: board.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)

  16. #16
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    That is not the correct stack trace...run as I described above - with the -jar command line argument.

  17. #17
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    Quote Originally Posted by doWhile View Post
    That is not the correct stack trace...run as I described above - with the -jar command line argument.
    Jays-Mac:~ Jay$ java -jar Board.jar
    Unable to access jarfile Board.jar

  18. #18
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    board.jar != Board.jar Post 15 uses board.jar, post 17 uses Board.jar

  19. #19
    JayF is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Java Images via JAR

    The jar's name is Board.jar
    Jays-Mac:~ Jay$ java -jar desktop/Board.jar
    Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoa der.main(JarRsrcLoader.java:58)
    Caused by: java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
    at code.Team.<init>(Team.java:16)
    at teamSelectorCode.teamSelectionWindow.addIcons(team SelectionWindow.java:66)
    at teamSelectorCode.teamSelectionWindow.<init>(teamSe lectionWindow.java:54)
    at code.Driver.main(Driver.java:6)
    ... 5 more

  20. #20
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Java Images via JAR

    And...look at the stack trace. It tells you exactly where the exception is being thrown. What is line 16 of Team.java? Is it the same line as your first post?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 12-16-2010, 06:14 PM
  2. Replies: 4
    Last Post: 12-16-2010, 05:32 PM
  3. Java Applets and Images
    By Danioson in forum Java Applets
    Replies: 7
    Last Post: 08-02-2010, 01:30 PM
  4. Replies: 7
    Last Post: 07-22-2008, 06:54 PM
  5. Help using images in Java
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •