'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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source