File loads in eclipse but not in a runnable .jar [solved]

Oh my god so much misinformation and confusion in this thread. I will attempt to solve them all.

Using this image as what your current setup:

You load by doing .class.getClassLoader().getResource(“level/…”)

If you put the “res” folder under the “src” folder, then you begin by “res/level/…”

The getResource method returns a URL. DO NOT convert this to a URI and give it to a File object. It won’t work.

Give the URL directly to whatever you are loading.

YYYYYYYYYYYEEEEEEEEEEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSSS!

The working code, for those interested:

public static void playMidi(Song song){
		String filename = "/music/"+song.name+".mid";
		URL tempURL = MusicPlayer.class.getResource(filename);
		
		try {
			midiPlayer = MidiSystem.getSequencer();
			midiPlayer.setSequence(MidiSystem.getSequence(tempURL));
			midiPlayer.open();
			midiPlayer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
			midiPlayer.start();
			
			currentSong = song;
        } catch (MidiUnavailableException ex) {
        	System.err.println("Midi Unavailable! "+ex.toString());
        } catch (InvalidMidiDataException ex) {
        	System.err.println("Midi Invalid! "+ex.toString());
        } catch (IOException ex) {
        	System.err.println("File Not Found! "+ex.toString());
        }
	}

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.

I never ever used the “class.getResource” method in my life ever
and assets do no belong into the src folder in my eyes, and fortunately libgdx agrees.