I am trying to create a runnable jar file .. did a few days ago with a single class and worked fine but now it doesn't seem to be working as good with multiple files .. Here is what I did

1) - create manifest.txt the contense of it is Main-class: HangmanLogic
2) - use this command in cmd
C:\Users\User\eclipse\Hangman> jar cvfm Hangman.jar manifest.txt HangmanLogic.class Hangman.class

that returns this in command line:

added manifiest
adding: HangmanLogic.class(in = 3920) (out= 2155)(deflated 45%)
adding: Hangman.class(in = 4700) (out= 2314)(deflated 50%)

3) when I try to run the jar file now using java -jar Hangman.jar I get the following errors

C:\Users\User\eclipse\Hangman>java -jar Hangman.jar
The word is trap and the hint is hint
[ _ ,  _ ,  _ ,  _ ]
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        at Hangman.<init>(Hangman.java:17)
        at HangmanLogic.<clinit>(HangmanLogic.java:10)

I think it has to do something with the manifest file? Not sure if its supposed to return "Added manifest"

It points to this location in the code in Hangman.java

    private ImageIcon img = new ImageIcon(getClass().getResource("images.jpg"));
    private Icon hanged0 = new ImageIcon( getClass().getResource( "0.png" ));
    private Icon hanged1 = new ImageIcon( getClass().getResource( "11.PNG" ));
    private Icon hanged2 = new ImageIcon( getClass().getResource( "22.PNG" ));
    private Icon hanged3 = new ImageIcon( getClass().getResource( "33.PNG" ));
    private Icon hanged4 = new ImageIcon( getClass().getResource( "44.PNG" ));
    private Icon hanged5 = new ImageIcon( getClass().getResource( "55.PNG" ));

and in HangmanLogic.java
private static Hangman hmGUI = new Hangman();

Recommended Answers

All 5 Replies

the problem is, it can't find the resource you're trying to use.
it's a NullPointerException. this is not related to the .jar, to the manifest file or anything. the .jar file runs just fine, if it wouldn't, it wouldn't print this:

The word is trap and the hint is hint
[ _ , _ , _ , _ ]

have you packed the images in the right place?

Um, everything in in the same folder, but not as a package. In order to make jar of multiple files, do they have to be package or just located within the same folder?

The error shows your classes and manifest are ok. It looks like you failed to package your png files in the jar properly - the message implies it can't find them.

I made an eclipse project, made a runnable jar from eclipse and I get the same error again >.>

If anyone runs into error like this, the solution is this
private ImageIcon img = new ImageIcon(getClass().getResource("images.jpg"));

changed to this:

URL source = Hangman.class.getResource("images.jpg");
private ImageIcon img = new ImageIcon(source);

worked making jar with eclipse, but doesn't work with cmd

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.