Images not shown while running the JAR created by Eclipse - java

I have my images folder inside source folder in my Eclipse IDE
After exporting the application into a runnable JAR, I run it through command prompt, but it does not display any images that I have used. When I run it through Eclipse, images are displayed.
And when I copy the source/images folder to where my executable JAR is, then it shows images, but I want my images also to be in JAR itself.
What is the problem?

public BufferedImage loadImage(String fileName){
BufferedImage buff = null;
try {
buff = ImageIO.read(getClass().getResourceAsStream(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return buff;
}

Your images should NOT be in your source folder. You should hold them separate in another folder (assets or ressources or images or so).
To include the folder in your jar, I highly suggest using ANT script (or maven) to generate your jar. Its much more flexible. It takes a bit to get the grasp on it, but there are a lot of examples and tutorials around and I think its definitely worth it.

Related

How to access images within a .Jar File

I've been trying for a couple of days to access an image within a .Jar file. I've taken a look at several solutions on this site and nothing seems to be working for me. I've tried several instances of the code included below, using URL and inputStreams, with full path names, reduced path names, and automating the path names using system.getProperty("user.dir"); I've tried alternating between forward and backslashes, doubling up on backslashes and the like.
I also understand that accessing an image in a .jar is not the same as locating a file and it's path.
Code I've tried using:
url = getClass().getResource("/ball.PNG");
try {
image = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
try {
image = ImageIO.read(this.getClass().getResource("/ball.PNG"));
if (image == null){
System.out.println("Could not find image for the ball!");
}
catch (IOException e) {
e.printStackTrace();
}
InputStream input = getClass().getResourceAsStream("ball.PNG");
try{
image = ImageIO.read(input);
}
catch(IOException e){
e.printStackTrace();
}
None of these solutions are working. compiling and running works just fine. The jar file is made just fine, and when I look inside, the PNG I used is included. Actually running the .Jar file gives me the error "Exception in thread "main" java.lang.IllegalArgumentException: input == null!" along with a few more lines that just indicate that errors occur when trying to pull information from the image.
The most interesting part to me is that most of these solutions work in some form or another when running the .class files, but none work for the .jar file.
For more context, the variable image is a BufferedImage; I've been using Notepad++ and command prompt to run, compile and create the .class and .Jar without issues. There is no subdirectory for the image files, and again after checking, they are included in the .jar file. I have no idea why the .class files can find the images and use them but the .jar files cannot.
I know the code isn't pretty to look at and a bit incomplete, but keep in mind that these are just quick examples I've written up. Compiling and running works just fine, as does running the jar files. Only the .jar file has trouble locating the image file. I've looked at these threads and used their examples to form my code:
Using png in a jar file
Access .png image in .jar file and use it
Some edits others have asked for:
A picture of the files and their location
A picture of the command "jar tf BallGame.jar"
posted in a code block:
C:\Users\Phili\Documents\TheBallGame>jar tf BallGame.jar
META-INF/
META-INF/MANIFEST.MF
Ball.class
BallGame$1$1.class
BallGame$1$2.class
BallGame$1.class
BallGame.class
Coin.class
gamePanel.class
GlobalConstants.class
RedBall.class
ball.png
coin.png
Distances and stuff.png
redBall.png
A picture of what is actually inside the jar file.

how to make jar file to load images on run time

i am loading buffered image in java through the following lines of code and it works good and when i clean and build this code .jar file is created which also loads the same file.but now if replace the picture in src folder the jar file still loading the old one picture.
i want that once i clean and built my program the jar file can load images that are currently available.
is there any solution for that or any other file extension that runs my program without using idle but loads images at run time.
try {
return ImageIO.read(imageloader.class.getResource(path));
} catch (IOException ex) {
Logger.getLogger(imageloader.class.getName()).log(Level.SEVERE,
null, ex);
}
Using Class.getResource() loads file from the classpath.
So I suppose the your jar contains the image file (because it is in the src folder), and your program loads the file from the jar. Not from an src folder anywhere else !
HTH,
This is how to load an image from file:
File file = new File("Pass the image file location here")
BufferedImage image = ImageIO.read(file)

Buffered Images not rendering when project is built as an executable jar file in eclipse

So, I'm working on a 2D tile based game for my programming class. I've gotten the basics done with rendering images and moving in game. All my images work when I run the project in eclipse. When I build it as an executable jar file, none of the images show up.
This is what it should look like (when I run it in eclipse with my current set up)
What is should look like
This is what happens when I run the executable jar.What I get
My images are loaded in assets class like so:
SpriteSheet sheet = new SpriteSheet(ImageLoader.ImageLoader("/textures/sheet.png"));
Here is Image Loader:
public class ImageLoader
{
public ImageLoader()
{
}
public static BufferedImage ImageLoader(String path)
{
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
}
return null;
}
}
The two folders I have specified in the build path are src and res.
I know that it is building the images into the jar as i can see them if i extract it.
Any idea as to what I messed up on?
Pretty sure u need to add a Res folder to the build path.
The Res folder is where all your sprite sheets should be.
Right click your project and edit build path.
Try adding the textures folder to your build path
EDIT
The folder doesn't need to be named "Res"

Can't load resources when running the code from an executable JAR

I'm having this problem where none of the resources load when I run a JAR of the program. I've changed code and checked that it is indeed both the code that loads images and the code that loads sounds do not work. It works perfectly fine when I run it in eclipse. I checked with 7-Zip and the sounds and images files aren't in a res folder anymore. I tried changing the path files and the location of the resources in my program before I exported it, but that didn't work. I also have the res folder as a source folder in the build path. I'm exporting the JAR with eclipse, and then adding my libraries into it with JarSplicer. Here are the codes that load images and sound resources:
-Load Sound-
public static WaveData LoadSound(String path){
WaveData sound = null;
try {
sound = WaveData.create(new BufferedInputStream(new FileInputStream(path)));
} catch (Exception e) {
e.printStackTrace();
}
return sound;
}
-Load Images-
public static Texture LoadTexture(String path, String fileType){
Texture tex = null;
InputStream in = ResourceLoader.getResourceAsStream(path);
try{
tex = TextureLoader.getTexture(fileType,in);
}catch(IOException e){
e.printStackTrace();
}
return tex;
}
Here's my error in the command prompt:
Here are the files in the jar every time I exported it (regardless of whether the resources were in res or not):
I'm stumped here. If someone knows how to fix this, then please help me out.
I found the answer to my problem.
ResourseLoader.getResourceStreamAs() does not work inside of JAR files, so you need to do getClass().getResourceStreamAs(), or [ClassName].class.getResourceStreamAs(), if it's static.
Also, I had to change the location of the files from res/[resource file]/[resource] to /[resource file]/[resource] because when you export, it takes out all of the files in res. Also, you have to make sure that you have that / at the beginning of the path there in order to designate it to search in the source folder, or else it will search in the folder of the class that called getResourceStreamAs(). And also, new FileInputStream() doesn't work in JAR files, so you have to use [ClassName].class.getResourceStreamAs() instead. Oh, and on top of that, I had a few files that somehow got the extension part its name in all capitals for no reason. So that gave me some confusion. I basically just had to do a bunch of fiddling with code, but I got it working!
One more thing: make sure you add your resources folder to the sources tab in the build path, or else eclipse won't export it. And if you want your libraries to be exported in your JAR, then you'll have to add them manually by making a fat jar with JarSplicer.
Turns out that paths to resources are case sensitive and do not work in jar files if there is a single misstyped letter in the path.

Java - How To Set An Image Path Without Using The C:\\ Direcory

I created a java app and I put some images in it and even gave it an image icon as the desktop image, but when i made it a jar file, and put it on another pc, all images were gone. This is the image path :
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
I checked it online and i found out that the problem is that i got the file through the C:\\ directory, they said the image file should look like this :
File imageFile = new File("res/icon.jpg");
I tried this but it didnt work, I kept getting an error message like : file not found
This is my full code :
BufferedImage image = null;
try {
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
image = ImageIO.read(imageFile);
} catch(IOException e) {
e.printStackTrace();
}
setIconImage(image);
Please, i have been trying to solve this for weeks, do anyone know how i can solve this, please if you do, please help
The images should not be loaded from the file system, but should be bundled into the app, inside your jar.
If you put the image foo.png inside the jar, under the package com.bar.resources for example, you simply need to use
InputStream in = getClass().getResourceAsStream("/com/bar/resources/foo.png")
to load the image as an input stream.
That will use the class loader to load the image. So, during development, if you're using a standard IDE project, you just need to put the image file in the appropriate package in your source directory: the IDE will "compile" the file by copying it to the same directory as the generated .class files.
If you're using a standard Maven/Gradle project, then it needs to be put in the appropriate package under src/main/resources.
Problem is you dont have res folder in your project, first of create a folder a "res" by Right Clicking on project and place the image icon.jpg image in that "res". and
use : `File imageFile = new File("res/icon.jpg");`// To retrieve image to your project.
You cannot retrieve the image from your local system, because it is not attached with your projects workspace.
There are two options....
1)Either make res a "source folder" . You will know this if you are using Eclipse.
Then you can use like
ImageIO.read(new File("res/icon.jpg"));
2) If res is a normal folder, you will have to use. In this case res will be considered as a package
ImageIO.read(new File("src/res/icon.jpg"));

Categories

Resources