Include HTML documentation inside .jar and open it from a plugin


#1

Hi all,

I’m quite new in Java coding world and when I tried to include some local formatted documentation (HTML file in my case) for my plugin, I didn’t find any example. So after trying many options, I came up with this code (below). Please comment if you think it is not appropriate, e.g. not compatible with Linux or Mac, as I tested it only with Win 7.

try {
	// Open doc from HTML file in project resources folder

	// relative path of doc file
	String relPath = "resources/Doc.html";
	
	// path to running jar...
	String jarpath = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toString()).getParent();

	// Define full output path
	String outDoc = jarpath.toString().replace("file:"+File.separator, "") + File.separator + "Doc.html";
	
	// Write HTML doc in jar's folder
	copyResFileToLocal(relPath, outDoc);
	
	BrowserLauncher.openURL("file:///"+outDoc);
	
} catch (IOException e) {
	e.printStackTrace();
	IJ.showMessage("Documentation not found");
}

… and …

 public void copyResFileToLocal(String inpath, String outpath) throws IOException {
        FileOutputStream out = null;

	InputStream in = getClass().getResourceAsStream(inpath);
	
	out = new FileOutputStream(outpath);
	
	byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
	
	in.close();
	out.close();
}

Thanks!


#2

Hey @pmascalchi,

If the html is packaged in the jar, you can save some work using 'getResource’
Bigdataviewer has an example of this:

This same file (HelpDialog) is itself a frame that displays the html page using swing, so you may be able to avoid copying the file elsewhere and having a browser display it.

Hope this is helpful,
John


#3

Hi @bogovicj,

Thanks for this quicker and simpler method!

Best,
Patrice