I'm trying to read the pixel values of an image for a project. However, when I try to read the image in Eclipse, I get a Null.
This is the instruction I'm using:
BufferedImage image =ImageIO.read(MyClass.class.getResource("/Resources/bird_small.png"));
Resources is a source folder with the path: /Project/Resources.
The program is saved in the path: /Project/src/MyClass.java
The program works fine when I run it directly with Terminal (with some minor alterations).
I have seen some similar posts on this, however none of them seem to help me.
Resources should be in the src
ProjectRoot
src
Resources
bird_small.png
MyClass.java
When you build your project, Eclipse will copy the everything in the src into the class path.
Related
I have a fairly simple Java program going with the following structure:
/src/main/java/ (all the classes in here)
/src/main/resources (marked as resource root in IntelliJ, one png file in here)
/pom.xml
The way I build my Maven project is a bit of a workaround (because i couldn't get IntelliJ's artifact system to work). I have a run/debug configuration that builds the project using Command line: install (hope I'm explaining that sufficiently well).
Everything works fine, except for the inclusion of the .png file.
The weird thing is that the file is actually added to the executable jar, but somehow the final program doesn't appear to use it at all. It's accessed via:
BufferedImage image = ImageIO.read(new File(getClass().getResource(url).toURI()));
Again, the final program works perfectly fine, except that the image is not visible at any point. Any help?
Edit: The png is located in the root folder of the jar, and the supplied URL is a string which is simply the name of the png file. I should probably add that the program works fine in the IntelliJ-Run-environment (including the png), but not using the executable jar.
Found the answer that worked for me here:
Access .png image in .jar file and use it
Had to use
img = ImageIO.read(MapObject.class.getResource(url));
instead of
ImageIO.read(new File(getClass().getResource(url).toURI()));
since the image was located in a jar.
I'm trying to export my Java game as a Runnable Jar which works, but when I open it up I get an error that my files aren't present.
Picture of error:
Picture of res folder in eclipse:
The game runs perfectly fine in Eclipse.
I have added the res folder as a source folder.
Thank you!
The contents of a jar file are not java.io.Files. Understand that you don't actually care about the files, you want their contents, so start using getResourceAsStream()--it works for both cases pretty transparently.
I'm making a simple game in Java, and trying to load a sprite from the jar file. I'm using the following code to load a sprite:
spriteURL = getClass().getResource("/res/sprites/sprite_fr1.png");
And if I export a jar file and unzip it, the following folders exist:
/res/sprites/sprite_fr1.png
However, when I try and load the image, I catch a NullPointerException, even though, as far as I can tell, the path exists?
What am I doing wrong here?
EDIT: Screenshot below.
All sprites are in the folder 'sprites' as PNG files. I'm calling the method from jeu, not jeu/canvases.
Regards,
Ben.
Check classpath of your project in your IDE. For example in eclipse there is configuration for each folder (Properties->JAva Build Path->Source Tab). It may be set to exclude png files.
I would like to export my Java program, but I have run into problems. It exports fine but my resource files like my res folder and my sound folder aren't there. They are located in the project directory, so I don't know if that should be a problem. I am currently just using ImageIcon().getImage() to get my pictures and that works fine but when I export and run it the program runs but it doesn't have any pictures. Any help would be greatly appreciated
The problem is that once the images/sounds are inside the jar you must access them as a resource.
Image image = new ImageIcon(this.getClass().getResource("/image.gif")).getImage();
Make sure the images are really inside the jar opening the jar with winrar or similar if not add the folders as source folders in your buildpath.
I have been learning Java and have had no problem with projects in which multiple .java files were in the same Default Package.
I am now trying to separate code and create folders for images, but everything I have tried has failed.
How do I properly add folders for images and other classes, and properly set the path to it?
All paths should be relative to the project, I know that much ;_;
Here's a link to a picture of my IDE and error message from program output:
http://img262.imageshack.us/img262/8415/directory.png
Thanks!!
First off, your Java code itself should go into a package, not the default package. Next, you may be able to refer to the image file by prepending src/ to your path:
"src/Textures/Crate.png"
But better would be to get the image as a resource, not as a file using the Class#getResourceAsStream.
Drag using the mouse your Textures folder to the LWJGL 6 project folder.
Try giving src/Textures/Crate.png as the path to the FileInputStream constructor.