More Java Woes

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Foxi4
I've been working on my first ever game in Java and am happy to say it is nearly complete!
I'm using Eclipse IDE and JRE 8 + JDK 8. Everything runs perfect in Eclipse, however my exported JAR does not run. Instead I get an "A Java Exception has occured." message popup.
I did some research and found you can run JAR's in CMD to find out what is causing the exception. So, here is the problem:

C:\Users\Marduk\Desktop>java -jar frog.jar
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at Menu.loadBufferedImage(Menu.java:334)
at Menu.<init>(Menu.java:56)
at RunGame.<clinit>(RunGame.java:3)


Don't mind the RunGame part, it is not important it just calls everything (the main method)
The REAL problem is line 334, my BufferImage method.

So, here it is:
Code:
private BufferedImage loadBufferedImage(String string) {
    try {
        BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
        return bi;
    } catch (IOException e) {
    }
    return null;
}

Again, this works perfectly in Eclipse. I even tried giving 'bi' a permanent one image, but no luck! It certainly messes up my images in my game while run the IDE though! (A sign that it is working)

Code:
private BufferedImage loadBufferedImage(String string) {
    try {
        BufferedImage bi = ImageIO.read(this.getClass().getResource("\\bggfx\\FrogGierStartScreengroundPanel_sd.png"));
        return bi;
    } catch (IOException e) {
    }
    return null;
}

For the sake of you understanding what is wrong with line 56-58, here is my code starting with Menu() in the class Menu.
Code:
public Menu() {
int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
ExitColor=Color.BLUE;
InfoColor=Color.BLUE;
MultiColor=Color.BLUE;
StartColor=Color.BLUE;
t = new Thread();
t.start();
//Main Menu Size and Set Visible'
//Loop check getScreenSize() if 1600x900 then BgPic becomes BgResizedUser (controlled by loop)
setFocusable(true);
BgPanel = loadBufferedImage("\\bggfx\\FrogGierStartScreengroundPanel_sd.png");
BgPic = loadBufferedImage("\\bggfx\\FrogGierStartScreenBackground.png");
GameBG = loadBufferedImage("\\bggfx\\map.png");
switch(screenRes)...
FYI, I've also tried
BgPanel = loadBufferedImage("/bggfx/FrogGierStartScreengroundPanel_sd.png");
rather than \\, it produces the same effect.
So what is wrong? It is not the images... They load perfectly in Eclipse! Maybe the code on lines 56-58 is wrong though? Looking for any suggestions!
And yes, I'm exporting all my resources into the JAR when I "EXPORT" it in Eclipse.

I can get it to run but only if I take away all images that use loadBufferImage...
Very strange.
Also audio does not play... Seems like it can't find the resources :|

Most Likely this is the culprit:
at javax.imageio.ImageIO.read(Unknown Source)
 

Psionic Roshambo

Well-Known Member
Member
Joined
Aug 12, 2011
Messages
2,244
Trophies
2
Age
50
XP
3,315
Country
United States
Not sure about this, since I am not a java programmer at all (My experience is with C++ so at least it looks semi close... lol)

This page may help? (It's almost a stab in the dark from me, but I wan't to help lol)

http://stackoverflow.com/questions/5713096/javax-imageio-iioexception-for-apparently-no-reason

Down in the comments is where I suspect part of the problem is solved? Again I am outside my comfy zone on this one so if it's dumb just tell me to bugger off :)
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Not sure about this, since I am not a java programmer at all (My experience is with C++ so at least it looks semi close... lol)

This page may help? (It's almost a stab in the dark from me, but I wan't to help lol)

http://stackoverflow.com/questions/5713096/javax-imageio-iioexception-for-apparently-no-reason

Down in the comments is where I suspect part of the problem is solved? Again I am outside my comfy zone on this one so if it's dumb just tell me to bugger off :)


I found that post on STOFLW earlier, sadly it did not apply to my issue or I misunderstood what his solution was.
I just tried getting rid of my folders in my "res" source folder because they were not officially marked as "source folders".
Nothing changed. :|
Thanks for the reply though.

OK! The problem is definitely with loading resources because my after removing all my image references the program loads but my audio does not play.
It says "null" in cmd, which is the audio.
Definitely a resource issue!
Any ideas?
I'm beginning to think JRE8/JDK8 is responsible!
 
  • Like
Reactions: Psionic Roshambo

Foxi4

Endless Trash
Global Moderator
Joined
Sep 13, 2009
Messages
30,818
Trophies
3
Location
Gaming Grotto
XP
29,787
Country
Poland
Try:
Code:
BufferedImage bi = ImageIO.read(getClass().getResource(string));
return bi;

...and use single slashes - you're using double slashes in your strings, why?
Code:
BgPanel = loadBufferedImage("\\bggfx\\FrogGierStartScreengroundPanel_sd.png"); //Why?
Mind you, you're asking the worst person in the universe - I temporarily gave up on Java and went back to C/C++. :P
 
  • Like
Reactions: Psionic Roshambo

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Try:
Code:
BufferedImage bi = ImageIO.read(getClass().getResource(string));
return bi;

...and use single slashes - you're using double slashes in your strings, why?
Code:
BgPanel = loadBufferedImage("\\bggfx\\FrogGierStartScreengroundPanel_sd.png"); //Why?
Mind you, you're asking the worst person in the universe - I temporarily gave up on Java and went back to C/C++. :P

FYI, I've also tried
BgPanel = loadBufferedImage("/bggfx/FrogGierStartScreengroundPanel_sd.png");
rather than \\, it produces the same effect.

As stated in OP I tried / and \\ both produce the same effect.
It does not seem to be just an image issue because even the audio won't play if I disable the images to allow the program to run.

Audio becomes 'null' for whatever reason. This is really weird because my friend used the same buffered image code but his works... His program is running on JDK and JRE 7 though.

The code is identical too... Hmm

I'll give your idea a try, and if I fail I'm going to to try compiling with JDK 7 and running on JRE 7.

Try:
Code:
BufferedImage bi = ImageIO.read(getClass().getResource(string));
return bi;

...and use single slashes - you're using double slashes in your strings, why?
Code:
BgPanel = loadBufferedImage("\\bggfx\\FrogGierStartScreengroundPanel_sd.png"); //Why?
Mind you, you're asking the worst person in the universe - I temporarily gave up on Java and went back to C/C++. :P


No luck with the code you suggested.
Also JRE 7/JDK 7 does not make a difference, same errors but only if the JAR file is exported and ran. (JRE 7 actually breaks a bit more code in the IDE)

I'm really stumped :|

Ok! So it is DEFINITELY this:
private BufferedImage loadBufferedImage(String string) {
try {
BufferedImage bi = ImageIO.read(getClass().getResource(string));
return bi;
} catch (IOException e) {
System.err.println(e.getMessage());
}
return null;

}
Because this:
private BufferedImage loadBufferedImage(String string) {
try {
BufferedImage bi = ImageIO.read(getClass().getResource("/map.png"));
return bi;
} catch (IOException e) {
System.err.println(e.getMessage());
}
return null;

}

Works, but is (obviously) only that image.
 
  • Like
Reactions: Psionic Roshambo

matt123337

Well-Known Member
Member
Joined
Mar 25, 2014
Messages
151
Trophies
0
XP
623
Country
Canada
the bggfx folder is inside the jar correct? If not then that's your problem, as the getResource(String s) method is returning null (as in it can't lcoate the input)

Also, I see that you tried both, but it's a lot easier to read if you just use a single slash.
 

Mangofett

GBAtemp Testing Area
Member
Joined
May 14, 2006
Messages
4,885
Trophies
1
Age
19
XP
1,059
Country
United States
From your description it sounds like Eclipse is not properly including resources in the .jar file. A quick internet search came up with this: http://www.coderanch.com/t/526139/vc/Place-Resource-Files-Eclipse-Include


Eclipse needs the resource files to reside in two places. One place so that the IDE can use them, and in another place so that they can be included in the jar. Placing them in just one of these locations will not take care of the other problem. They need to reside in both places.

The two places are:
1) The root of the bin directory (NOT inside of a sub-directory there)
2) In a sub-directory under the project folder.

Eclipse has an easy way to add the sub-directory for location #2. Right click the project and select New > Source Folder.

As long as the files live in both places, the IDE can use them AND they will be included in the jar.

Happy hacking.
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
the bggfx folder is inside the jar correct? If not then that's your problem, as the getResource(String s) method is returning null (as in it can't lcoate the input)
Also, I see that you tried both, but it's a lot easier to read if you just use a single slash.

I got rid of the bggfx folder so its now all the resources are in a folder "res" which is a "source folder".
Yes it is inside the jar when it is compiled.


From your description it sounds like Eclipse is not properly including resources in the .jar file. A quick internet search came up with this: http://www.coderanch.com/t/526139/vc/Place-Resource-Files-Eclipse-Include
Happy hacking.

Looks promising! I'll give it a try tomorrow!
Thanks!
No luck! :|
All the resource files are in the bin folder.
 
D

Deleted-236924

Guest
What's the folder structure in your .jar?

You could try using the absolute URL when calling loadBufferedImage for testing purposes (ex: "C:\Users\<user>\Desktop\game.jar\res\img.png"), I guess.

I found this
I'm not sure how correct this is (and not sure if it applies to Eclipse as well), but it could be worth investigating.

This seemed interesting as well
 
D

Deleted-236924

Guest
Okay, well I'm having the exact same problem and I can only load my file right if I give my program the absolute path to my file.

Trying to load a midi to play sound, but I can't get it to work any other way than using absolute path.

The annoying thing is that I got loading images to work fine in another program, but in your case (and in my case with my midi file) it won't work.

Edit: Got my midi file loading working, but unfortunately I think my solution won't work with images. :/
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
What's the folder structure in your .jar?

You could try using the absolute URL when calling loadBufferedImage for testing purposes (ex: "C:\Users\<user>\Desktop\game.jar\res\img.png"), I guess.

I found this

I'm not sure how correct this is (and not sure if it applies to Eclipse as well), but it could be worth investigating.

This seemed interesting as well

Okay, well I'm having the exact same problem and I can only load my file right if I give my program the absolute path to my file.

Trying to load a midi to play sound, but I can't get it to work any other way than using absolute path.

The annoying thing is that I got loading images to work fine in another program, but in your case (and in my case with my midi file) it won't work.

Edit: Got my midi file loading working, but unfortunately I think my solution won't work with images. :/

Same here! How did you get the midi working? I will post a tutorial once I myself get this working.
It's amazing that there is not a lot of information about this...
Maybe it only applies to Eclipse SR2?
 
D

Deleted-236924

Guest
Code:
final Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
InputStream is = new BufferedInputStream(this.getClass().getResourceAsStream("birabuto.mid"));
sequencer.addMetaEventListener(new MetaEventListener() {
	@Override
	public void meta(MetaMessage msg) {
		if (msg.getType() == 0x2F) { // End of track
			// Restart the song
			sequencer.setTickPosition(0);
			sequencer.start();
		}
	}
});
sequencer.setSequence(is);

However, BufferedImage doesn't accept BufferedInputStream so that probably won't help.

I am able to get images to load properly by simply doing Image image = new Image("image.png"); (with image.png in the <default package>), but I'm assuming that you need them to be BufferedImage for some purpose or another.
 
D

Deleted-236924

Guest
nah, midi file extension is .mid

Could you try this?

Code:
BufferedImage bi = ImageIO.read(this.getClass().getResourceAsStream("image.png"));

(getResourceAsStream() instead of getResource())
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
nah, midi file extension is .mid

Could you try this?

Code:
BufferedImage bi = ImageIO.read(this.getClass().getResourceAsStream("image.png"));

(getResourceAsStream() instead of getResource())

Makes no difference. -works in eclipse just fine though.

Same error via CMD
<dir>java -jar frog.jar
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at Menu.loadBufferedImage(Menu.java:342)
at Menu.<init>(Menu.java:57)
at RunGame.<clinit>(RunGame.java:3)
 
D

Deleted-236924

Guest
Well this particular line of code seems to work just fine on my end, using NetBeans.

If I change "image.png" to something that doesn't exist, build fails as expected.
When image.png exists (right now it's in the same package as the java file), it can find it fine and build the .jar normally. I can then run it directly from NetBeans or run it manually by double-clicking the .jar
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Good news! (sort of)
As a temporary solution I can now run images and audio as long as they are in the same folder as the .jar
Still not sure why packed content refuses to work.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Veho @ Veho: Mkay.