file path that works in both Applets and Applications

Hello, I have been working on a game recently, and I have tried to make it run both as an applet and an application, I have been able to overcome most of the problems, but one I could solve but it would be inelegant.

The problem is that when I run as an applet (this is all in eclipse btw) it treats the root folder as /bin, but when I run it as an application it treats the root as being the project folder. Is there a way to make both of them the same in a way that will work with jar files nicely.

thank you, and sorry if I wasn’t very clear

h3ckboy

I just recently had a similar problem. And I also think Sproingle(sp?)'s new FAQ thread covered this (can’t find the link).

The solution that worked for me was answered in the above thread with

URL url = this.getClass().getClassLoader().getResource("resources/Ball.png");

The one in sprongles FAQ sounded like it was more reliable, it was something along these lines:

Thead.getCurrent.load.... 

Oh god, it was nothing like that, but it started with Thread. :L

searches for spoingle faq

[EDIT]

Found it!


// APPROACH 1: To load from the root of jars in your classpath
Thread.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.xml
getClass().getResourceAsStream("config/settings.xml");

I add a top level folder named “res” and then add it to the project build as a class folder.

Then you use this line of code to grab em!


TheClassWhereMainIs.class.getResource(filename)

getClass().getClassLoader().getResource(String) is more reliable. I believe Thread.currentThread().getContextClassLoader() doesn’t work in Applets in the browser.

Also, it is best to put “res” under “src” so you can do getResource("/res/" + fileName) and when exporting, everything is neatly organized under a “res” folder.

jonjava’s way worked like a charm after i tweaked my code to accomodate.

@ra4king: I tried the code you gave, but it gave a compiler error at the part, it just wanted to create a local variable. Also as for putting resources in the src folder, I have tried this in the past, but for some reason it has only worked when I put it into the bin folder. If you know a way to fix this I would be happy :).

Actually I have come across a problem just now, what if I am trying to load a directory? There is one point where I have to find all the directories inside of a specific directory. here is the code:


//important part
File dir = new File("bin/data/users/");
    		File listDir[] = dir.listFiles();
//less important part
    		for (int i = 0; i < listDir.length; i++) {
    			if (listDir[i].isDirectory()) {
    				component = new Button(new Rectangle(275,50+i*75,100,50),listDir[i].toString().substring(15),applet);
    		        component.addActionListener(this);
    		        components.add((Component)component);
    			}
    		}

most of this may not make sense, but the marked portion is what really matters, and what I am hoping to convert.

Thank you
h3ckboy

You can only list the contents of a directory in the file system, you can’t list the contents of a partial url path within a jar or the classpath.

The best way is to have some kind of index file which lists which files to open and their relative path. That may be hand-written or generated by some kind of build script.

Does this mean that I cannot make directories either?

Not within the jar(s) you’re running from, they’re immutable when you’re running from the classpath. Think of your games jars as your exe - it’s fixed and doesn’t change between users.

What are you trying to do? It sounds like you’re approaching something from the wrong direction.

@h3ckboy
The angle brackets were just me denoting that you can use either getResource(String) or getResourceAsStream(String) XD

And you shouldn’t be putting anything under bin…ever.

And as others pointed out, you cannot list the contents of a “folder” that is within a JAR nor can you create folders inside the JAR. If you want to make a save folder, use the Users’ home folder by getting the path from System.getProperty(“user.home”);

I guess, that would work, I was just trying to keep it all as compact as possible.

haha, ok, ra4king that makes a lot more sense, i thought you were thinking something along the lines of ArrayList() kind of thing.

thank you all once again :).

@ra4king
I never make it work with res folder under src. Maybe you keep the compiled class into it? logically I think eclipse execute class on bin folder, that’s why the res folder should be on.

Use a separate resource folder, mark it as a source folder in eclipse, and its contents will be copied into your jar. Me, I’m used to using maven projects, where it does this for everything in src/main/resources by default.