Use the getResource() method to return a URL that can be opened later, or getResourceAsStream() to open the resource directly as an InputStream. There are several different objects where this method can be used, and which you should use depends on many subtle aspects of java classloading, but for 99% of cases there are two approaches, depending on whether you want an absolute or relative resource path:

1 2 3 4 5 6

// APPROACH 1: To load from the root of jars in your classpathThread.currentThread().getContextClassLoader().getResourceAsStream("images/backgrounds/sky.jpg");

// APPROACH 2: To load resources relative to the package of this class (the class of 'this')// e.g. if your class is org.stuff.Game, this will look for /org/stuff/config/settings.xmlgetClass().getResourceAsStream("config/settings.xml");

Notice that neither path uses a leading "/", such as "/config/settings.xml". Using a leading slash in the first example simply won't work much of the time, while it will cause the second example to behave similarly to the first (searching from the root). Finally, there are environments such as OSGi where

neither of these approaches is correct, so the best advice is to use a utility class to hide all this behind, so you can change it out if and when you need to.

. Except it does completely the wrong thing on Windows, so you can fall back on using System.getEnv() and reading the USERPROFILE variable on Windows or HOME on Unix. You don't usually even have to detect the OS if you just look for those variables and bypass the problematic user.home property altogether.

The real problem is, what are you actually trying to do? If you're trying to read or save settings, then read on to see just how deep the rabbit hole goes: First off, seriously consider using the Preferences API for application settings -- it takes care of all this stuff for you. If that's not an option for whatever reason, then your problem becomes figuring out the appropriate directory to use. Most users don't really mind all that much when you drop folders into their home directory, and a lot of applications do so, whether Unix or Windows, but if you really want to be a proper citizen, that's where things get really hairy.

Windows: The environment variable you're looking is APPDATA for application prefs, but LOCALAPPDATA for stuff you don't want synchronized on roaming profiles. Throw a directory under there and it's good enough for government work ... but the frightening whole story can be seen in this SO thread.

Linux and other Unixoids: You think preferences live in $HOME? The freedesktop.org people have different news for you with the XDG Base Directory Spec. It not only splits app data into "config" and "data" dirs ($XDG_CONFIG_HOME and $XDG_DATA_HOME) but adds to the "fun" by adding path search environment variables for each! Short story: use a hidden folder in $HOME, or use the Preferences API.

OSX: TODO -- I'm going to need someone to fill this in.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Game DevelopmentQ: How do run I my game at a constant framerate?It's not anything special: after every frame you draw, simply call a method that measures the time between now and the last frame you drew, and pause for however long it takes to make up the difference. How you do this pause can be as simple as a single Thread.sleep or as complex as multiple sleeps and yields, depending on how precise you need to be. Obviously you can only slow down your game this way to reach the desired framerate. If you need constant-rate motion in your game, you will still need to adjust for the actual time each frame is taking, not just the ideal frame rate.

As with any utility, it's best to use an existing utility class that accounts for things like timer inaccuracy on different platforms and adapt it to your needs. Some implementations of this:

This post is to collect draft questions that don't have answers yet. Feel free to reply if you want to answer one or submit a question (remember, it should be actually frequently asked here on JGO, not just be a "good question")

Q: What's an Entity System? Should I use one?A: (blah blah various definitions yadda yadda link to articles and implementations. composition is good, entity systems are a maybe, but have lots of tradeoffs)

Q: I'm new to learning Java. Where do I start?A: (links to tutorials, books, etc, use mid-level toolkit like libgdx. read javadoc, ctrl-click stuff)

I used the thread context classloader answer you gave, but I'm kind of wondering about it: is the context classloader even relevant outside of JavaEE? I'm pretty sure it doesn't work properly in OSGi, and you have to use the current classloader instead, though I'm not going to sweat it since I doubt anyone's going to be using OSGi anyway...

I used the thread context classloader answer you gave, but I'm kind of wondering about it: is the context classloader even relevant outside of JavaEE? I'm pretty sure it doesn't work properly in OSGi, and you have to use the current classloader instead, though I'm not going to sweat it since I doubt anyone's going to be using OSGi anyway...

According to kevglass it's the only reliable way to access resources within the sandbox (applets, webstart). In an environment where no security manager is active, it's not really any better than your average classloader, but that's no reason not to use it in the general case.

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

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