Issue
Other possible solutions I got in different forums are not working. Here is my code.
class javaFX extends Application{
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("JavaFX app");
Label label = new Label("Hello World, JavaFX !");
Scene scene = new Scene(label, 400, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
Application.launch(args);
}
}
This is the StackTrace. It is showing NoSuchElementException which is a bit weird.
Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class xlSheets.javaFX
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: xlSheets.javaFX.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
Solution
The Application.launch()
method uses reflection to create an instance of the Application
subclass, calling its constructor taking no arguments.
According to the documentation the Appplication
subclass
must be a public subclass of
Application
with a public no-argument constructor
So in order for this to work, both the Application
subclass and the constructor (if explicitly defined) need to be declared public
.
The following fixes the problem (I also changed the class name to conform to Java naming conventions):
public class JavaFX extends Application{
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("JavaFX app");
Label label = new Label("Hello World, JavaFX !");
Scene scene = new Scene(label, 400, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
Application.launch(args);
}
}
Answered By - James_D