I’m using the following method to load the native files from a folder “nat” next to the JAR file.
public static void load() {
String system = System.getProperty("os.name");
String path = null;
path = new File("nat").getAbsolutePath();
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");
}
}
If possible, I’d like to be able to put that folder inside the JAR and read it from there so that the program doesn’t rely on that folder being copied along with the JAR file. I’ve been reading images and text files from within a JAR with the following methods but I can’t seem to adapt that to reading natives.
public BufferedImage loadImage(String path){
URL myImageURL = FileLoader.class.getResource("/res/" + path);
try {
BufferedImage myImage = ImageIO.read(myImageURL);
return myImage;
} catch (IOException e) {
e.printStackTrace();
System.out.println("ERROR@FileHandler.loadImage(String path) - IOException with path: " + path);
return null;
} catch(IllegalArgumentException e){
e.printStackTrace();
System.out.println("ERROR@FileHandler.loadImage(String path) - IllegalArgumentException with path: " + path);
return null;
}
}
public String[] loadText(String path){
InputStream mySettingsStream = FileLoader.class.getResourceAsStream("/res/"+path);
String mySettingsString = "null";
StringBuilder build = new StringBuilder();
int i = 0;
try {
while((i = mySettingsStream.read()) != -1) {
build.append((char)i);
}
mySettingsString = build.toString();
return mySettingsString.split("\r\n");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}