Can't get JavaFX to run in Eclipse

4.7k views Asked by At

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.

My problem is that I can't get JavaFX to run in eclipse. I have installed the e(fx)clipse addon.

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".

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?

Edit:

This is the import statement:

import javafx.application.Application;

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();
    }
}

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 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
2

There are 2 answers

1
Braveness On

So if everything is correct with your jar and you have correctly included it, it can be pretty hard to find the bug. How big is your project? If I were you, I'd create a Maven project from your project instead of including the jar manually. This will also save you a lot of work when adding more dependencies.

Here you can find all javafx dependencies: https://mvnrepository.com/artifact/org.openjfx

I integrated javafx13 into my modularized project. The corresponding dependency only has to be imported into the POM and then you have to import the necessary requirements into your module-info.java (as you did before).

1
Barosanu240 On

In your module-info.java file, you should add:

opens 'your main class package';

You can follow this online tutorial: https://www.youtube.com/watch?v=B-mgPyXEIgo

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.