What is wrong with this FX Application? - javafx

I am hours trying to know why this code is wrong, it gives me an Exception, the GUI.fxml is on the root of the project.
public class MyApp extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
String location = "/GUI.fxml";
Parent root = FXMLLoader.load(getClass().getResource(location));
primaryStage.setScene(new Scene(root,300,450));
primaryStage.setTitle("Minha Janela");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Already searched a lot, no solution found yet.
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2825)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
at g1.MyApp.start(MyApp.java:13)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more

The FXML should not be in the root of the project, but in the classpath. Try relocating the fxml into a source folder.
For general projects, you have a src folder. You can also create your own custom source folder also.
For maven projects, you can try keeping them inside src/main/resources.

Related

Why do I get a constructor exception on JavaFX?

Based on some online reference, I try to bring up a JavaFX applicaion with the simple following code.
#SpringBootApplication
public class DesktopApplication {
public static void main(String[] args) {
Application.launch(ChartApplication.class, args);
}
}
public class ChartApplication extends Application {
#Override
public void start(Stage stage) {
}
}
I can't start up the app due to an error
me#USAUS desktop % java -jar target/desktop-0.0.1-SNAPSHOT.jar
Aug 18, 2022 4:10:10 PM com.sun.javafx.application.PlatformImpl startup
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module #69ec05a2'
Aug 18, 2022 4:10:15 PM com.sun.glass.ui.mac.MacApplication ambda$waitForReactivation$6
WARNING: Timeout while waiting for app reactivation
Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.example.desktop.ChartApplication
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NoSuchMethodException: com.example.desktop.ChartApplication.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3585)
at java.base/java.lang.Class.getConstructor(Class.java:2271)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
I don't get why the class can't get constructed.
I use Java 17 and try various version of OpenJfx (versions from 18.0.2, 19 EA and 20 EA), and Spring Boot (2.7.3, 3.0.0-M4) with the same exception.
The cause is that JavaFX is removed from JDK after Java 11.

InvocationTargetException with javafx launch

I have some trouble with launching a javafx application. To explain, I wrote a code that worked but I was on Ubuntu 19.04 and I decided to reinstall my Linux version (20.04). I copied my code and reinstalled e(fx)clipse, scenebuilder, openjfx, and imported the same jfxrt.jar but nothing worked. I can't manage to launch javafx application, even if the code is empty (ig code). Oh and I have to use java 8 for this homework.
Can you help me please?
Here is my code
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class LaunchApplication extends Application {
public static void main(String[] args) {
launch(args);
}
//Empty but still error
#Override
public void start(Stage arg0) throws Exception {
}
//Old code, doesnt work anymore
// #Override
// public void start(Stage primaryStage) throws Exception {
// try {
//
// FXMLLoader loader = new FXMLLoader();
// loader.setLocation(getClass().getResource("view/Login.fxml"));
// Parent root = loader.load();
//
// Scene scene = new Scene(root);
// primaryStage.setScene(scene);
// primaryStage.setResizable(true);
// primaryStage.show();
//
// } catch (Exception e) {
// System.out.println(e);
// }
//
// }
}
And there my error when running it
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodError: <init>
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:288)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
at com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:675)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:337)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
... 5 more
Caused by: java.lang.NoSuchMethodError: <init>
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1934)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1817)
at java.lang.Runtime.load0(Runtime.java:810)
at java.lang.System.load(System.java:1088)
at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:107)
at com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:39)
at com.sun.glass.ui.gtk.GtkApplication.lambda$new$5(GtkApplication.java:156)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:140)
at com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
at com.sun.glass.ui.Application.run(Application.java:146)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:278)
... 9 more
```

JAVAFX Problem trying to play sound when clicking button

I'm trying to make a sound of Windows play when the user clicks the button. The code is just below:
public class TestController extends Application {
public String audio = getClass().getResource("src/Sounds/WindowsError.wav").toString();
#FXML
private Button playbt;
#FXML
void playtest(MouseEvent event)
{
System.out.println("Clicked!");
AudioClip clip = new AudioClip(audio);// 1
clip.play(); // 2
}
#Override
public void start(Stage primaryStage) {
try
{
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/FXML/Test.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
The controller is properly configured to FXML. However, when I try to run the project, the following error occurs.
Exception in Application constructor 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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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 Controller.TestController
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.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165(LauncherImpl.java:819)
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
Caused by: java.lang.NullPointerException
at Controller.TestController.<init>(TestController.java:17)
... 13 more Exception running application Controller.TestController
The files in my project are arranged as follows:
Does anyone know why this error occurs? The project hangs when opening due to the sound playback code.
I'm not totally solid on this, but I think the syntax of the getResource for the audio variable is in "relative address" form since it doesn't start with "/". In other words, it could be looking for a subfolder src in Controller package folder.
Maybe the following will help the system locate your audio resource:
public String audio = getClass().getResource("../Sounds/WindowsError.wav").toString();
Perhaps it would be good to move this line into the start() method, and run a System.out.println(audio) to inspect what you have at that point.
EDIT: just saw fabian's comment. Changing to the form using the classpath root as he suggests might be a better general practice than my suggestion to use a relative path.

JavaFX java.lang.IllegalStateException: Location is not set

Help! I'm stuck.. I try to run my main javafx app
Here is my codes;
#Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/com/utmkl/fxml/SimulatorDisplay.fxml"));
Parent content = (Parent)loader.load();
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setScene(new Scene(content));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
Below is my folder structure:
picture Folder structure
Below is the error
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.utmkl.VMCSManager.start(VMCSManager.java:43)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
I've found the answer..
the resource need to be in the same folder structure as main class
Here is my Maven-JavaFX Folder structure
VMCS
- src/main/java
- com.utmkl
VMCSManager.java
- src/main/resources
- com.utmkl.fxml
SimulatorDisplay.fxml
So, the correct code (which success to run)
VMCSManager.java
#Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
Parent content = loader.load();
Scene scene = new Scene(content);
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
Even though you do set a location with loader.setLocation(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); it is likely that the resource URL doesn't actually refer to an existing resource on the classpath.
If you add System.out.println(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); to your code I think it will print null as a result.

UnsatisfiedLinkError with javafx

I am trying do learn javafx. I have a very simple program to print "Hello World" once in the main method (as a test) and once in the Application. The code looks like this:
import javafx.stage.*;
import javafx.application.*;
public class HelloApp extends Application
{
public static void main(String[] args)
{
System.out.println("Hello World");
launch(args);
}
public void start(Stage stage)
{
System.out.println("Hello World");
}
}
this gives me the following output. Note that it prints the first "Hello World", but not the second.
--------------------Configuration: javafx trials - JDK version 1.6.0_07 <Default> - <Default>--------------------
Hello World
Exception in thread "main" java.lang.RuntimeException: java.lang.UnsatisfiedLinkError: Can't load library: C:\Documents and Settings\apcs.LGSUHSD\My Documents\JCreator LE\MyProjects\bin\glass.dll
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:281)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:124)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:163)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.UnsatisfiedLinkError: Can't load library: C:\Documents and Settings\apcs.LGSUHSD\My Documents\JCreator LE\MyProjects\bin\glass.dll
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1650)
at java.lang.Runtime.load0(Runtime.java:770)
at java.lang.System.load(System.java:1005)
at com.sun.glass.utils.NativeLibLoader.loadLibraryFullPath(NativeLibLoader.java:200)
at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:88)
at com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:31)
at com.sun.glass.ui.Application.loadNativeLibrary(Application.java:73)
at com.sun.glass.ui.Application.loadNativeLibrary(Application.java:80)
at com.sun.glass.ui.win.WinApplication$1.run(WinApplication.java:36)
at com.sun.glass.ui.win.WinApplication$1.run(WinApplication.java:33)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.glass.ui.win.WinApplication.<clinit>(WinApplication.java:33)
at com.sun.glass.ui.win.WinPlatformFactory.createApplication(WinPlatformFactory.java:20)
at com.sun.glass.ui.win.WinPlatformFactory.createApplication(WinPlatformFactory.java:17)
at com.sun.glass.ui.Application.Run(Application.java:103)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:271)
... 5 more
Process completed.
I have no Idea why this is happening. I have the jar file for javafx in the project. Can someone please explain why this is happening and what I can do about it?
if you are using eclipse add C:\Program Files\Oracle\JavaFX Runtime\lib\jfxrt.jar as external jar and rebuild project.

Resources