Results 1 to 20 of 23
Thread: Java Images via JAR
- 04-05-2012, 09:43 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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:
bg.png is locate at .../src/images/bg.pngJava Code:bgImage = new JLabel(new ImageIcon(getClass().getResource("/images/bg.png"),""));
- 04-05-2012, 09:47 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Java Images via JAR
How are you doing the 'export to a JAR'?
- 04-05-2012, 09:51 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
Re: Java Images via JAR
"Package required libraries into generated JAR"
- 04-05-2012, 09:55 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
- 04-05-2012, 10:08 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
- 04-05-2012, 10:12 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Java Images via JAR
That did not answer my question at all, and I do not like guessing
- 04-05-2012, 10:16 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
- 04-05-2012, 10:19 PM #8
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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.
- 04-05-2012, 10:21 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
- 04-05-2012, 10:30 PM #10
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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.
- 04-05-2012, 10:34 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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.
- 04-05-2012, 10:38 PM #12
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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?
- 04-05-2012, 10:42 PM #13
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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
- 04-05-2012, 10:52 PM #14
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Java Images via JAR
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.
- 04-05-2012, 10:59 PM #15
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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)
- 04-05-2012, 11:01 PM #16
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Java Images via JAR
That is not the correct stack trace...run as I described above - with the -jar command line argument.
- 04-05-2012, 11:05 PM #17
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
- 04-05-2012, 11:08 PM #18
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: Java Images via JAR
board.jar != Board.jar Post 15 uses board.jar, post 17 uses Board.jar
- 04-05-2012, 11:11 PM #19
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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
- 04-05-2012, 11:16 PM #20
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Similar Threads
-
how to mark on 2 images at a time,both images are on different JPanel
By smitharavi in forum AWT / SwingReplies: 0Last Post: 12-16-2010, 05:14 PM -
how to scroll 2 images at a time(synchronisation),both images are on different panels
By smitharavi in forum AWT / SwingReplies: 4Last Post: 12-16-2010, 04:32 PM -
Java Applets and Images
By Danioson in forum Java AppletsReplies: 7Last Post: 08-02-2010, 12:30 PM -
hello Java World http://www.java-forums.org/images/smilies/smile.gif
By aniljoo in forum IntroductionsReplies: 7Last Post: 07-22-2008, 05:54 PM -
Help using images in Java
By toby in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks