简体   繁体   中英

Javafx - On intelliJ with gradle error: package javafx.fxml does not exist import javafx.fxml.FXML

I don't understand why I have always the same error:

image

when I try to add library (fx java) on my strcuture project.

My build.gradle:

plugins {
    id 'java'
    id 'application'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}


application {
mainClassName = 'Launcher'
}

adding dependencies this way is bad. you're using gradle so let it do the work. once gradle is resolved all dependencies it will cached each on your system. so you don't need to add external libraries with a folder(lib) for each project. gradle will resolve that for you from it's local cache. so get rid of that lib folder and replace your build.gradle file contents with this

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.9'
}

application {
    //Note: Check package name. replace it with right one if it's wrong
    mainClassName='home.Launcher'
}

javafx {
    version='11.0.2'
    modules=['javafx.controls', 'javafx.fxml']
}

repositories {
    mavenCentral()
}

also you're imported packages from awt not from javafx. replace those eg: java.awt.event.ActionEvent to javafx.event.ActionEvent

Happy coding:)

There isn't enough to go on to fully answer this, you will find you will get help quicker if you follow the rules about posting questions, including the most important, being a https://stackoverflow.com/help/minimal-reproducible-example

We don't know what version of gradle or javafx you are using.

I'm guessing you don't have the JavaFX library on the classpath or in the project libraries...

Post stacktraces as code, not as an image link, no one wants to click on links.

But from the Stacktrace, it can't find package javafx.fxml.

I find it easiest when using gradle, to use the javafx plugin. When using the plugin, mainClassName goes in the javafx function, if you read my coments within the javafx function, you will see how to set the JAVAFX_HOME and PATH_TO_FX system variables, you will need to edit this gradle file obviously to match your setup.

Also, mainClassName is the full package with class name, so I assume in your case it should be org.example.Launcher

plugins {
    id 'org.openjfx.javafxplugin' version '0.0.9'
}


apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'


group 'org.example'
version '1.0-SNAPSHOT'

javafx {
    version = "14.0.2.1"

    /*******************************************************************************************************************
     *
     *   Set an environment variable pointing to the location of the JavaFX SDK!
     *   JAVAFX_HOME is the sdk directory
     *   PATH_TO_FX  is the sdk/lib directory
     *
     *   ie: for Windows : Open an Administrator Command Prompt
     *      To Permanently set an environment variable for the current user:
     *             C:\> setx JAVAFX_HOME "C:\bin\Java\javafx-sdk-14.0.1"
     *             C:\> setx PATH_TO_FX "C:\bin\Java\javafx-sdk-14.0.1/lib"
     *
     *      Or, alternatively to Permanently set global environment variable (for all users):
     *             C:\> setx /M JAVAFX_HOME "C:\bin\Java\javafx-sdk-14.0.1"
     *             C:\> setx /M PATH_TO_FX "C:\bin\Java\javafx-sdk-14.0.1/lib"
     *
     *   ie: for MacOS  : Open a Terminal
     *     If using bash:
     *             echo 'export JAVAFX_HOME=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1' >> ~/.bash_profile
     *             echo 'export PATH_TO_FX=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1/lib' >> ~/.bash_profile
     *
     *     If using zsh:
     *             echo 'export JAVAFX_HOME=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1' >> ~/.zshrc
     *             echo 'export PATH_TO_FX=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1/lib' >> ~/.zshrc
     *
     *******************************************************************************************************************/

    sdk = System.getenv('JAVAFX_HOME')
    if (sdk == null || sdk.isBlank()) {
        throw new InvalidUserDataException("JAVAFX_HOME environment variable is not set. It must be set to root folder where JAVAFX SDK is located")
    }
    modules = ['javafx.base', 'javafx.graphics', 'javafx.controls', 'javafx.fxml', 'javafx.web', 'javafx.swing']
    mainClassName = 'org.example.Launcher'
}

application {
    applicationDefaultJvmArgs = [
            "--add-opens=javafx.graphics/com.sun.javafx.css=ALL-UNNAMED",
            "--add-opens=javafx.graphics/com.sun.prism=ALL-UNNAMED",
            "--add-opens=javafx.graphics/com.sun.prism.sw=ALL-UNNAMED"
    ]
}

repositories {
    mavenCentral()
}


Hope this helps, and gets you pointed in the right direction... It really helps if you read the docs for JavaFX including how to set it up for IntelliJ and Gradle... all the info is really in there, and if more people actually read it, we wouldn't have to keep answering these kinids of questions, We have all read it, that's what it's there for!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM