You can embed any HTML application using the webkit -- the webkit version of the jvm is relatively up-to-date, for the last time i checked, there may be few functions missing, WebGL maybe.
To make it work, don't use file://
protocol, and it is overkill to create your own webserver. You just have to define your own protocl and declare it in java layers.
Examples are here: Registering and using a custom java.net.URL protocol
Here is my custom protocol implementation:
/**
* Register a protocol handler for URLs like this: <code>myapp:///pics/sland.gif</code><br>
*/
public class MyURLConnection extends URLConnection
{
protected MyURLConnection(URL url) {
super(url);
}
private byte[] data;
@Override
public void connect() throws IOException
{
if (connected)
{
return;
}
loadImage();
connected = true;
}
public String getHeaderField(String name)
{
if ("Content-Type".equalsIgnoreCase(name))
{
return getContentType();
}
else if ("Content-Length".equalsIgnoreCase(name))
{
return "" + getContentLength();
}
return null;
}
public String getContentType()
{
String fileName = getURL().getFile();
String ext = fileName.substring(fileName.lastIndexOf('.')+1);
switch(ext){
case "html":return "text/html";
case "jpg":
case "png":return "image/"+ext;
case "js": return "application/javascript";
}
return "text/"+ext;
// TODO: switch based on file-type "image/" + ext
}
public int getContentLength()
{
return data.length;
}
public long getContentLengthLong()
{
return data.length;
}
public boolean getDoInput()
{
return true;
}
public InputStream getInputStream() throws IOException
{
connect();
return new ByteArrayInputStream(data);
}
/**
* Reads all bytes from an input stream and writes them to an output stream.
*/
private static long copy(InputStream source, OutputStream sink)
throws IOException
{
long nread = 0L;
byte[] buf = new byte[8192];
int n;
while ((n = source.read(buf)) > 0) {
sink.write(buf, 0, n);
nread += n;
}
return nread;
}
private void loadImage() throws IOException
{
if (data != null)
{
return;
}
String fileName = getURL().getFile();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
copy(this.getClass().getResourceAsStream(fileName),byteArrayOutputStream);
data = byteArrayOutputStream.toByteArray();
}
public OutputStream getOutputStream() throws IOException
{
// this might be unnecessary - the whole method can probably be omitted for our purposes
return new ByteArrayOutputStream();
}
public java.security.Permission getPermission() throws IOException
{
return null; // we need no permissions to access this URL
}
}
public class MyURLHandler extends URLStreamHandler
{
@Override
protected URLConnection openConnection(URL url) throws IOException
{
return new MyURLConnection(url);
}
}
public class MyURLStreamHandlerFactory implements URLStreamHandlerFactory
{
public URLStreamHandler createURLStreamHandler(String protocol)
{
if (protocol.equals("myapp"))
{
return new MyURLHandler();
}
return null;
}
}
Then just register the protocol in your javafx startup:
public void start(Stage stage) {
URL.setURLStreamHandlerFactory(new MyURLStreamHandlerFactory());
// etc.
and use the url myapp://anyhost/yourfile.html
in your webkit (anyhost is not used, use anything you want or modify the protocol)
Edit:
I did it on my project (in progress) on github: you just have to launch the WebDesktop
main class. It uses resources of src/main/resources
https://github.com/pdemanget/blue-browser