Reputation: 393
When I launch my application in my development enviroment(Eclipse) it runs, however, when I try to export it to a runnable .jar-file, it gives me the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at mallApp.MainTwo.<clinit>(MainTwo.java:24)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:398)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:410)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:405)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:695)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:657)
at javafx.scene.control.Control.<clinit>(Control.java:99)
... 4 more
With "Caused by: java.lang.IllegalStateException: Toolkit not initialized" being the issue.
There are numerous threads here about similar issues, but none of them seem to be quite the same, and the fixes that I have tried do not work for me. My class extends Application and therefore has Application.launch(args) in the main method. This should initialize the Toolkit but it does not for some reason when exported to a .jar.
When I try to add the Toolkit in a different way, for example using JFXPanel or Platform.startup(Runnable)
, it gives me the opposite error, saying that Toolkit is already initialized.
public class MainTwo extends Application {
...
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage arg0) throws Exception {
model = new Main2Model();
c = model.getCompanyList();
primaryStage = arg0;
primaryStage.setTitle("MallApp");
primaryStage.centerOnScreen();
showMainView();
}
Upvotes: 3
Views: 3897
Reputation: 393
The problem was that I had initialized static javafx-attributes when i declared them. The issue was resolved and the JAR became runnable when I moved the initalization of the fields to the start-method as shown below:
Instead of having:
private static TextField total = new TextField();
private static ObservableList<String> items = FXCollections.observableArrayList();
public void start(Stage arg0) throws Exception {
model = new Main2Model();
c = model.getCompanyList();
primaryStage = arg0;
primaryStage.setTitle("MallApp");
primaryStage.centerOnScreen();
showMainView();
}
I have:
private static TextField total;
private static ObservableList<String> items;
@Override
public void start(Stage arg0) throws Exception {
total = new TextField();
items = FXCollections.observableArrayList();
model = new Main2Model();
c = model.getCompanyList();
primaryStage = arg0;
primaryStage.setTitle("MallApp");
primaryStage.centerOnScreen();
showMainView();
}
Upvotes: 5