Demonstrates a Java/SWT application with embedded SWT.Browser compiling as native image via GraalVM.
- Provide solutions for communicating between a webpage embedded in an
SWT.Browserwidget and the hosting Java application. - Solve compiling the project including its dependencies into a native binary.
Clone the project and open it in IntelliJ.
Navigate to the App class.
Click the green Play button besides the main() method.
Add VM Options feature and add the parameter -XstartOnFirstThread.
The webpage which is loaded into the SWT.Browser is located in index.html.
It contains anchor tags with special links starting with dq://.
A LocationListener is registered on the SWT.Browser reacting on navigation events from these links.
A global FocusManager is instructed to give keyboard focus to text fields with certain names contained in certain panels.
The values of Properties from the Java Document can also flow into the webpage.
This is achieved by registering a DocumentListener and then executing a Javascript function defined in index.html.
This function tries to find a DOM element with an id matching the Property name.
The .innerHTML of the DOM element is then set to the value of the Property.
This project supports building with GraalVM Native Image.
It uses the Maven Native Image plugin in the POM profile native.
Follow the instructions for setting up GraalVM and Native Image.
Then you can build via:
mvn -Pnative -DskipTests packageThe native executable is found at target/maven-native-image-swt.
The native image compilation does not process the Java sources directly.
Instead, a regular JAR with JVM bytecode is produced first.
This JAR also contains resources.
The compiler options are passed via a special set of resources.
They control how the .class files and resources are processed.
Comprehensive information is available in this Medium article.
Normally, a reachability analysis would exclude any dead code.
But for many SWT classes, this would result in excluding important code.
This is where the reflection and JNI configuration comes into play and prevents that.
Also, many resources such as the native libraries contained in the SWT JAR are preserved and end up on the final native image.
pom.xml currently specify the macOS version of SWT only.