• Java - Filepaths
    5 replies, posted
My program is set up with packages like this: spaceinvaders (contains all my class files) spaceinvaders.resources (contains my images) (Although I have tried having the resources package as a separate package rather than a subpackage) I've been struggling all weekend to get the path the files in the resources package. I'm using lwjgl and slick, and need to load images (as Textures and SpriteSheets) from the resources package. These classes require an InputStream or a String to retrieve the file, so I've tried: resources/image.png /resources/image.png Toolkit.getDefaultToolkit().getClass().getResource(<path>).toString() Main.class.getResourceAsStream(<path>) new FileInputStream(new File(<path>)) which all returned "FileNotFoundException", which varied from "could not find file" to "volume label incorrect" I assumed this was because Toolkit.getDefault... returned the path with file:/C:/ instead of C:/ so I made it a substring beginning at (6). This still didn't work. Next, I added to that a replaceAll("%20"," ") to remove the %20s and suddenly the file worked... in the IDE. I cleaned and built, to be shown the exception "could not find file "ile:/C:/...." " What the hell's going on? Why is it this difficult to access these files? All the examples I've found use the first method (relative path to file with no beginning slash) but for me, this doesn't work. I have tried making a new file and getting the absolute path to find out what directory it thinks it's looking in and it said the path was "C:/Users/<My Name>/Computing/Space Invaders/resources/image.png". Seriously, what the hell is going on?
ClassLoader.getSystemClassLoader().getResourceAsStream("spaceinvaders/resources/invader.png")
What do I do if I need it as a string? The SpriteSheet class doesn't allow for transparency unless you use a string. [editline]24th January 2011[/editline] Ah shit, I got it to work. The SpriteSheet works with the resources/image.png format but the other images didn't - I discovered a class in slick called ResourceLoader which loads resources as streams (and works with slick).
I don't get why people put assets/resources into their packages... make an additional folder in your project and put files into it.. then use "<foldername>/path/to/file.png" etc...
[QUOTE=Borsty;27631674]I don't get why people put assets/resources into their packages... make an additional folder in your project and put files into it.. then use "<foldername>/path/to/file.png" etc...[/QUOTE] Because it's easier to distribute just one executable jar file instead of tons of folders and files and it's pretty hard to load non-internal resources from an applet.
[QUOTE=Robber;27630973]ClassLoader.getSystemClassLoader().getResourceAsStream("spaceinvaders/resources/invader.png")[/QUOTE] It's better to use the classloader associated with an actual class in the application, rather than hard-coding use of the system class loader, so that it'll work correctly even when the module is being accessed via some alternate classloader instead. You can also call getResource() and getResourceAsStream() on a Class instance (as opposed to a ClassLoader), which lets you use relative paths based on the class's package. So if the "spaceinvaders" package has something like a ResourceManager class, its methods can use [cpp]getClass().getResourceAsStream("resources/invader.png")[/cpp] to load the file "spaceinvaders/resources/invader.png" using an appropriate classloder, without needing to hard-code any particular package name or classloader.
Sorry, you need to Log In to post a reply to this thread.