Michael Haywood
Michael Haywood

Reputation: 319

Game works on netbeans but not outside netbeans in jar file?

I am creating a basic game of 'Pong'. I have finished the game apart from a few glitches I need to remove. The game runs perfectly in netbeans but if I create a jar file errors come up causing it to not work. I am quite new to java but I believe it is something to do with my code looking for the images but the images have not been loaded up yet. Here is the error. How can I get this to work outside of netbeans in a jar file?

C:\Users\michael>java -jar "C:\Users\michael\Documents\NetBeansProjects\Pong\dis
t\Pong.jar"
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        at pong.BallMainMenu.<init>(BallMainMenu.java:19)
        at pong.Board.gameInit(Board.java:93)
        at pong.Board.addNotify(Board.java:86)
        at java.awt.Container.addNotify(Unknown Source)
        at javax.swing.JComponent.addNotify(Unknown Source)
        at java.awt.Container.addNotify(Unknown Source)
        at javax.swing.JComponent.addNotify(Unknown Source)
        at java.awt.Container.addNotify(Unknown Source)
        at javax.swing.JComponent.addNotify(Unknown Source)
        at javax.swing.JRootPane.addNotify(Unknown Source)
        at java.awt.Container.addNotify(Unknown Source)
        at java.awt.Window.addNotify(Unknown Source)
        at java.awt.Frame.addNotify(Unknown Source)
        at java.awt.Window.show(Unknown Source)
        at java.awt.Component.show(Unknown Source)
        at java.awt.Component.setVisible(Unknown Source)
        at java.awt.Window.setVisible(Unknown Source)
        at pong.Pong.<init>(Pong.java:16)
        at pong.Pong.main(Pong.java:23)

Upvotes: 1

Views: 617

Answers (2)

JustDanyul
JustDanyul

Reputation: 14044

I'm suspecting you are writing the file urls relative to the working directory, which is potentially different when you execute it inside NetBeans (depending on where you execute it from when you run it from the console).

For example, when running this from the netbeans project the working directory is most likely C:\Users\michael\Documents\NetBeansProjects\Pong, where's to the working directory in your other example is just "C:\Users\michael\"

Say you are loading a image, from the path "myimages/test.jpg". When running from netbeans this would become "C:\Users\michael\Documents\NetBeansProjects\Pong\myimages\test.jpg" , and it would become "C:\Users\michael\myimages\test.jpg" in the other example.

Try using the following to access the resource instead

getClass().getResource("/myimage/test.jpg");

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

Upvotes: 2

Badar
Badar

Reputation: 1460

Have you added Java JDK and the external libraries (.jar files) to your System path variables?

As you are using Netbeans, you can use this source How to deploy Java Project using Netbeans properly. To set up System Path for Java, check out this link. It may be varying depending on your platform; i.e. Windows, MacOS, etc.

Upvotes: 0

Related Questions