I am sure it’s a basic question , I just can’t figure it out
I am trying to load bunch of images located in images subfolder of my project
here is my code
package com.ieml.swt.diploma;
import java.io.InputStream;
public class loadTest
{
public static void main(String[] args)
{
System.out.println(getResourceImage("marks.png"));
}
public static InputStream getResourceImage(String fileName) {
return loadTest.class.getResourceAsStream("./images/" + fileName);
}
}
I have a separe folders for src and class files
so .java file is located in src/com/ieml/swt/diploma folder
and .class file under bin/com/ieml/swt/diploma folder
files I’m trying to load here do existt in the “loadTest/images” subfolder loadTest is my project’s root directory
it justs print s null like it’s doesn’t load this file
am I missing something here ?
This will look for the images under
bin/com/ieml/swt/diploma/images, i.e. relative to the.classfile. If you leave out the dot in the beginning, it will look inbin/images.But whatever you do,
Class.getResourceAsStream()is for loading resources from the classpath, i.e. inside thebinfolder. There is no way to access anything outside, and it wouldn’t make much sense anyway, since it’s designed to work for JAR files as well. Usually, you actually put the resources in thesrcfolder and copy them tobinduring the build (eclipse will do this automatically).