title: JavaFX native packaging - bizarre error with initialising static variables
body: <p>I'm working on a JavaFX project in Netbeans that's currently around 3000 lines long, and I've packaged as an .exe regularly for testing but never come across an issue like this curious incident. </p>
  
  <p>When packaging natively as a Windows .exe file, I found that after installing the .exe and launching my program I was getting a popup saying "<strong>Class {mypackage}/{mymainclass} not found.</strong>" followed by "<strong>Failed to launch JVM.</strong>"</p>
  
  <p>Launching the program in Netbeans, packaging as an .exe, and launching the .jar in Powershell with "java -jar {app}.jar" all gave <em>absolutely no errors</em>. Even the .jar inside the installed application folder was fine, with no errors on the command line.</p>
  
  <p>After a few hours of trawling through git commits and packaging natively, I managed to trace the issue down to a single line of code:
  <code>private static ComboBox choices = new ComboBox();</code></p>
  
  <p>When I initialise the ComboBox in an initialiser, the program magically works after being installed from an .exe:</p>
  
  <pre><code>private static ComboBox choices;
  {
      choices = new ComboBox();
  }
  </code></pre>
  
  <p>However, when I use a static initialiser (by placing <code>static</code> in front of the first curly brace), even though Netbeans states "initialiser can be static", I get the same error as before.</p>
  
  <p>This is puzzling, because Java is obviously fine with the code itself, but after it's been through the native packager it will not launch. I've used plenty of similar lines of code to initialise static variables in other classes, with no ill effects.</p>
  
  <p>I tried adding a similar line to the main class: <code>private static CheckBox chkbox = new CheckBox();</code></p>
  
  <p>It caused the exact same error after the .exe was installed (as before, the .jar was fine), but when I cut-and-pasted the line to a different class, it had no effect.</p>
  
  <p>Interestingly, my main class already has a static boolean that is initialised in the same way that the ComboBox and CheckBox were: <code>private static boolean someBool = true;</code> but the boolean causes no problems.</p>
  
  <p>Can anyone explain the reason behind this? Thank you.</p>
  
  <p>Edit: I also tried packaging on a different machine, but it stopped working after the same git commit. 
  I'm using version 1.8.0_144 of the JDK and JRE.</p>
  
  <p>Edit 2: here is the main class of a simple example that produces the error.</p>
  
  <pre><code>package testproject;
  
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.CheckBox;
  import javafx.scene.layout.StackPane;
  import javafx.stage.Stage;
  
  public class MainApplication extends Application {
  
      private static CheckBox testCheckBox = new CheckBox(); // Causes error after launching from installed .exe
  
      /* UNCOMMENT THIS SECTION AND REMOVE THE " = new CheckBox()" ABOVE TO FIX THE ERROR
      {
          testCheckBox = new CheckBox();
      }
       */
  
      @Override
      public void start(Stage primaryStage) {
          StackPane root = new StackPane();
          Scene scene = new Scene(root, 300, 250);
  
          primaryStage.setTitle("Empty window");
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  
  }
  </code></pre>