I can put a file into the META-INF folder easily (just change the type to ZIP and then move it there) but how should I do to load the file in the program?
I’m fairly inexperienced with loading files and don’t even know how to load something from a folder lying at the same place as the JAR file, is there a difference to this compared to loading something from the META-INF?
If possible I’d like to be able to put folders in the META-INF folder and then load files from those folders (it’d make file handling a bit easier for me)
@junkdog
I’m sorry. First of all I don’t quite understand that code you linked to and also I’m not interested in the Manifest file. I want to get, say, an image “ball.png” which lies in the META-INF map (or anywhere else in the JAR file)
I use the following code to load Native files from a folder which lies parallel to the JAR file (they are in the same folder). Let’s say I want to move that “nat”-folder into the JAR file so that I only have to keep track of one file
public static void load() {
String system = System.getProperty("os.name");
String path = null;
path = new File("nat").getAbsolutePath(); //This here is where the path into
//the folder is set right?
if(path == null) {
System.out.println("Couldn't load natives!");
return;
}
path += "\\";
if (system.contains("Windows")) {
System.setProperty("org.lwjgl.librarypath", path + "Windows");
}
else if (system.contains("Mac")) {
System.setProperty("org.lwjgl.librarypath", path + "Mac");
}
else if (system.contains("Linux")) {
System.setProperty("org.lwjgl.librarypath", path + "Linux");
}
}
A jar file is an archive that you need to treat differently from the normal file system. You acccess resources inside a jar through a classloader. If you put the image in /nat inside the jar file I think this should work to load it:
ClassLoader loader = this.getClass().getClassLoader();
java.net.URL imageURL = loader.getResource("/nat/image.png");
ImageIcon image = new ImageIcon(imageURL);
@Grunnt
I tried it. Afraid it didn’t work
This has turned out to be a lot harder than I thought it would be
Ok, I got it working.
I can load an image like this:
public BufferedImage loadImage(String path){
java.net.URL imgURL = getClass().getResource(path);
if(imgURL != null){
try {
return ImageIO.read(imgURL);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
else{
System.err.println("Couldn't find file: " + path);
return null;
}
}