JExplorer Programmer’s Guide

Version: 3.0

  1. Introduction
  2. Requirements
  3. Architecture
  4. Browser Mode
  5. Hello World
  6. Loading URL
  7. Loading HTML
  8. Pop-ups
  9. JavaScript
  10. JavaScript Dialogs
  11. User-Agent
  12. DOM API
  13. Logging
  14. Technical Support

1. Introduction

1.1. About This Document

This document represents a developer's guide to JExplorer library. Here you can find all required information regarding the use of JExplorer library in your Java application.

1.2. About JExplorer

JExplorer is a Java library that allows integrating with installed in operating system MS Internet Explorer web browser control into Java application. Integration with MS Internet Explorer is implemented via system native MSHTML library. With JExplorer you can embed MS IE web browser control to display modern web pages.

1.3. Examples and Demo

Almost all the examples in this guide can be found in the samples directory of JExplorer Distribution Package. The samples you can also find online.

JExplorer Web Start Online Demo you can find at http://www.teamdev.com/jexplorer#onlinedemo

1.4. Package

The structure of JExplorer Distribution Package (jexplorer-3.0.zip) is the following:

lib/
    jexplorer-3.0.jar    // JExplorer library
samples/                    // API samples
doc/javadoc/                // Public API JavaDocs
doc/guide/                  // Programmer's Guide
demo/
    jexplorerdemo.jar       // Demo application library
    jexplorerdemo.bat       // Executable *.bat to run Demo application on Windows
Readme.txt                  // Readme file
License agreement.txt       // License agreement

1.5. Installation

To use JExplorer library in your Java application you need to include all the JAR files from the lib directory of JExplorer Distribution Package. Your application class path should look like:

java -classpath jexplorer-3.0.jar <your-application-class>

The jexplorer-3.0.jar library contains native binaries. At the first run JExplorer extracts the binaries from the JAR file into user's temp folder. So, Java application must have rights to write to user's temp directory.

1.6. Licensing

JExplorer library requires a valid license (teamdev.licenses) file for operations. You can request a fully-functional 30-day evaluation license or purchase a commercial license for JExplorer library.

30-day Evaluation license

When you request a 30-day evaluation license file, you get an email message with the link that you can use to download a license.jar file. To enable the evaluation license you need to include this license.jar into your application class path.

Commercial License

When you purchase a license file, you get two JARs: development.jar and runtime.jar. You need to include one of these JARs into your application class path to enable the license. The difference between Development and Runtime license is the following:

– you need to use the Development license (development.jar) for development purposes only. The number of licenses should be equal to the number of developers who use JExplorer in development. For example, if you have 10 developers involved into your project and only 4 of them work with JExplorer, you should purchase 4 licenses only.

– the Runtime license (runtime.jar) should be used when you distribute your software with JExplorer enclosed. For example, you can include the runtime.jar file into class path of your application production version. You can deploy the Runtime license with your application to unlimited number of end users.

License Information

In order to get information about the license, use the "teamdev.license.info" system property. The following code demonstrates how to enable license info printing in your application.

System.setProperty("teamdev.license.info", "true");
Browser browser = new Browser();

2. Requirements

JExplorer integrates with installed in operating system MS Internet Explorer.

  1. Microsoft Windows 7, 8, 8.1, Server 2008/2012, 32-bit and 64-bit.
  2. MS Internet Explorer 6, 7, 8, 9, 10, 11. Both 32-bit and 64-bit.
  3. Oracle JDK/JRE 1.7.x and higher, 32-bit and 64-bit.

3. Architecture

JExplorer supports multi-process architecture and runs native code in separate JExplorer32.exe or JExplorer64.exe processes depending on JVM architecture.

By default each Browser instance is initialized with a new BrowserContext. For example:

new Browser() => new Browser(new BrowserContext())

BrowserContext is responsible for communication between Browser instance and its corresponding JExplorer.exe native process. Two Browser instances with different context, will use two different JExplorer.exe processes. For example:

Browser browserOne = new Browser(new BrowserContext()); // Starts JExplorer32.exe
Browser browserTwo = new Browser(new BrowserContext()); // Starts JExplorer32.exe

When you need to run two Browser instances in same native process (e.g. to share session cookies), you can use the following approach:

BrowserContext context = new BrowserContext();
Browser browserOne = new Browser(context); // Starts JExplorer32.exe
Browser browserTwo = new Browser(context); // Use already started JExplorer32.exe

JExplorer32.exe process is terminated automatically once you dispose the latest Browser instance that belongs to the process.

By default parent and child Browser instances are running in same JExplorer.exe process.

4. Browser Mode

MS Internet Explorer can display web pages using different versions of Internet Explorer via Browser Mode feature. In JExplorer by default web engine is configured to display web pages in MS IE 7 Browser Mode.

In case when MS Internet Explorer 11 is installed in your environment, you might want to enable MS IE 11 Browser Mode in JExplorer. To configure Browser Mode use the BrowserFeatures.enableBrowserMode(BrowserMode mode) method. For example:

// Enable MS IE 11 Browser Mode
BrowserFeatures.enableBrowserMode(BrowserMode.IE11);
Browser browser = new Browser();

Browser Mode is a global setting that is stored in Windows Registry. Once you set Browser Mode, all Browser instances will use it. To remove this setting and enabled default behavior (MS IE7 Browser Mode) use the BrowserFeatures.disableBrowserMode() method.

Browser Mode must be configure before you create any Browser instance.

5. Hello World

Once you configure Java project, include all JExplorer and license JAR files into your application class path, you should be able to run your first program with JExplorer:

import com.teamdev.jexplorer.Browser;
import javax.swing.*;
import java.awt.*;

public class HelloWorldSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame("JExplorer: Hello World");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.setContent("<html><body><h1>Hello world!</h1></body></html>");
    }
}

This simple example above shows how to create a Browser component, embed it into Swing container, display it and load the "<html><body><h1>Hello world!</h1></body></html>" HTML string directly into the Browser.

When you run this sample you should see the following output:

Hello world example output

6. Loading URL

To load web page by its URL use the following method:

browser.navigate("http://www.google.com");

This method doesn't wait until web page is loaded completely. To find out when web page is loaded completely use the NavigationListener.mainDocumentCompleted(Browser browser, String url) event. For example:

browser.addNavigationListener(new NavigationAdapter() {
    @Override
    public void mainDocumentCompleted(Browser browser, String url) {
        // Web page is loaded completely
    }
});
browser.navigate("http://www.google.com");

To block current thread execution and wait until web page is loaded completely use the following approach:

Browser.invokeAndWaitLoadMainFrame(browser, new Runnable() {
    @Override
    public void run() {
        browser.navigate("http://www.google.com");
    }
});
// Web page is loaded completely

7. Loading HTML

To load HTML content from string use the following method:

browser.setContent("<html><body><h1>Hello world!</h1></body></html>");

This method doesn't wait until HTML content is loaded and displayed completely. To find out when it's loaded see Loading URL.

8. Pop-ups

By default JExplorer supports and displays popup windows. To change default behavior you can register your own PopupHandler implementation. For example:

browser.setPopupHandler(new PopupHandler() {
    @Override
    public PopupContainer handlePopup() {
        return new PopupContainer() {
            @Override
            public void insertBrowser(final Browser browser) {
                final JFrame frame = new JFrame();
                frame.getContentPane().add(browser, BorderLayout.CENTER);
                frame.setSize(800, 600);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                frame.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        browser.dispose();
                    }
                });

                browser.addDisposeListener(new DisposeListener() {
                    @Override
                    public void onDispose(Browser browser) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                frame.setVisible(false);
                                frame.dispose();
                            }
                        });
                    }
                });
            }
        };
    }
});

9. JavaScript

You can execute any JavaScript code on the loaded web page and get result of JavaScript code execution. The following code demonstrates how to execute JavaScript code and get return value:

JSValue returnValue = browser.executeScript("document.title");
if (returnValue.isString()) {
    System.out.println("Title = " + returnValue.getString());
}

The result of JavaScript execution is represented as JSValue. Using JSValue.isXXX() methods you can determine what value type it contains (e.g. String, Boolean, Number, Null). To extract value from JSValue use corresponding JSValue.getXXX() method.

Don't call browser.executeScript() in Java UI/EDT thread. JavaScript code you are trying to execute may cause Alert or other JavaScript UI dialog to be shown. In this case you might receive a deadlock.

10. JavaScript Dialogs

JExplorer supports and displays JavaScript dialogs such as Alert or Confirmation by default. Using DialogHandler you can handle JavaScript dialogs in your application. The following code demonstrates how to suppress all JavaScript dialogs:

browser.setDialogHandler(new DialogHandler() {
    @Override
    public int showDialog(String title, String text, int type) {
        // Suppress dialog and close it right away without displaying UI
        return DialogHandler.IDOK;
    }
});

In your DialogHandler implementation you can display your own customized dialogs.

11. User-Agent

JExplorer API allows modifying user-agent string sent to a web server when you load web page. The following code demonstrates how to do this:

Browser browser = new Browser();
browser.setUserAgent("MyUserAgent");
browser.navigate("http://whatsmyuseragent.com/");

To get default MS IE user-agent string use the Browser.getDefaultUserAgent() method.

12. DOM API

JExplorer provides DOM API that allows working with DOM of the loaded web page. You can access document, go through DOM structure, read HTML elements and their attributes, modify DOM structure, etc.

The following sample demonstrates how to access document of the loaded web page, find all A elements on the web page and print their text content:

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.dom.DOMDocument;
import com.teamdev.jexplorer.dom.DOMNodeList;
import com.teamdev.jexplorer.event.NavigationAdapter;

import javax.swing.*;
import java.awt.*;

public class DOMGetElementsSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addNavigationListener(new NavigationAdapter() {
            @Override
            public void mainDocumentCompleted(Browser browser, String url) {
                DOMDocument document = browser.getDocument();
                DOMNodeList elements = document.getElementsByTagName("a");
                int length = elements.getLength();
                for (int i = 0; i < length; i++) {
                    System.out.println("Text Content: " + elements.item(i).getTextContent());
                }
            }
        });
        browser.navigate("http://www.google.com");
    }
}

13. Logging

JExplorer logging system is based on Java Logging, so you can configure JExplorer logging using standard Java Logging API. For example, in order to access different JExplorer Loggers and specify the SEVERE log level you can use the following code:

import com.teamdev.jexplorer.LoggerProvider;
import java.util.logging.*;
...
LoggerProvider.getBrowserLogger().setLevel(Level.SEVERE);
LoggerProvider.getIPCLogger().setLevel(Level.SEVERE);
LoggerProvider.getProcessLogger().setLevel(Level.SEVERE);

The BrowserLogger is used to log browser messages.
The IPCLogger is used to log IPC (Inter-Process Communication) messages.
The ProcessLogger is used to log messages that are come from JExplorer.exe process.

By default the log level for all JExplorer loggers is set to Level.ALL.

Redirect logging to a file

To redirect all log messages of BrowserLogger to the "D:/jexplorer.log" file, you can use the following way:

Logger logger = LoggerProvider.getBrowserLogger();
FileHandler fileHandler = new FileHandler("D:/jexplorer.log");
logger.addHandler(fileHandler);
fileHandler.setFormatter(new SimpleFormatter());

You can redirect log messages of any JExplorer logger: BrowserLogger, IPCLogger or ProcessLogger.

14. Technical Support

Support Site

You can find more information about how to use JExplorer library at JExplorer Support Site.

Support Forum

If you would like to discuss any topics related to JExplorer, please visit our Support Forum.

Samples

You can find JExplorer samples on the following resource: https://sites.google.com/a/teamdev.com/jexplorer-support/samples

Feedback

If you have any questions regarding JExplorer, please e-mail us at: jexplorer-evaluation@teamdev.com