getClass().getResourceAsStream(path)
will start its file search from the location of the calling class. So by using this path "imgspic1.jpg"
, you're saying this is your file structure
srcmyProjectguiimgspic1.jpg
To have the search traverse back, you need the extra separator before imgs
. So
"imgspic1.jpg"
Also, I think when you use a back slash as separator, you need to escape it. So
"\imgs\pic1.jpg
Or just use forward slash
"/imgs/pic1.jpg
Another option is to use a class loader, that will search from the root, where you don't need the beginning separator
getClass().getClassLoader().getResourceAsStream("imgs/pic1.png");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…