JCEF (Java Chromium Embedded Framework) Browser does not load content; only blank screen on Eclipse

Viewed 665

I have been trying to get the Java version of Chromium Embedded Framework (JCEF) to work on Eclipse for some time. I am able to verify that the library files are working correctly, since if I run the included sample class files on the VM, the program runs and some webpage is displayed. However, if I run the program from Eclipse, the program will always display a blank window. I am able to verify that the library binary jcef_helper.exe is successfully run, but not matter how I link the .jar files and other library files, the webpage will not generate and there will always be a blank screen. I cannot pinpoint the issue here. I tried specifying path, adding the JCEF library path of my OS environment variables PATH field to no avail. I have followed the documentation, even sample files behave the same way when I have anything to do with compilation/ run. One note of interest, my Eclipse console will display this during run:

initialize on Thread[AWT-EventQueue-0,6,main]

Perhaps there is a thread issue?

Code in question:

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.cef.CefApp;
import org.cef.CefApp.CefAppState;
import org.cef.CefClient;
import org.cef.CefSettings;
import org.cef.browser.CefBrowser;
import org.cef.handler.CefAppHandlerAdapter;

public class WebV
{
    public static void main(String args[])
    {
        Dimension dispDimension = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int)Math.round(dispDimension.getWidth() / 2);
        int height = (int)Math.round(dispDimension.getHeight() / 2);

        CefApp.addAppHandler(new CefAppHandlerAdapter(null)
        {
            @Override
            public void stateHasChanged(CefAppState state)
            {
                if(state == CefAppState.TERMINATED)
                {
                    System.exit(0);
                }
            }
        });
        CefSettings settings = new CefSettings();
        settings.windowless_rendering_enabled = false;
        CefApp cefApp = CefApp.getInstance(settings);
        CefClient client = cefApp.createClient();
        CefBrowser browser = client.createBrowser("http://www.google.com", false, false);
        Component browserComponent = browser.getUIComponent();

        JFrame frame = new JFrame();
        frame.setTitle("WebV");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);

        frame.add(browserComponent);
        frame.setVisible(true);
    }
}

Tested with both Java 17 and Java 8. Run on Eclipse, with VM args:

-Djava.library.path=./bin/lib/win64

Any inupt would be greatly appreicated.

2 Answers

This answer was kindly provided by 'Yanovsky' at the JCEF forums. The simple example provided by the JCEF package is incomplete. We need to add a CefMessageRouter instance to our browser client. This is demonstrated in the detailed examples, but not the simple one. This code should be added before we create our CefBrowser instance and after we have created a CefClient object:

CefMessageRouter msgRouter = CefMessageRouter.create();
msgRouter.addHandler(new MessageRouterHandler(), true);
msgRouter.addHandler(new MessageRouterHandlerEx(client), false);
client.addMessageRouter(msgRouter);

For reference, this is my post in the JCEF forum: https://magpcss.org/ceforum/viewtopic.php?f=17&t=18834&sid=45381db639d1621f2f8b38b4d8848800

I just copied and pasted your code in eclipse and added the jars and ran the code and voila, google.com opened without any issues.

I didnt have to add any more new lines as suggested by mindoverflow.

The webpage was visible in the window after a few secs and not immediately. Here's what popped up after running the code. enter image description here

And in the eclipse console, I see this

initialize on Thread[AWT-EventQueue-0,6,main] with library path C:\path\to\dlls

Related