1

I created an application using netbeans, and it works very good when I run it from netbeans, but when I try to run it from a command line or from the excutable .jar file, then it doesn't work! And this errors appears to me:

Dec 12, 2011 11:36:33 PM game.Threads.Intro_main <init>
SEVERE: null
javax.imageio.IIOException: Can't read input file!
        at javax.imageio.ImageIO.read(Unknown Source)
        at game.Threads.Intro_main.<init>(Intro_main.java:58)
        at game.Intro.<init>(Intro.java:28)
        at game.Start.<init>(Start.java:25)
        at game.Start.main(Start.java:35)

Exception in thread "main" java.lang.NullPointerException
        at sun.awt.CustomCursor.<init>(Unknown Source)
        at sun.awt.windows.WCustomCursor.<init>(Unknown Source)
        at sun.awt.windows.WToolkit.createCustomCursor(Unknown Source)
        at game.Threads.Intro_main.<init>(Intro_main.java:69)
        at game.Intro.<init>(Intro.java:28)
        at game.Start.<init>(Start.java:25)
        at game.Start.main(Start.java:35)

I think that the problem is the command line & the .jar doesn't determine the true path for images which I load in the application, so I don't know the right way to avoid this problem.

I know that there are many questions here about this problem, but I didn't know the true solution, I tried many answers and they didn't work!

Thank you for your cooperation :)

Kevin
  • 53,822
  • 15
  • 101
  • 132
Jason4Ever
  • 1,439
  • 4
  • 23
  • 43
  • How do you load the images? And where in the jar are they located? – Roger Lindsjö Dec 12 '11 at 21:43
  • Can you show us line 58 in Intro_main.java? (Or, even better, show us Intro_main.java.) – Jack Edmonds Dec 12 '11 at 21:50
  • @RogerLindsjö i load images using BufferedImage imageIO.read(filepath); the jar file is located in /dist project – Jason4Ever Dec 12 '11 at 22:09
  • @JackEdmonds intro_bg = ImageIO.read(new File(assets/Graphics/intro_bg.png)); intro_roz = ImageIO.read(new File(assets/Graphics/intro_roz.png)); intro_cursor = ImageIO.read(new File(assets/Graphics/intro_cursor.png)); intro_cloud = ImageIO.read(new File(assets/Graphics/intro_cloud.png)); – Jason4Ever Dec 12 '11 at 22:11

3 Answers3

3

The best way to avoid this is to package the images you want to use in the .jar file. Then use the classloader to load the images.

javax.imageio.ImageIO.read(getClass().getResourceAsStream("myImage.png"));
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
  • i did this and this error appeard to me also !! : Exception in thread "main" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) at game.Threads.Intro_main.(Intro_main.java:58) at game.Intro.(Intro.java:28) at game.Start.(Start.java:25) at game.Start.main(Start.java:35) – Jason4Ever Dec 12 '11 at 22:14
  • @Jason4Ever Are you sure you put the `myImage.png` into the .jar file? Try unzipping it (a jar file is actually a .zip file in disguise. Just change the extension from .jar to .zip) and make sure you can locate `myImage.png`. – Jack Edmonds Dec 12 '11 at 23:31
  • hii i found the problem , the problem is the classes inside the jar file don't load images from the images folders inside .jar file , but when i copied the images folder to be out of .jar file , and put it with the .jar file , the jar file worked fine , because it loaded the images from the folder out of .jar file , so how i make classes load images from the folder of images inside .jar file ?? – Jason4Ever Dec 13 '11 at 15:14
  • Try `javax.imageio.ImageIO.read(getClass().getResourceAsStream("images/myImage.png"));` – Jack Edmonds Dec 13 '11 at 15:21
  • Thank you man :) , i used this method but it's also not working ! , but i solved it by adding / at the first of full path to be : javax.imageio.ImageIO.read(getClass().getResourceAsStream("/images/myImage.png")‌​); and it's working now very good :) :) – Jason4Ever Dec 13 '11 at 17:10
1

To solve this problem, simply extract jar file into a folder via winrar archive program. Then see the result.. Your external files wont be there.. So you have to put your external files with your jar file in the same folder. And NullPointerException problem is came out not correctly initialize your jar file.. You have to set your Main class which has main method.

olyanren
  • 1,448
  • 4
  • 24
  • 42
  • hii i found the problem , the problem is the classes inside the jar file don't load images from the images folders inside .jar file , but when i copied the images folder to be out of .jar file , and put it with the .jar file , the jar file worked fine , because it loaded the images from the folder out of .jar file , so how i make classes load images from the folder of images inside .jar file ?? – Jason4Ever Dec 13 '11 at 15:20
1

If you use read-only images, you could leave them in the jar and use the URL getClass().getResource() or the InputStream getClass().getResourceAsStream(). Otherwise you can use the path of the working directory with System.getProperty("user.dir").

The home directory System.getProperty("home.dir") often also is used: for instance with "/NetbeansProjects" for the NetbeansIDE.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138