Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT Browser widget: html source inside jar?

Tags:

java

jar

swt

I want to implement a help system for my tiny SWT desktop application.

I've thought about a SWT browser widget containing a single html markup page and a set of anchors to navigate (there are only very few things to explain).

Everything works fine, but how do I load the html file from a jar?

I know aboutgetClass().getClassLoader().getResourceAsStream("foo");, but what is the best practice when reading from the input stream? The answer to Load a resource contained in a jar dissuades using a FileInputStream.

Thanks in advance

like image 672
f4lco Avatar asked Jun 20 '26 12:06

f4lco


2 Answers

Well, I found a rather simple solution that obviously just works:

InputStream in = getClass().getClassLoader().getResourceAsStream("html/index.html");
Scanner scanner = new Scanner(in);
StringBuffer buffer = new StringBuffer();
while(scanner.hasNextLine()) {
    buffer.append(scanner.nextLine());
}

browser.setText(buffer.toString());
like image 61
f4lco Avatar answered Jun 23 '26 01:06

f4lco


i tend to use commons-io for such a task giving me simple abstraction methods like IOUtils.toString(InputStream in); and leaving the choice of best implementations to the able people at apache ;)

commons-io: http://commons.apache.org/io/

apidocs: http://commons.apache.org/io/api-release/index.html

like image 22
fasseg Avatar answered Jun 23 '26 02:06

fasseg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!