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

huangapple go评论78阅读模式
英文:

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

问题

我不明白为什么我一直都会遇到相同的错误:

image

当我尝试在我的结构化项目中添加库(例如 Java)时。

我的 build.gradle 文件:

plugins {
    id 'java'
    id 'application'
}

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

repositories {
    mavenCentral()
}

application {
    mainClassName = 'Launcher'
}
英文:

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'
}

答案1

得分: 2

以下是翻译好的内容:

添加依赖项的方式不好。你正在使用 Gradle,让它来完成这项工作。
一旦 Gradle 解析了所有依赖项,它们将被缓存在你的系统上。所以你不需要为每个项目添加一个外部库的文件夹(lib)。Gradle 会从它的本地缓存中为你解析这些依赖项。所以摆脱那个 lib 文件夹,并将你的 build.gradle 文件内容替换为以下内容:

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

application {
    //注意:检查包名。如果错误,请将其替换为正确的包名
    mainClassName = 'home.Launcher'
}

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

repositories {
    mavenCentral()
}

此外,你导入了来自 awt 的包,而不是 javafx。请替换这些导入,例如:将 java.awt.event.ActionEvent 替换为 javafx.event.ActionEvent。

祝你编码愉快 Javafx – On intelliJ with gradle error: package javafx.fxml does not exist import javafx.fxml.FXML

英文:

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 Javafx – On intelliJ with gradle error: package javafx.fxml does not exist import javafx.fxml.FXML

答案2

得分: 0

以下是翻译好的内容:

这里的信息不足以完全回答这个问题,如果你遵循发布问题的规则,尤其是最重要的一点,提供一个最小可复现的示例,你会更快地获得帮助。

我们不知道你正在使用哪个版本的Gradle或JavaFX。

我猜测你的类路径或项目库中没有JavaFX库...

将堆栈跟踪作为代码发布,而不是作为图像链接,没有人想要点击链接。

但从堆栈跟踪中可以看出,它找不到javafx.fxml包。

我发现在使用Gradle时,使用JavaFX插件是最简单的。使用插件时,mainClassName 放在 JavaFX 函数中。如果你阅读一下我在 JavaFX 函数内部的注释,你会看到如何设置 JAVAFX_HOME 和 PATH_TO_FX 系统变量,显然需要编辑这个 Gradle 文件,以使其与你的设置相匹配。

另外,mainClassName 是完整的包名加类名,所以我假设在你的情况下应该是 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"

	/*******************************************************************************************************************
	 *
	 *   设置指向 JavaFX SDK 位置的环境变量!
	 *   JAVAFX_HOME 是 sdk 目录
	 *   PATH_TO_FX  是 sdk/lib 目录
	 *
	 *   例如:对于 Windows:打开管理员命令提示符
	 *      永久为当前用户设置环境变量:
	 *             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"
	 *
	 *      或者,永久设置全局环境变量(适用于所有用户):
	 *             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"
	 *
	 *   例如:对于 MacOS:打开终端
	 *     如果使用 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
	 *
	 *     如果使用 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 环境变量未设置。必须设置为 JAVAFX SDK 所在的根文件夹")
	}
	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()
}

希望这能帮助你并指引你朝着正确的方向前进... 如果你阅读了 JavaFX 的文档,包括如何在 IntelliJ 和 Gradle 中设置它,这将非常有帮助... 所有的信息实际上都在文档中,如果更多的人实际上阅读了它,我们就不必继续回答这些问题了!我们都已经阅读过它,那就是它的用途!

英文:

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!

huangapple
  • 本文由 发表于 2020年8月17日 01:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63439684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定