I am following along with a tutorial book on LWJGL3 and I tried to make my own image loading code (The book code does not use the STB library) and it works perfectly fine when the project is run normally (IntelliJ IDE, run directly from the build folder), It loads the image from into the ByteBuffer and stbi_load_from_memory
works fine, but as soon as it is compiled and put in a jar file it just stops working, Even tho the image was successfully loaded into the ByteBuffer, as if the stbi_load_from_memory
function just returns null if it's in a Jar.
The code:
public static ByteBuffer loadImage(String fileName, IntBuffer w,
IntBuffer h, IntBuffer comp) throws Exception
{
ByteBuffer image;
//the class name is Utils
InputStream imageFile =
Utils.class.getResourceAsStream(fileName);
//The image data gets put into the byte array no matter if
//its a jar or not
byte[] imageData = new byte[imageFile.available()];
imageFile.read(imageData);
ByteBuffer imageBuffer =
BufferUtils.createByteBuffer(imageData.length);
imageBuffer.put(imageData);
imageBuffer.flip();
//imageBuffer is the right size and has the right contents
//This is where everything fails if its in a jar,
//image is set to null and the Exception is thrown
image = stbi_load_from_memory(imageBuffer, w, h, comp, 0);
if(image == null)
{
throw new Exception("Failed to load image: " + fileName);
}
return image;
}
It can't be a misspelled file name or recourse path, because it loads the data into the array properly no matter if it's a jar or not.
Additional Information:
Java version: 1.8.0_131 64bit
Processor architecture: amd64
IDE version (if needed for some reason): IntelliJ 2017.1.4 Ultimate
OS: Ubuntu 16.04 LTS 64 bit
Kernel: Linux 4.8.0-58-generic amd64
LWJGL version: 3.1.2
The project is made in Gradle 3.3
if required i will upload and post the link of the entire source code