1

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    1. Ensure that the resources are actually been included in your Jar. Different IDEs and build systems have different requirements when it comes to "how" these get embedded. If you're doing it from the command line (or a "non traditional" IDE), you might find that you need to take some extra steps. A Jar file at the end of the day is just a zip file, so you just unzip to check the contents. 2. Make sure that the path to the resource you are trying to load is correct. For example, in your code above `"/ball.PNG"` assumes that the resource is within "default" or "root" path of the Jar file. – MadProgrammer May 06 '22 at 02:23
  • 1
    When using `Class#getResource`, relative paths are based on the class's package path (so using absolute path often helps ;)) – MadProgrammer May 06 '22 at 02:24
  • Yes. In my first paragraph I mentioned that I have tried a combination of full and partial path names. I also mentioned that I had taken a look inside the jar file and the image was in it. I also said that there were no subdirectories, and the image was in the root path of the jar file. I have tried your suggestions already to no avail unfortunately. – A_Mindless_Nerd May 06 '22 at 02:52
  • Please edit your question to show the file structure including where the ball.png file is, and the location of the class is that is attempting to load the resource. Is the `ball.png` file located in the same folder inside your jar as the class that is calling `getResource`? If not, then that is your issue, and you need to adjust the path accordingly. – sorifiend May 06 '22 at 03:04
  • Please post output of `jar tf your.jar` inside a code block in your question – g00se May 06 '22 at 09:41
  • I've updated the question to include some edits and pictures. – A_Mindless_Nerd May 06 '22 at 20:16
  • Does this answer your question? [Reading a resource file from within jar](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar) – haba713 May 06 '22 at 20:18
  • 1
    How are you running your code using the jar file? Note that the names of the file are case sensitive so "ball.PNG" is not "ball.png". – matt May 06 '22 at 21:10
  • 1
    The case sensitivity could be why your issue changes when you use the jar file vs the class file. On the file system, windows didn't recognize different cases, "ball.PNG" is the same as "ball.png" but in the jar file, the case matters. – matt May 06 '22 at 21:18
  • That was it. I needed to change the .PNG to .png. Didn't realize that the file was case sensitive. Thank you. – A_Mindless_Nerd May 07 '22 at 02:34

0 Answers0