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()
.getResourceAsStream("com/linar/java2com/plus.gif");
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()
.getResource("com/linar/java2com/plus.gif");
ImageIcon image = (new ImageIcon(url));
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- What are class variables in Java?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are local variables in Java?
- How to Use Updatable ResultSet in JDBC?
- How to Use JDBC Java to Create Table?
- Why final variable in Enhanced for Loop does not act final?