1

I have images folder under src folder(src/images). when i run program in eclipse, the program runs good but when i export runnable jar file and try to run this, i see error. I write in below:

java.io.FileNotFoundException: src\images\test2.bmp (The system cannot find the
path specified)

My program:

public class FirstSWTExample {
public static void main(String[] args) {
  .
  .
  saveImage();
  Image image=new Image(display, "src/images/test2.bmp");
    shell.setImage(image);
  }
public static void saveImage() {
    String s="....";
    byte[] dataCustImg = Base64.decode(s.getBytes());

    try {

        OutputStream stream = new FileOutputStream("src/images/test2.bmp");

        stream.write(dataCustImg);
        stream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

I think jar file not found images folder. when i unzip jar file, the images folder not found. How can i solve my problem? i am beginner in java sorry if my question is easy.

Fahim
  • 384
  • 5
  • 20
  • Are you try this?`ImageIO.read(getClass().getResource("/images/test2.bmp"));` – David Pérez Cabrera May 23 '16 at 10:58
  • Thnaks. but how can set this to my shell? I am new in java – Fahim May 23 '16 at 11:03
  • When you run your program in eclipse, there is a relative directory `src` and your program uses it. If you want to use a jar resource, you must to load as `getClass().getResource(PATH)` but you can change it, look this: http://stackoverflow.com/questions/2797367/write-to-a-file-stream-returned-from-getresourceasstream – David Pérez Cabrera May 23 '16 at 11:07
  • Thanks. I write URL imgPath = FirstSWTExample.class.getClassLoader().getResource("/images/test2.bmp"); but imgPath is null. why? – Fahim May 23 '16 at 11:22
  • your imagen is Jar inside or outside? if it is outside, you must to consider smruti ranjan awnser – David Pérez Cabrera May 23 '16 at 11:23
  • I don't understand what is your mean of inside or outside? – Fahim May 23 '16 at 11:26
  • You can distribute the image inside the jar (the distributed jar contains all resources) or outside: a directory with the jar and other subdirectories with the images/resources? – David Pérez Cabrera May 23 '16 at 11:28
  • @DavidPérezCabrera Please send your first answer that i accept your answer. thanks – Fahim May 24 '16 at 05:31

2 Answers2

0

So you are doing this

"src/icon.png"

And forgot the package. So this is what I've tried

"src/myPackage/icon.png"

And it works.enter image description here

-1

The problem might be with file path.

This is probably not the best solution but you can try using absolute path

"C:\\projectName\\src\\images\\test2.bmp"
smruti ranjan
  • 741
  • 2
  • 9
  • 23