I am working on a java project and I want to export it to a runnable jar file. This project has a GUI with several background images, and when I run it in eclipse with the images in my project folder it works, but when I try to export it to a jar, I get the following exception when running on cmd: javax.imageio.IIOException: can't read input file!
I have tried moving my images to various places in the project to attempt to include them in the jar, and even then it didn't work. I'm using File objects with commands like
final Image backgroundImage = javax.imageio.ImageIO.read(new File("background.png"));
How do I make it so that I can still use the images (preferably changing as little code as possible)?
Assuming that your image file, background.png, is in the same folder as your class file, replace new File("background.png") with this.getClass().getResource("background.png").
Related
Every time I export my project into *.jar in eclipse I lose all of images in my project (in Eclipse). I've tried to put images directly into the *.jar and it works in some other cases but not this one.
Fragment of code with loading of this icon:
primaryStage.getIcons().add(new Image("file:icon.png"));
You're loading the image from the working directory, not the jar itself. This is why it's working in development, and not in production. If you move the image to the same folder as the jar is located it should work, but I'm assuming you want the image to be contained inside your jar file.
Move the image to one of your packages (any, as long as it gets bundled with the jar). Then you need to access the image, use the ClassLoader.getResourceAsStream(String) for this. It looks like you're using javafx, so you can use this to get your image: primaryStage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("the/path/to/your/image.png"));.
to work with the files inside the jar, need to use ZipInputStream or JarInputStream
I have some images and xml files inside my project folders which I want to read. For example, my code is inside src/app/MyClass.java. The files I want to read are in res/filename.png, res/filename.xml.
So if I wanted to give my project an icon, I would do this (works fine if running through Eclipse):
primaryStage.getIcons().add(new Image("file:res/icon.png"));
The problem with this is that when I run it through the .jar file, the image does not appear. Same thing with other files. It can't find the xml file through the jar file.
I tried this:
InputStream input = getClass().getResourceAsStream("res/icon.png");
primaryStage.getIcons().add(new Image(input));
But Eclipse says that input is null. What am I doing wrong? Any help would appreciated!
There are some possibilities as to why this is the case. The first of which is you are running a JAR and not an Executable Jar File. Another possible reason why the program fails to display images is that when exporting you are not clicking the extract required libraries button as shown below. If neither of these are the case comment below.
Try creating a source folder named resources (right click project > New > Source Folder) and put everything in there. You can now do getClass().getResourceAsStream("/file.ext") and it shouldn't be null.
If there are any other errors after you create your jar file (following the picture above), you can run this through command line/terminal and it should show you any errors you may have:
java -jar jarfile.jar
I want to use things like text files and png files by using BlueJ. I also want it to work on a computer which does not have the resources.
How to do this?
There are many different ways to use external resources and to ensure they are accessible from another computer.
I'm not familiar with BlueJ but if its like most other IDEs...
For code that you write and run in BlueJ you should be able to put the resources next to the compiled .java files.
Say the resource you placed there was called "img.png" and the class containing your main method was called "Main.class".
To load this file as a BufferedImage you would do the following:
BufferedImage image = ImageIO.read(Main.class.getResourcesAsStream("img.png"));
This will load the file based on its location relative to your main class file.
If you want this file to be accessible to someone running your program on a different computer the best option is to bundle it with you code into a .jar file. See THIS for instructions on how to create a jar.
That will show you how to bundle your code into a jar next you have to add the resources (the "img.png" file). To do this download Jar Splice and follow the on screen instructions, its a very simple program to use.
The output from Jar Splice will be a .jar file containing your code and your resources which can be run on any computer with java installed.
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 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.