Snap package for Java (Swing and AWT) application crashes when it is executed

I'm trying to build a snap package for a Java (Swing and AWT) application. It allows to download (and manage) wallpapers automatically from the Internet and stores them locally. This is the snapcraft.yaml file:

name: wallpaperdownloader
version: 2.0
summary: Download and manage your favorite wallpapers fron the Internet
description:
  Wallpaperdownloader is a simple java based application for downloading wallpapers from the Internet
confinement: strict
apps:
  wallpaperdownloader:
    command: java -jar $SNAP/jar/wallpaperdownloader.jar
    plugs: [x11, network-bind]
parts:
  wallpaperdownloader:
    plugin: maven
    source: https://bitbucket.org/eloy_garcia_pca/wallpaperdownloader.git
    source-type: git

I built it without any problem (using Maven plugin) and snap package is created. Then, I install it using --devmode flag in order to bypass confinement (only for testing purposes) but it doesn't work. It creates a .wallpaperdownloader folder within /home/user with all the stuff and configuration needed to run, but it crashes before any GUI appears. This is the exception thrown:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
    at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
    at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
    at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
    at sun.font.SunFontManager$2.run(SunFontManager.java:431)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.font.SunFontManager.<init>(SunFontManager.java:376)
    at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
    at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
    at sun.font.SunFontManager.getInstance(SunFontManager.java:250)
    at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:264)
    at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1113)
    at javax.swing.JComponent.getFontMetrics(JComponent.java:1626)
    at javax.swing.text.PlainView.calculateLongestLine(PlainView.java:639)
    at javax.swing.text.PlainView.updateMetrics(PlainView.java:209)
    at javax.swing.text.PlainView.updateDamage(PlainView.java:527)
    at javax.swing.text.PlainView.insertUpdate(PlainView.java:451)
    at javax.swing.text.FieldView.insertUpdate(FieldView.java:293)
    at javax.swing.plaf.basic.BasicTextUI$RootView.insertUpdate(BasicTextUI.java:1610)
    at javax.swing.plaf.basic.BasicTextUI$UpdateHandler.insertUpdate(BasicTextUI.java:1869)
    at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:201)
    at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:748)
    at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
    at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
    at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:669)
    at javax.swing.text.JTextComponent.setText(JTextComponent.java:1669)
    at javax.swing.JFormattedTextField$AbstractFormatter.install(JFormattedTextField.java:948)
    at javax.swing.text.DefaultFormatter.install(DefaultFormatter.java:125)
    at javax.swing.text.InternationalFormatter.install(InternationalFormatter.java:285)
    at javax.swing.JFormattedTextField.setFormatter(JFormattedTextField.java:464)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:788)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:501)
    at es.estoes.wallpaperDownloader.window.WallpaperDownloader.initializeGUI(WallpaperDownloader.java:814)
    at es.estoes.wallpaperDownloader.window.WallpaperDownloader.initialize(WallpaperDownloader.java:426)
    at es.estoes.wallpaperDownloader.window.WallpaperDownloader.<init>(WallpaperDownloader.java:160)
    at es.estoes.wallpaperDownloader.window.WallpaperDownloader$1.run(WallpaperDownloader.java:149)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Thank you very much for your help.


Solution 1:

Replace command:

java -jar $SNAP/jar/wallpaperdownloader.jar

with run.sh.

Here is run.sh:

#!/bin/sh
# Not good, needed for fontconfig
export XDG_DATA_HOME=$SNAP/usr/share
# Font Config
export FONTCONFIG_PATH=$SNAP/etc/fonts/config.d
export FONTCONFIG_FILE=$SNAP/etc/fonts/fonts.conf
export HOME=$SNAP_USER_DATA
java -jar -Duser.home=$SNAP_USER_DATA $SNAP/jar/wallpaperdownloader.jar

"-Duser.home=$SNAP_USER_DATA" solved my problem.

Update: View a full example in the Snappy Playpen using a custom launcher for start-up.