I am trying to build an app in JavaFX but i am facing the issue.
Below code generating error.I error are showing since i created the project.
I downloaded the JavaFX 14 from "https://gluonhq.com/products/javafx/" and include all jar files present in lib directory.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
But This code works fine
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
//Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
StackPane root=new StackPane();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Getting this error:
"C:\Program Files\Java\jdk-14.0.1\bin\java.exe" --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\lib\idea_rt.jar=60984:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\Shapes\out\production\Shapes;E:\Software\javafx-sdk-14.0.2.1\lib\javafx-swt.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.controls.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.fxml.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.media.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.swing.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.web.jar -p E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar sample.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x66dac2b5) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0x66dac2b5
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at sample.Main.start(Main.java:14)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
The fundamental error is (formatted for readability):
Caused by: java.lang.IllegalAccessError:
class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x66dac2b5)
cannot access class com.sun.javafx.util.Utils (in module javafx.graphics)
because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0x66dac2b5
That error is telling you that the javafx.fxml module has ended up on the class-path while the javafx.graphics module is on the module-path. This scenario prevents the special access to internal code the javafx.graphics module grants to the javafx.fxml module.
The solution to this is to do one of the following:
Include javafx.fxml in your --add-modules VM argument.
Make your own code modular, add the necessary requires, exports, and opens directives, and launch your application with --module.
If you're curious about how the above error comes about then my answer to another question goes into more detail. Note it focuses on the javafx.media module but the concept is the same.
Related
I got this error when i'm trying to run my code by the statement "LauncherImpl".
Has anyone an idea how to fix it?
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.IllegalAccessError: class application.view1controller (in unnamed module #0x3fb6a447) cannot access class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.application to unnamed module #0x3fb6a447
at application.view1controller.main(view1controller.java:24)
... 11 more
Exception running application application.view1controller
package application;
import com.sun.javafx.application.LauncherImpl;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class view1controller extends Application {
public void start(Stage view1) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/application/view1.fxml"));
Scene scene = new Scene(root);
view1.setScene(scene);
view1.show();
}
public static void main(String[] args) {
LauncherImpl.launchApplication(view1controller.class, SplashScreen.class, args);
}
VM Arguments:
--module-path "D:\Fabian Boni\Documents\eclipse workspace\javafx-sdk-11.0.2\lib" --add-modules
javafx.controls,javafx.fxml,javafx.base,javafx.graphics
I am trying to create my first JavaFX program,using intelliJ IDEA
and i m getting this error :
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0
(Native Method)at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.
launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.
launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0
(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main
(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application
start method
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.
launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.
lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class
com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module
#0x5ba89dd0) cannot access class com.sun.javafx.util.Utils (in
module javafx.graphics) because module javafx.graphics does not
export com.sun.javafx.util to unnamed module #0x5ba89dd0
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>
(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at sample.Main.start(Main.java:13)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.
lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.
lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.
lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged
(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.
lambda$runLater$11(PlatformImpl.java:427)
at
javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run
(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop
(Native Method)
at
javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.
lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application sample.Main
This is my FXML file :
`<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="386.0" prefWidth="621.0"
xmlns="http://javafx.com/javafx/8.0.172-ea"
xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller">
<children>
<Label fx:id="m" layoutX="282.0" layoutY="192.0" text="this is a
test " />
</children>
</AnchorPane>`
main.java :
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root=
FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}}
this is the project structure :
project structure
I even changet the fxml file location but he keep on getting the same error.
the FXML file and Main.java are in the same package
i am Ubuntu
i have found the solution , i have added the JavaFX SDK to the modules and modified the module-info.java file because i am using jdk 11
module project-name {
requires javafx.fxml;
requires javafx.controls;
opens sample;
}
This is the test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<center>
<Label text="It is all OK" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
I did a Main.java that launchs the window, like follow:
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
public static void main(String args[]) {
launch(args);
}
public void start(Stage stage) {
try {
URL url = this.getClass().getResource("/test.fxml");
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (Exception e) {
// ignore
}
}
}
It works.
But the main.py just can not launch and it is almost equal to Main.java.
I did 2 versions of this file that launchs different errors.
This is the first version:
import sys
from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene
class Main(Application):
#classmethod
def main(cls, args):
Main.launch(cls, args)
def start(self, stage):
try:
url = self.getClass().getResource('/test.fxml')
root = FXMLLoader.load(url)
scene = Scene(root)
stage.setScene(scene)
stage.show()
except Exception as exc:
pass
if __name__ == '__main__':
Main.main(sys.argv)
That shows:
Exception in Application start method
Traceback (most recent call last):
File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in
Main.main(sys.argv)
File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
Main.launch(cls, args)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
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)
java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method
This is the second version:
import sys
from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene
class Main(Application):
#classmethod
def main(cls, args):
Main.launch(cls, args)
def start(self, stage):
try:
url = self.getClass().getResource('/test.fxml')
self.loader = FXMLLoader(url) # changed here
root = self.loader.load() # changed here
scene = Scene(root)
stage.setScene(scene)
stage.show()
except Exception as exc:
pass
if __name__ == '__main__':
Main.main(sys.argv)
I get another error, that is:
Exception in Application start method
Traceback (most recent call last):
File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in
Main.main(sys.argv)
File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
Main.launch(cls, args)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
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)
java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method
Where is my mistake?
All the files are in the same folder.
Please, help me. Thanks.
Jython version is:
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
Java version is:
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
I am running the code at Lubuntu based on Ubuntu 18.04.1 LTS.
I successfully made a simple JavaFX FXML program (in pre-Java 9 style, not module)
I used 3 files and the main class as below:
└───javafxfxmldemo
FXMLDocument.fxml
FXMLDocumentController.java
JavaFXFXMLDemo.java
Main class
public class JavaFXFXMLDemo extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It works perfectly both in NetBeans 9 and in DOS terminal with javac and java.
Now, I tried to make the same program in Java modules. Below is my source structure:
└───src
└───com.fxmlapp
│ module-info.java
│
└───com
└───fxmlapp
FXEventModuleDemo.java
FXMLDocument.fxml
FXMLDocumentController.java
module-info.java
module com.fxmlapp {
requires javafx.controls;
requires javafx.fxml;
exports com.fxmlapp;
}
All the rest of code is identical except the main class name.
It compiles in NetBeans 9 and I also could compile in DOS terminal as below:
javac -d mods\com.fxmlapp --module-path %PATH_TO_FX% src\com.fxmlapp\module-info.java src\com.fxmlapp\com\fxmlapp\FXEventModuleDemo.java src\com.fxmlapp\com\fxmlapp\FXMLDocumentController.java
After compilation, I placed the FXMLDocument.fxml file in the mods directory as below.
├───mods
│ └───com.fxmlapp
│ │ module-info.class
│ │
│ └───com
│ └───fxmlapp
│ FXEventModuleDemo.class
│ FXMLDocument.fxml
│ FXMLDocumentController.class
│
└───src
└───com.fxmlapp
│ module-info.java
│
└───com
└───fxmlapp
FXEventModuleDemo.java
FXMLDocument.fxml
FXMLDocumentController.java
But, it fails to run. I tried both in NetBeans 9 and in DOS terminal as below:
java --module-path %PATH_TO_FX%;mods -m com.fxmlapp/com.fxmlapp.FXEventModuleDemo
The error comes from the statement:
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
I know, there are many posts with this issue, but I couldn't find a solution to this when the error occurs in java modules. I don't think it is the file location issue because below statement works.
System.out.println(getClass().getResource("FXMLDocument.fxml"));
FXMLLoader.load() seems doesn't work in modules.
If it works in non-module style, I believe it must work in modules too.
The actual error message is as below; Yes, I saw many questions asking with this error, but I still couldn't find a solution with java modules) Did I do something wrong with my module-info.java or my compile or run commands?
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
/D:/OpenJFX_Demo/FXEventModuleDemo/mods/com.fxmlapp/com/fxmlapp/FXMLDocument.fxml:14
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at com.fxmlapp/com.fxmlapp.FXEventModuleDemo.start(FXEventModuleDemo.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private javafx.scene.control.Label com.fxmlapp.FXMLDocumentController.label accessible: module com.fxmlapp does not "opens com.fxmlapp" to module javafx.fxml
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:340)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:280)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleFields(FXMLLoader.java:3479)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.access$3900(FXMLLoader.java:3328)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor$1.run(FXMLLoader.java:3444)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor$1.run(FXMLLoader.java:3440)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.addAccessibleMembers(FXMLLoader.java:3439)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerAccessor.getControllerFields(FXMLLoader.java:3378)
at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1170)
at javafx.fxml/javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:865)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 17 more
Exception running application com.fxmlapp.FXEventModuleDemo
As explained by #nullpointer:
The error says module com.fxmlapp does not "opens com.fxmlapp" to module javafx.fxml.
So, I added --add-opens com.fxmlapp/com.fxmlapp=javafx.fxml. It solveed the problem.
I'm in need of doing surgery to the JavaFX WebView class to make it render even when not visible. In my quest to achieve this I found Javassist, but when I try to use it, I get this error:
java.lang.IllegalArgumentException: Can not set javafx.scene.web.WebView field sample.Controller.webView to javafx.scene.web.WebView
I think that's because the member was defined before it got modified? I'm not sure. At the moment, I'm not modifying anything in the class, just loading, defrosting it (?) and saving it, by using the code:
CtClass webViewClass = ClassPool.getDefault().get("javafx.scene.web.WebView");
webViewClass.defrost();
webViewClass.toClass();
This is my minimum reproducible example. First, Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
public class Main extends Application {
public static void main(String[] args) {
try {
CtClass webViewClass = ClassPool.getDefault().get("javafx.scene.web.WebView");
webViewClass.defrost();
webViewClass.toClass();
} catch (NotFoundException | CannotCompileException e) {
e.printStackTrace();
}
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
}
and these are the contents of the Controller.java file:
package sample;
import javafx.fxml.FXML;
import javafx.scene.web.WebView;
public class Controller {
#FXML
private WebView webView;
#FXML
private void initialize() {
webView.getEngine().load("http://stackoverflow.com");
}
}
and this is the view, sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.web.WebView?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<WebView fx:id="webView" minHeight="-Infinity" minWidth="-Infinity"/>
</AnchorPane>
The full exception is:
Exception in Application start method
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 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(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: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/C:/Users/pupeno/Documents/Dashman/code/experiments/webviewwoes/out/production/webviewwoes/sample/sample.fxml:8
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at sample.Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.web.WebView field sample.Controller.webView to javafx.scene.web.WebView
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:765)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 17 more
Exception running application sample.Main
Process finished with exit code 1
Most likely there are two versions of your Webview loaded by two different ClassLoaders, at least I would expect such an exception only in that case.
Note this paragraph from the javassist tutorial:
If the program is running on some application server such as JBoss and
Tomcat, the context class loader used by toClass() might be
inappropriate. In this case, you would see an unexpected
ClassCastException. To avoid this exception, you must explicitly give
an appropriate class loader to toClass(). For example, if bean is your
session bean object, then the following code:
CtClass cc = ...; Class c =
cc.toClass(bean.getClass().getClassLoader()); would work. You should
give toClass() the class loader that has loaded your program (in the
above example, the class of the bean object).
Calling toClass() makes the context class loader of the thread load the class. I assume when the FXMLLoader loads the view, it loads the classes with a different classloader, and somehow things get mixed up so that you end up with this exception. Another possible outcome would have been that everything loads fine, but your modifications don't work because the FXMLLoader loads unmodified classes.
Do something like this:
ClassPool classPool = ClassPool.getDefault();
CtClass webViewClass = classPool.get("javafx.scene.web.WebView");
webViewClass.defrost();
classPool.toClass(webViewClass, FXMLLoader.class.getClassLoader(), null);