Where to put data files

Ok, I figure I'll just put the rather basic question up front:

How do I access an external file?  I'm wondering where exactly to put the file, if I need to adjust anything in my IDE, and what to type in my code.

Now to give a little background on why I'm asking this:

I'm pretty new to jME (actually, to Java in general) although not to 3D game programming (ie. I've written games in the past in other development tools.)  I've started going through the beginner tutorials on the wiki and have gotten through the first few with little problem, but I'm stumped on the one for model loading:


In the midst of the code it has

// Point to a URL of my model

URL model = HelloModelLoading.class.getClassLoader().getResource("jmetest/data/model/maggie.obj");

I believe that to be my problem right there.  I just get an IO error when I run this code, so I'm assuming that file path/name is not actually accessing the file.  Similarly, changing it to "data/test.obj" with my model in a folder called "data" in the project directory doesn't work, even though "data/test.jpg" did work in a previous tutorial for loading a texture.

Meanwhile, while searching for more information I found this page:


This comment in the midst of the code points to what I need to do, but I don't understand what it means:

// File Path for the model (dump file in toplevel of classpath)

URL url = MyClass.class.getClassLoader().getResource("head.md3");

I assume this is a general Java question and not specific to jME so I apologize for posting it here, but I've tried googling for help and everything I see about model loading just assumes that you know how to access the file or is just generally too cryptic for me to understand.  Like, I've looked up java.io but I just get dizzy with the whole runaround of FileInputStreams and DataInputStreams, and anyway I don't see how it all relates to jME.  Indeed, it's such a basic question that I can't believe it took so long to explain what exactly I'm getting at.

If anyone can help me out here that'd be great!

I recommend if you're going through the tutorials, and something isn't clicking right, or you're getting errors - go through the JME source, as they're all there. In the first example you have, if you notice the location of the URL is jmetest/data/model/maggie.obj Well, in the source there is a package named jmetest.data.model where the maggie.obj is located.

I use eclipse personally, and its just a matter of creating a package for your data (or just dropping the stuff top level in your source folder) and then accessing it in your code. Not sure if its different or the same for other IDEs like netbeans however.

Thanks for the reply!  I already knew where maggie.obj was located, but I didn't know if the filepath in the tutorial was meant to work or if that was just telling me where the model is located so that I could adjust things appropriately (the tutorial didn't specify.)

Anyway, the second part of your post did reassure me that I'm not just going insane here, and that what I'm doing should be working.  Well there was one little adjustment I needed to make in NetBeans, but it was easy enough and the help pointed me to it; I needed to add the data directory in the project properties.  Once I did that the model loaded, yipee!

Unfortunately I can already tell this is going to be a headache again the first time I attempt to deploy my project so other people can run it, but I guess I'll cross that bridge when I come to it.

search for ResourceLocator in the forum, if you make use of it, you can save yourself some headache when deploying :slight_smile:

Y'know now that you mention it I did see a reference to that when searching the forum for information.  In fact, now that I'm looking at the thread again, it's one you posted in, heh:


I didn't really understand how to use ResourceLoader from your post there, but then I only saw the one post so now I'll look up more about it as you suggest.

ADDITION: Darnit all I've already encountered another major stumbling block with model loading.  Although the mesh is loading now, the texture isn't loading.  aargh, I think I'm gonna go run a few errands and then come back to try to figure out this ResourceLoader thing.

First set up the resource locator ( you do this once, when the game gets initialized)

notice the trailing '/'

    new SimpleResourceLocator(Main.class.getClassLoader().getResource("com/jmonkey/mygame/resources/")));

Then later use the Resource locator, whenever you need to load a resource:
Notice we only tell the locator that we want to load north.jpg, he'll know where to searh for it now,

                        "north.jpg"), Texture.MM_LINEAR, Texture.FM_LINEAR));

Great, thanks for the explanation!  Now the model is loading with texture.  Boy that turned out to be much more rigmarole than I expected.  :expressionless:

ADDITION: well well well, a little playing around and I've discovered that I don't need to use the resource locator when loading my model in order to find the texture, I just need to have the locator path set.

Hi jhocking,

You are facing a notion that is very common to Java programming: the classpath.

The classpath is actually a set of directories or packaged (which ends up being the same) that affects how Java loads classes. The ClassLoader (which loads classes) will usually look accross all directories and packages in the classpath.

When running a Java application from command line, it is common to run Java like this:

java -cp lib/mygame.jar:lib/jme.jar:data:classes test.SampleApplication

The line…

// File Path for the model (dump file in toplevel of classpath)

URL url = MyClass.class.getClassLoader().getResource("head.md3");

…is used to obtain a URL that points to a resource located within the classpath. This way, you don't really tie yourself to a particular directory, and you can relocate and deploy your applications much more easily (for example, when deploying through the web this allows you to easily deploy your data files). This is not a requirement, though, as other applications may prefer to use normal data files.

During deployment, resources should be located inside your binaries (generated .class files) directory. Be very carefull however, because most IDEs clean that directory often! To avoid that, most IDEs also allow you to define a "resources" directory that will be combined with the compiled classes to compound the classpath that will be used to run your application.

Hope it helps!

Best regards,

Thanks for the explanation on classpaths, it's a notion that I was gradually becoming more aware of as I kept plugging away at this.  Which incidentally is why I'm posting another discovery I made related to the classpath (both as a note to self and I suppose for the benefit of anyone in the future searching the forum for how to do this.)

While trying to get things to run from the built .jar, I figured out that I needed to manually add the data files directory to the class path in manifest.mf.  It's a minor annoyance, may only apply to Netbeans (I'm starting to think maybe I should switch to Eclipse), and it's entirely possible I'm doing something wrong to need this hack, but it works and I only need to do it once.