无法使JavaFX在Eclipse中运行 [英] Can't get JavaFX to run in Eclipse
问题描述
我在JavaFX上遇到了一个问题,经过一天多在Google和其他论坛上的研究后,我无法解决.我目前正在通过教程和几本书来学习Java,到目前为止,进展非常顺利.
I have a Problem with JavaFX that I can't resolve after more than a day of researching on Google and different forums. I am currently learning Java with a tutorial and a few books, which has been going very nicely up until now.
我的问题是我无法让JavaFX在Eclipse中运行.我已经安装了e(fx)clipse插件.
My problem is that I can't get JavaFX to run in eclipse. I have installed the e(fx)clipse addon.
我已确保JavaFX SDK在Java Buid路径的库中列出,并且尝试打开一个新的JavaFX Project,但是无论如何我总会得到无法解析导入javafx"的信息.
I have made sure that the JavaFX SDK is listed in the Libraries in the Java Buid Path and I tried opening a new JavaFX Project, but whatever I do I always get "the import javafx can't be resolved".
我已经在Ubuntu 18.04(我的主系统)和Win 8.1上使用Java JDK 12进行了所有尝试.
I have tried all of this with the Java JDK 12 on Ubuntu 18.04 (my main system) and Win 8.1.
这里有人有什么主意吗?
Has anybody here an idea what could be wrong?
这是导入语句:
import javafx.application.Application;
这是我的类的代码(尽管我也尝试了Eclipse JavaFX Project中自动生成的"Main"类):
This is the Code of my Class (although i also tried the auto generated "Main" Class from the Eclipse JavaFX Project):
import javafx.application.Application;
import javafx.stage.Stage;
public class JvaFXDemo extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.show();
}
}
现在,我可以通过在包含以下代码的"scr"文件夹中创建一个名为"module-info.java"的类来摆脱此错误:
Now i was able to get rid of this Error by creating a Class named "module-info.java" in the "scr" Folder that contains this Code:
requires javafx.base;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires javafx.media;
requires javafx.swing;
requires javafx.swt;
requires javafx.web;
Eclipse现在不在代码中显示任何错误,但是当我尝试对其进行编译时,我得到了一个巨大的异常:
Eclipse doesn't show any Errors in the Code now, but when i try to compile it i get a massive Exception:
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 application.Main
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 application.Main (in module BlaBla) because module BlaBla does not export application 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.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
推荐答案
在module-info.java文件中,您应该添加:
In your module-info.java file, you should add:
opens 'your main class package';
您可以按照以下在线教程进行操作: https://www.youtube.com/watch?v=B-mgPyXEIgo
You can follow this online tutorial: https://www.youtube.com/watch?v=B-mgPyXEIgo
作为建议,您不应该为JavaFx使用IDE附加组件,例如e(fx)clipse.运行JavaFx发生了很大变化,这些附加组件不是最新的.
As an advise, you shouldn't use IDE add-ons for JavaFx like e(fx)clipse. Running JavaFx has changed a lot and these add-ons are not up to date.
这篇关于无法使JavaFX在Eclipse中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!