Reputation: 21
I'm new in Javafx, I just downloaded JDK 12 and followed a tuturial, it has worked but not worked for me, (I'm using module to require javafx.controls) here is the code: in my main class :
I tried a lot of solutions in SOFlow but no result, I tried : 1) add public keyword to my class 2) removed the main method still not work help ?
package com.teachersdunet.hellojavafx;
import javafx.application.Application;
import javafx.stage.Stage;
public class HelloApp extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
}
}
and here is the error after executing it :
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: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.RuntimeException: Unable to construct Application instance: class com.teachersdunet.hellojavafx.HelloApp
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.teachersdunet.hellojavafx.HelloApp (in module com.teachersdunet.hellojavafx) because module com.teachersdunet.hellojavafx does not export com.teachersdunet.hellojavafx to module javafx.graphics
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:639)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:490)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
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:389)
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
Upvotes: 2
Views: 13424
Reputation: 1
Try to put your class in default package org.example ,or you can add in module-info.java file(under package org.example) this line exports(name of your created package).
Upvotes: 0
Reputation: 339
You are getting an error like that because you haven't invoked anything inside the start method. You will have to set the Scene
and also provide the directory of your FXML
file.
I have corrected your code.
package com.teachersdunet.hellojavafx;
import javafx.application.Application;
import javafx.stage.Stage;
public class HelloApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
try {
Parent root = FXMLLoader.load(getClass().getResource("directory_of_your_fxml_file"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I hope this helps.
Upvotes: 1
Reputation: 82521
The solution is almost mentioned in the stacktrace; the issue is narrowed down to the point where it tells you about the export that is missing:
...
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.teachersdunet.hellojavafx.HelloApp (in module com.teachersdunet.hellojavafx) because module com.teachersdunet.hellojavafx does not export com.teachersdunet.hellojavafx to module javafx.graphics
...
Add the following line to the com.teachersdunet.hellojavafx
module:
module com.teachersdunet.hellojavafx {
...
exports com.teachersdunet.hellojavafx;
}
Or alternatively granting access to only a single module:
module com.teachersdunet.hellojavafx {
...
exports com.teachersdunet.hellojavafx to javafx.graphics;
}
Upvotes: 5