Skip to content

Demonstrate embedding a Java/SWT application using SWT.Browser in a native image

License

Notifications You must be signed in to change notification settings

stippi2/MavenNativeImageSWT

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Native Image SWT Demo

Demonstrates a Java/SWT application with embedded SWT.Browser compiling as native image via GraalVM.

Purpose

  • Provide solutions for communicating between a webpage embedded in an SWT.Browser widget and the hosting Java application.
  • Solve compiling the project including its dependencies into a native binary.

Running the app

Clone the project and open it in IntelliJ. Navigate to the App class. Click the green Play button besides the main() method.

⚠️ On macOS, this should likely fail on the first try. But it created a Run-Configuration which you can now edit. Use the Add VM Options feature and add the parameter -XstartOnFirstThread.

Repo layout

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.

Native image

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 package

The native executable is found at target/maven-native-image-swt.

Native image compiler configuration

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.

⚠️ The current native-image configuration only supports native macOS images. The pom.xml currently specify the macOS version of SWT only.

About

Demonstrate embedding a Java/SWT application using SWT.Browser in a native image

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 89.8%
  • HTML 10.2%