0

I have compiled .jar file using IntelijIDEA from few classes. The project structure is like this:

src -> .class; 
res -> buttons -> Draw.png

When I run this project from IDEA it works fine. But when I run compiled .jar file it does not.

I think the problem is with finding my Draw.png file. One of the class uses the following line of code:

String startButtonHover1 = Draw.png;
BufferedImage startButton = ImageIO.read(this.getClass().getResource("buttons\\" + startButtonHover1));

If I run .jar file via cmd it reports the error in the ImageIO.read : input == null!

How it can be solved? Maybe some cofigurations of IntelijIDEA have to be changed before building jar file and I did not do it.

I also tested these lines:

System.out.println(Main.class.getResource("buttons\\" + startButtonHover1));
System.out.println(Main.class.getResource("\\buttons\\" + startButtonHover1));
System.out.println(Main.class.getResource("res\\buttons\\" + startButtonHover1));
System.out.println(Main.class.getResource("..\\res\\buttons\\" + startButtonHover1));
System.out.println(Main.class.getResource(".\\res\\buttons\\" + startButtonHover1));

All of them generate null as well while first two give in appropriate output when I run the code from InteijIDEA.

Any suggestions will be appreciated.

bob
  • 4,282
  • 2
  • 22
  • 30
tov_Kirov
  • 21
  • 5
  • Are you sure that file is in jar? You can check this by renaming it to zip and looking inside. – talex Oct 22 '16 at 11:26
  • I decided to close your question as duplicate since duplicate contains detailed instruction about how to set up your project to avoid any problems. If that doesn't solve anything provide more information about how you set up your project structure (is `res` package, simple folder, or folder added to classpath/buildpath), how you created this JAR. In short, all steps necessary to reproduce your problem. – Pshemo Oct 22 '16 at 11:35
  • The solution found! 1.This line: BufferedImage startButton = ImageIO.read(getClass().getResource("\\resources\\buttons\\" + startButton1)); has to be changed to: BufferedImage startButton = ImageIO.read(getClass().getResource("/resources/buttons/" + startButton1)); 2. The folder with the pictures has to be added by the way which was described here: http://stackoverflow.com/questions/9864267/loading-image-resource – tov_Kirov Oct 22 '16 at 12:23

0 Answers0