Dear Stackflow Developers first of all thanks a lot for helping me a lot. I am working on a project in java swing application in which client has to access the html files in swing GUI. i want to add Html files ,images and index file to jar of the project so that client can access html files without storing them in actual .Can the client unzip the html files from jar? please suggest me the best way . regards
Asked
Active
Viewed 708 times
2
-
1See also [this answer](http://stackoverflow.com/a/6373907/418556). – Andrew Thompson Oct 10 '12 at 10:20
2 Answers
4
You can use e.g.
ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));
In general, you can retrieve an InputStream in the following way:
InputStream is = this.getClass().getResourceAsStream("yourpackage/mypackage/file.html");
Then use the stream or URL to work with the files.

Andrew Thompson
- 168,117
- 40
- 217
- 433

StanislavL
- 56,971
- 9
- 68
- 98
3
You can access jar resources directly with Class.getResource()
. This also works in your development environment where resources are copied to the "output" directory (where your *.class files are generated)
For example, let's say you have an HTML file which is located in a jar at the path "/path/to/my/index.html", you can retrieve the URL to that file with
URL indexURL = MyClass.class.getResource("/path/to/my/index.html");
Assuming that MyClass
is a class of yours.

Guillaume Polet
- 47,259
- 4
- 83
- 117