I have a game completely done, but I need to make a runnable jar of it. I have a res folder with subfolders that contain textures and sounds and midi music.The images and sounds load perfectly, but the midi files, while they play when I run the game in eclipse, they do not run when I play the game as a runnable jar.

The problem is with the file loading. I checked and the game is throwing an IOException when I try to load the file.

I do not have this problem with other resources. Can anyone help me out here? I need to get the game runnable in the next few hours.

//i know it's kinda stupid to have an enum for just this purpose but it makes some other code just slightly easier so cut me a break :ppublicstaticenumSong{MENU("menu"),LEVEL_1("level_1"),LEVEL_2("level_2"),LEVEL_3("level_3"),LEVEL_4("level_4"),LEVEL_5("level_5"),LEVEL_6("level_6"),LEVEL_7("level_7"),LEVEL_8("level_8"),LEVEL_9("level_9"),LEVEL_10("level_10"),ENDING("ending");

I highly doubt the path is wrong. Like I said, this works in eclipse and I use a practically similar method to load the PNGs that work perfectly in the JAR. I think it's something with the midi, but I couldn't imagine what.

Sorry to be such a pain, but is there something I'm missing here? I can't get Thread.currentThread().getContextClassLoader().getResource() to work for any resource, including resources that I know are there for a fact. The code doesn't work in eclipse or in a Jar. Is there some special common setup that is necessary for this to function that I don't have implemented?

Just a note, I tested it and "Thread.currentThread().getContextClassLoader()" does return an object, so the problem is only with the getResource() method

Err... didn't work. Somebody commented about saving it in the root of the project? I'm not sure what this means and I wasn't able to figure it out with google, probably because I wasn't using the right key words. Is this music folder in the root?

I realized that the "MusicPlayer.class.getResource" was the way to go. I dunno why those other methods weren't working, but they weren't, so that's that. I was looking over my working texture loading code I wrote months ago and saw that "getResource()" worked there so I tried it here and it's glorious.

Thank you to all to all who responded! Couldn't have done it without you! What a confusing mess, truly one for the ages.

It's not that confusing really. It's just so many people here are misinformed. Resource loading is very simple if you understand how the methods work.

Also the difference between getResource(String) and getClassLoader().getResource(String) is that the String you pass to getResource(String) begins with a forward slash, like in your example, it removes the forward slash and calls getClassLoader().getResource(String) so it's much more efficient to directly call getClassLoader().getResource(String) without a leading forward slash.

Is there any article or tutorial about this topic anywhere? Maybe a chapter in a book? I had nearly the same problem yesterday with resources not being loaded and it took me one hour to get it working. What a waste of time. I don't have the feeling though that I actually know what I am doing. But I didn't find any place in the depths of the internet where someone took the time and had the knowledge to explain these things. It seems to be like in this thread: everyone says "do it this way, it works for me... at least it should" but there is no background to why it should work and how it does.And maybe even more important: why other methods that don't work in this case might be used in other cases too.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org