28 Sep 2008

Loading Resources from a Static Class in Java

1 Comment Programming

If you’ve ever needed a static method to load a resource, there’s a lot of conflicting information about how to get a resource contained in your JAR. I tried SomeProg.class.getClassLoader() and a few others but ended up with either a null value or an exception. Eventually on a whim I tried the following, which worked.

The package is lime49.lockcrypt, and the file is located in the build path. As it happens I was loading a Chinese font.

InputStream in = LockCrypt.class.getClassLoader().getResourceAsStream("lime49/lockcrypt/fireflysung.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, in);
Tags: , , ,
written by
The author didn‘t add any Information to his profile yet.

One Response to “Loading Resources from a Static Class in Java”

  1. Reply HypeDeveloper says:

    Omg I just spent hours trying to figure out why a sound I was loading was playing when I compiled and ran, but not when i ran my .jar file!

    THANK YOU SO MUCH DUDE :D

Leave a Reply