Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187.1K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 443 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Problems with exporting to jar with images in eclipse - getting instability

I made a cheesy pong game with images that pop up and other distractions. I accessed my images as so:
zappa = new ImageIcon("/images/zappa2.jpg");
(yes, a pony-tailed pic of frank zappa pops up to block part of the screen and then starts moving around as further distraction)
I tried to export as a jar and a runnable jar, but either way, the program worked fine but no pictures showed up in my game.
I read a bunch of stackoverflow forums and found something that semi-worked. I changed the path to:
zappa = new ImageIcon(getClass().getResource("/images/zappa2.jpg"));
When I exported to a jar, the pictures showed in the game! However, whenever I comment out my old path and use the new getResource path, the game becomes very unstable. It only loads a rare number of times out of a set of attempts. In both eclipse and the exported jar, most of the times I try to load the program, I just get an empty frame, and on some occasions, I get game play, but my paddle is unresponsive to the mouse.
I want the stability of my first path, but the pictures exporting correctly of my second path.
Suggestions?
Also, what do I want: jar or runnable jar? This is my first GUI program. Previously, I've made text programs, exported to jar, and ran them thru terminal.
Thank you, core
Answers
-
If you want you program to run, then you want a runnable JAR file.
I cannot really comment on your problems with the animation without seeing your code.
IMO: you have chosen the worst way to load your images--using ImageIcons, painting BufferedImages, that have been loaded with ImageIO, onto the Graphics contest of a JPanel is a much better way to go.
-
Thanks, but none of the code I've searched since reading this is working. Could you provide some sample code for if I made a source folder called resources and inside of that I made a regular folder called images, and my jpg is inside the images folder?
Does that sound right or am I getting a little folder crazy?
You response sounds like the most hopeful advice I've gotten so far, so I'd really like to make this work.
-
currently I'm using this line after those imageicon commands:
zappa.paintIcon(this, g, 250, 250);
I'm assuming that the command for displaying a buffered image would be different?
-
actually, I'd like to change those last 2 questions. I found some code online that's working for me.
I made an images folder in the project and copied my image in and this worked:
String path = "images/zappa2.jpg";
However, this doesn't export correctly (works fine in eclipse). I assume that's because I need to export with the image inside of source folder. How would I change that file path to get the same picture that's inside of a source folder called "resource", which inside has a regular folder called images? I've tried a bunch of varieties of "/src/resources/images/zappa2.jpg", but can't find the winning combination of folder names.
-
actually, I'd like to change those last 2 questions. I found some code online that's working for me. I made an images folder in the project and copied my image in and this worked:String path = "images/zappa2.jpg";However, this doesn't export correctly (works fine in eclipse). I assume that's because I need to export with the image inside of source folder. How would I change that file path to get the same picture that's inside of a source folder called "resource", which inside has a regular folder called images? I've tried a bunch of varieties of "/src/resources/images/zappa2.jpg", but can't find the winning combination of folder names.
You are REALLY doing yourself a disservice by not reading the documentation and using an official site for getting your information and sample code.
The Java Tutorials has trails that cover ALL of the basic functionality and includes examples with WORKING code.
For example a SIMPLE web search for 'the java tutorialss icons' returns the page for the trail about how to use icons in Java.
https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html
Generally, applications provide their own set of images used as part of the application, as is the case with the images used by many of our demos. You should use the
Class
getResource
method to obtain the path to the image. This allows the application to verify that the image is available and to provide sensible error handling if it is not. When the image is not part of the application,getResource
should not be used and theImageIcon
constructor is used directly.See how that paragraph tells you EXACTLY how to 'obtain the path to the image'?
Try the example code in the tutorial. Then try applying that method to your own code.
-
Thanks for the link, but I've read this one before. I was using ImageIcon and it was working inside of Eclipse, but the images wouldn't export. The first reply to my question had me change tracks towards bailing on ImageIcon in place of using BufferedImages.
Are you suggesting I return to an ImageIcon approach?
-
Thanks for the link, but I've read this one before.
But it doesn't sound like you did what I suggested. Did you?
Try the example code in the tutorial. Then try applying that method to your own code.
Reread that code example and look closely at HOW it works and the classes and methods it is using.
Are you suggesting I return to an ImageIcon approach?
Again - READ the code example - I provided an excerpt from it. Did you see this? Did you try it?
You should use the
Class
getResource
method to obtain the path to the image.EVERYTHING in a jar file (classes, resources, files) is accessed RELATIVELY TO THE CLASSPATH of the jar file.
That is what the 'getResource' method is used for.
THERE ARE NO SHORTCUTS!
For whatever reason many people, perhaps you too, just don't want to read the documentation, tutorials or the Java API to try to LEARN about functionality. They just start writing code and then can't understand why it doesn't work the way they think it should.
A typical comment is 'I tried that example after making a few simple changes'. Well - you need to try WORKING code without making ANY changes. Then you have a baseline. Then make your 'simple changes' and when it doesn't work you know for certain it was due to one of your changes.
Here is the doc link for the 'getResource' method.
https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)
It explains how the class loader is used to load classes/resources based on the classpath that has been established.
If you can't get the resources into the jar file (export you say) in Eclipse then that is an Eclipse problem. Netbeans does it just fine.
I suggest you try to adopt the methods the experts use:
1. read the documentation for the functionality you want to learn about
2. find a tutorial/book that has WORKING code and actually try the examples on your own machine and environment
3. stay on step #2 until the examples work for you. If they don't work find out why and fix it
4. save/archive the ENTIRE PROJECT for the working examples so you can always go back to a KNOWN baseline if things go wrong later
5. make SMALL changes to the example to EVENTUALLY reach the code you need for your use case
-
I'll read back through that article and give it another shot. Honestly, I'm teaching AP Computer Science in a high school for the first time. I learned through codeHS last year and took a few AP seminars in the summer. However, once the school year started, things got hectic and I've only been able to grab from piles of resources trying to put fires out. This course is making me hustle so hard that it leave little time to expand any of the math courses I teach.
From the chaos I'm existing in, I appreciate you stressing a return to official documentation. My resource research has devolved into band-aids.
-
morgalr,
I switched to BufferedImage, figured out the path issue, and am now rocking! Thank you very much for the guidance!