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.
Related
I am a beginner and I am trying to learn by messing around with some open source game code.
I was setting it all up in Eclipse but I don't know where to put these sprite gif files.
In the code I found this:
URL url = this.getClass().getClassLoader().getResource(ref);
And when I put all the class files under a java project together and tried to run I got this error message:
Can't find ref: sprites/ship.gif
Of course the code came with sprites including ship.gif. I just don't know where to put it. I tried making a folder under the java project called sprites and putting it in there.
I don't have a res folder.
The this.getClass().getClassLoader().getResource method look for relative path of the data from the package of the class.
Assuming your class is com.my.package.MyClass, you usually have your project organized containing at least in your case:
com/my/package/MyClass.java
com/my/package/sprites/ship.gif
Most of the time, you certainly have a resources or a images folder for your java project.
You can only load those resources if they are on the classpath. Try to add the sprites folder as a source folder on your Eclipse build path and try again.
Put the .gif in the jar.
This can be achieved by creating dedicated folder you add in the sources of eclipse. Often this folder is called "resources".
Be ware that in some cases a "/" is required at the begining of ref.
Thanks everyone.
How I fixed my problem:
When I downloaded the source I opened folders and went to the class files and copied them to my project and then I was trying to do the same thing with the sprites. What I didn't realize is that if I just drop the two main folders "org" and "sprites" into the source file of my project then it all works on its own. These two folders were the first things I had after downloading this open source code.
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 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.
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.