(IntelliJ) Maven adds png resource to jar, but doesn't use it - java

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.

Related

Icons in compiled jar file [duplicate]

I have a simple program that needs to display images.
I know how to do this running the code from Eclipse and I know how to do it running from a JAR file, but I'd like a solution that works in both cases.
Eclipse project is as such:
- Project (java)
- src
- controller
- Main.java
- ui
- Display.java
- images
- image.jpg
The code snippet that works from within Eclipse:
ImageIcon image = new ImageIcon("images/image.jpg);
The one that works for a JAR (all in a single JAR file):
java.net.URL imgURL = getClass().getResource("/images/image.jpg");
ImageIcon image = new ImageIcon(imgURL);
What would I need to change in order to get a single piece of code that works in both situations?
Put the images folder inside the src folder, and Eclipse will copy the images into the target folder (bin or classes, generally), which will make them available from the classpath, just as if they were in your jar in the released version of your app.
getResource() doesn't look in a jar. It looks in the classpath of the classloader. Whether the image is in a jar or not is thus not important. It must be in the classpath. And obviously the target folder of eclipse (bin or classes, generally) is in the runtime classpath of the app when you launch it from Eclipse.

JavaFX, can't get icon on the title bar to show up after exporting to *.JAR

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

Loading a .png into eclipse?

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.

Cannot Load Images from 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.

Can not load the images in Java Swing when running from a .jar-file

I can run my Java Swing application from Eclipse without problems. But when I run it from a .jar-file, the images fails to load.
I load my images with:
setIconImage(Toolkit.getDefaultToolkit().
getImage(getClass().getResource("../images/logo.png")));
How can I load the images so they work even when I'm running from a .jar-file?
The images is in the same Jar-file, in the packet com.example.images and the class where they are used is in com.example.gui.dialogs
Use the absolut path inside your jar. If you don't know it, try opening the JAR with a zip programm, e.g. 7zip. Then use the absolute path:
getClass().getResource("/com/examples/images/logo.png")
This obiously only works when your image is in your jar. If its not, but in your classpath, this should be fine too.
The images should be packed as well in a jar file. Actually, I'm not 100% sure there is no other solution, but at least I made it work this way, when experimenting the same issue.
The jar was then added to the classpath, and I'm accessing image resources this way:
getResource("images/logo.png");

Categories