Adding Your Swing Application
This page covers two things:
-
Make your Swing JAR (and its dependencies) visible to SwingBridge using one of two loading strategies — the
applibsdirectory or aswing-app-jar-list.conffile. -
Create a Vaadin Flow view that wraps the Swing application’s
mainclass.
The instructions apply equally whether you started from the skeleton starter (Getting Started) or set up the project from scratch (Installation).
JAR Loading Strategies
SwingBridge needs to load your Swing application from a separate classloader so that each user session gets an isolated AppContext. There are two ways to provide it the JARs.
applibs directory |
swing-app-jar-list.conf |
|
|---|---|---|
When to use | Default. You drop JARs into a directory and SwingBridge picks them all up. | You want explicit control over the order and identity of each JAR (useful for hand-curated classpath ordering, or for loading from a non-standard location). |
Format | Any number of | One JAR path per line. Lines starting with |
Default location |
|
|
Override |
| Paths inside the file resolve against |
|
Note
|
The |
Using the applibs Directory
Copy your Swing application’s main JAR and all its dependency JARs into applibs. All .jar files in the directory become part of the SwingBridge classpath at runtime.
Source code
filesystem
my-swingbridge-project/
├── pom.xml
├── applibs/
│ ├── my-swing-app.jar
│ ├── dependency-a.jar
│ └── dependency-b.jar
└── src/...To use a custom directory, pass -Dapplibs.dir=<path> on the command line or configure it under <systemPropertyVariables> in your pom.xml (see Installation).
Using swing-app-jar-list.conf
Place a file named swing-app-jar-list.conf at the classpath root (src/main/resources/swing-app-jar-list.conf in a Spring Boot project) listing one JAR path per line:
Source code
# My Swing application's classpath (order matters)
${maven.multiModuleProjectDirectory}/my-app/target/my-app.jar
${maven.multiModuleProjectDirectory}/libs/legacy-utils.jar
/opt/shared-libs/customer-shared.jarPath resolution rules:
-
Lines beginning with
#and blank lines are skipped. -
The placeholder
${maven.multiModuleProjectDirectory}expands to the system property of the same name (set automatically by Maven for multi‑module builds). -
Absolute paths are kept as‑is.
-
Relative paths resolve against
-Dswingbridge.jarlist.baseDir=<path>if set, otherwise againstuser.dir(the JVM’s working directory). -
Order is preserved on the classloader, so place override JARs ahead of the JARs they shadow.
Create a Vaadin View
Wrap the Swing application’s main class in a Vaadin view by passing its fully qualified name to [classname]`SwingBridge`:
Source code
Java
@Route("myapp")
public class MyAppView extends VerticalLayout {
public MyAppView() {
add(new SwingBridge("com.mycompany.swingapp.MySwingAppMain"));
setSizeFull();
}
}Start the server:
Source code
terminal
mvn spring-boot:runNavigate to http://localhost:8888/myapp to see the application. If you configured a custom port (the Spring Boot default is 8080), navigate to that port instead.
Constructor Variants
[classname]`SwingBridge` provides three constructors for the common cases:
Source code
Java
// 1. Main-class FQN, default classloader (uses the applibs directory)
new SwingBridge("com.mycompany.swingapp.MySwingAppMain");
// 2. Main-class FQN with command-line arguments forwarded to main()
new SwingBridge("com.mycompany.swingapp.MySwingAppMain",
new String[] { "--config", "production" });
// 3. Main-class FQN with a custom classloader supplier
new SwingBridge("com.mycompany.swingapp.MySwingAppMain",
() -> SwingBridgeRunner.createClassLoader(
SwingBridgeRunner.loadSwingAppJarList(),
getClass().getClassLoader()));The third form is the most explicit — it pairs the Swing app with a JAR list (or a directory load) of your choice. For example, to use the swing-app-jar-list.conf file described above, subclass [classname]`SwingBridge` and override the supplier:
Source code
Java
public class MyApp extends SwingBridge {
public MyApp() {
super("com.mycompany.swingapp.MySwingAppMain");
}
@Override
protected Supplier<URLClassLoader> createClassLoaderSupplier() {
return () -> SwingBridgeRunner.createClassLoader(
SwingBridgeRunner.loadSwingAppJarList(),
getClass().getClassLoader());
}
}Next Steps
-
Tune behaviour with system properties (frame rate, error reporting): Configuration.
-
Public API surface and lifecycle hooks (
beforeInit,afterInit): Reference. -
Run and debug from your IDE: Running and Debugging.