Integrating Chromium as a web renderer in a java application with JCEF

In this tutorial we will integrate chromium in a java application. With this setup we can use the browser as a rendering engine instead of swt, swing or other ui frameworks. For this we will use JCEF (java chromium embedded) to integrate chromium into our java application.

Chromium is the engine behind the google chrome browser. It is maintained by google and is constantly updated. To render the browser component into a java application we use JCEF. JCEF is a wrapper around the CEF (chromium embedded framework). CEF is written in c++ and a wrapper around the chromium browser engine. It simplifies using the chromium engine. CEF is used in many commercial products for rendering the ui like spotify, steam or origin. JCEF renders the browser into a Swing application.

Setting everything up

At first we will setup JCEF. We have two options here. First we can use a prebuilt version of JCEF. Problem here is that this is a very old version. Second we can build JCEF on our own. For the simplicity of this tutorial we will use the prebuilt version of JCEF.

When using the pre built version, the files for WebGL support are missing. When you build everything from scratch there will be additional files (d3dcompiler etc.) in the build’s output folder for WebGL support.

The javascript engine in the prebuilt version is slow compared to the engine in newer releases (since chromium v41). So if you want the newest and fastest javascript engine then build chromium yourself.

Go to https://bitbucket.org/chromiumembedded/java-cef and download the jcef built under Quick Links -> Downloads. Once you have done that create a new java project and copy all related files, so that your project looks like mine.

I put everything jcef related under the folder jcef. Then add jcef.jar to build path (right click on jcef.jar -> Build Path -> Add To Build Path) and most importantly add the jcef folder to the java library path (right click project -> Properties -> Java Build Path -> Libraries Tab -> expand jcef.jar -> edit native library location -> set it to this project’s jcef folder)

After that we are ready to program!

Implementing a simple demo

Let’s implement a simple demo app that opens google in our window. Create a new java class called App and add the following content:

Now right click an run as a java application. A window opening http://www.google.com should appear.

Useful settings

You can use all chromium specific parameters with JCEF. You just have to add a string array with all the parameters you need, when instantiating the JCEF application.

final CefApp cefApp = CefApp.getInstance(new String[]{"-–disable-web-security"});

Be aware that adding these settings can be a securtiy threat when you load webpages you don’t own. Use these settings at your own risk!

If you want to load resources via XMLHTTPRequest locally via the file protocol, you have to add the parameter:

-–disable-web-security

If this is not enough for your application you can also add the parameter:

–-allow-file-access-from-files

Accessing the camera

If you want to allow access to your camera, you have to add the parameter:

–enable-media-stream

Disable pinch to zoom

When you are on a touch device and pinch, the browser will zoom all elements. This can be very annyoing as it can destroy your layout. To disable this behaviour add the the parameter:

–disable-pinch

Thank you for reading my article. I hope that i could give you a short introduction in how to embed chromium into a java application.