2

I have the loadImage function for getting images. It only does not work when I am trying to get the file from the getFile() function.

public BufferedImage loadImage(String img) {
    BufferedImage bimg = null;
    try {
        bimg = ImageIO.read(new File("res/" + img));
    } catch (Exception e1) {
        try {
            bimg = ImageIO.read(ClassLoader.getSystemResource("res/" + img));
        } catch (Exception e2) {
            try {
                bimg = ImageIO.read(getClass().getResource("res/" + img));
            } catch (Exception e3) {
                System.out.println("Cannot load image: " + img);
            }
        }
    }
    return bimg;
}

I have tried using the following function:

public File getFile(String file) {
    File f = null;
    try {
        f = new File("res/" + file);
    } catch (Exception e1) {
        try {
             f = new File(ClassLoader.getSystemResource("res/" + file).getFile());
        } catch (Exception e2) {
            try {
                f = new File(getClass().getResource("res/" + file).getFile());
            } catch (Exception e3) {
                System.out.println("Cannot load File: " + file);
            }
        }
    }
    return f;
}

These functions are in a jar lib and my other project want to get a file from the jar lib.

This is in my project trying to get config.cfg file from the jar lib

File file = files.getFile("config.cfg");

I have opened the jar and checked, if that the file exists inside the jar.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
8803286
  • 81
  • 2
  • 10

2 Answers2

1

You can use the Class.getResourceAsStream(String name). For example, if you have "/data/config.cfg" in the jar file, then you can open that file for reading using the following line:

InputStream is = this.getClass().getResourceAsStream("/data/config.cfg");
Mudassar
  • 3,135
  • 17
  • 22
0

It depends on your packaging.

Remember, that you don't have to pack your application as a jar file - it can be in a not packaged form, containing classes in separated directories (just like in a jar file but without it).

Getting a File in Java is like getting a file in the operation system you are running your application from. So if you had a "non-jar" form, then your code would work just fine. But if you have a JAR application, then your operating system would have to support operations on packaged files (jar is in fact ZIP archive) just like on not-packaged files. I have no knowledge of such case, however.

All in all - getting a file from resources (from your application's jar file) will not work as it should allow for the native file system to perform all the operations that are allowed for normal file (copy, paste, write, change attributes etc). Getting resources as stream is separate thing (and it will work in all cases).

EDIT: According to your comment, you have a JAR form. In that case, getting file as File from resources will not work at all.

silver est
  • 51
  • 6
Antoniossss
  • 31,590
  • 6
  • 57
  • 99