How to access a resource (image, xml or properties file) insider a jar file?

How to accessing a resource within a jar file?

You access some resources (images, xml file or properties file) that are inside a jar. For example, you can retrieve an image with the following way:

    InputStream in = this.getClass().getClassLoader()
    int c;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    while ((c = in.read()) != -1) {
        byteArrayOutputStream.write((char) c);

Here is another example using the getResource() method that takes an URL parameter to retrieve an image:

    URL url = this.getClass().getClassLoader()
    ImageIcon image = (new ImageIcon(url));



Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |