67544

Images are not displaying in jar but displaying in compiler?

Question:

I know, I know, this has been asked before. But every resource I've looked at uses IconImages while I just have plain Images.

Is there a different solution? Please help as I've been stuck researching and trying to figure this out for days now with no progress.

Image Floor = Toolkit.getDefaultToolkit().getImage("Floor.PNG");

EDIT: If I was to make sure the jar wouldn't compress and I created a seperate directory in the jar for images and put the correct file path, would this code work?

Answer1:

Toolkit#getImage(String s) looks for a file and likely your image is in the Jar and is now a resource not a file. Look to using resources for this.

Note that ImageIO.read(...) can accept an InputStream parameter the Class class has a method, getResourceAsStream(...) which can put the resource into a Stream that ImageIO can read. Give that a try.

Also, are you getting any error messages when you try what you're doing?

Answer2:

Make sure you know what your current directory is, and how it relates to the position of the files in your jar.

Here's how I would handle it.

1) Require there to be a file called "images.txt" in the directory with your jar (or bundle it into the jar.) 2) Make a file called "images.txt" with a format like `FLOOR:C:\\images\\floor.png` 3) Load this file into memory on load. 4) Load your images based on the entries in the file

This will give you the advantage of changing your images without changing your code if it's defined outside the jar :)

It's not loading because you're not putting the path to the images in the declaration. It expects the images to be wherever the jar is (notice there's no directories there)

You need to offload the definition of the file names to a file, or at the very least guarantee the relative position of the files.

Another good option is to put the images in the jar itself, say in an img directory, and reference them there. But then changes to the images require a new jar, which may not be desired for development purposes.

Answer3:

The getImage call is looking in the file system working directory, not inside the Jar file. This is why the jar file loads the images successfully when they are placed in the same directory outside the jar file. If the images are bundled in the jar file, they are no longer file system files to be accessed, but rather Jar file resources. There is a different way to load these, but sorry, I don't know it off the top of my head.

Answer4:

Check the extension of files. I had this problem because the extension was "PNG", when I changed it to "png", everything was ok.

Answer5:

You can't expect a JAR file to magically know where your images are. If you put a JAR file alone on the desktop, it's going to look for the files on the desktop! The code

getImage("Floor.png")

searches the current directory of the JAR (or source project) by default and you'd expect that if the JAR was in the same directory, it would work. If the JAR is on the desktop how does it know where Floor.png is? Of course, you can specify a hard-coded path

getImage("C:\Some Folder Path\Floor.png")

but then Floor.png has to be in C:\Some Folder Path\ for the JAR to work properly.

It sounds like what you really want to do is keep the images in the JAR file (which acts like a ZIP file). The tutorial on doing that is here:

<a href="http://download.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource" rel="nofollow">http://download.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource</a>

Answer6:

<blockquote>

And I know for ImageIcon you use: new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg") but I have not found anything similar for plain Image.

</blockquote>

<head-desk /> You should really get into reading the JavaDocs. Otherwise you are 'coding by magic'. Which generally won't work.

URL urlToImage = getClass().getResource("myimage.jpeg"); // If you need to support Java 1.3 Image image = Toolkit.getDefaultToolKit().getImage(urlToImage); // If your users have dragged their JRE into this millennium BufferedImage bufferedImage = ImageIO.read(urlToImage);

Recommend

  • Does Xcode 6 Simulator use x86_64?
  • Oracle SQL - Generate and update a column with different random number in every row
  • How to save dynamically created textboxes and their values
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • Shouldn't else be indented in the below code
  • How to get listview position?
  • pip in virtualenv gets ConnectTimeoutError
  • android google indoor map
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • rspec simple example getting error on request variable in integration test
  • Connect .sks to skscene.h
  • Installed module is empty
  • Blackberry - Custom EditField Cursor
  • How to test if a URL from an Eclipse bundle is a directory?
  • How to attach a node.js readable stream to a Sendgrid email?
  • Django rest serializer Breaks when data exists
  • Cancel a live stream “fast motion” catch-up in Flash
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Body moving without any force applied? (Box2d)
  • How to rebase a series of branches?
  • Azure Cloud Service Web Role web pages do not load
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Date difference with leap year
  • what is the difference between the asp.net mvc application and asp.net web application
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Why winpcap requires both .lib and .dll to run?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to set the response of a form post action to a iframe source?
  • Android Google Maps API OnLocationChanged only called once
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Busy indicator not showing up in wpf window [duplicate]
  • Converting MP3 duration time
  • Python/Django TangoWithDjango Models and Databases