为什么不将所有JavaFX deps放在类路径中而又不用担心Java模块呢? [英] Why isn't it enough put all JavaFX deps in the classpath w/o bothering about Java modules?
问题描述
我一直在尝试编写Hello JavaFX应用程序,面对着有关启动该应用程序的Java模块的必要考虑.
I've been trying to write Hello JavaFX app and faced with necessity consider about Java modules for starting the app.
F.e. javafx-maven-plugin 的 run 目标产生这样的启动命令:
F.e. javafx-maven-plugin's run goal produce such startup command:
[DEBUG] Executing command line: [C:\java\zulu14.29.23-ca-jdk14.0.2-win_x64\bin\java.exe,
--module-path, C:\.m2\repository\org\openjfx\javafx-base\14\javafx-base-14-win.jar;
C:\.m2\repository\org\openjfx\javafx-base\14\javafx-base-14.jar;
C:\.m2\repository\org\openjfx\javafx-controls\14\javafx-controls-14-win.jar;
C:\.m2\repository\org\openjfx\javafx-controls\14\javafx-controls-14.jar;
C:\.m2\repository\org\openjfx\javafx-graphics\14\javafx-graphics-14-win.jar;
C:\.m2\repository\org\openjfx\javafx-graphics\14\javafx-graphics-14.jar,
--add-modules, javafx.base,javafx.controls,javafx.graphics,
-classpath, D:\project\target\classes, org.pkg.pkg.App]
但是我项目的结构没有提到Java模块功能-我没有 module-info.java 文件.
为什么我不能将所有JAR放在应用程序的类路径中并感到高兴?
F.e.:
But my project's structure doesn't mention Java module functionality - I didn't have module-info.java file.
Why can't I put all above JARs in the app classpath and be happy?
F.e.:
C:\java\zulu14.29.23-ca-jdk14.0.2-win_x64\bin\java.exe
"-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.4\lib\idea_rt.jar=59556:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.4\bin"
-Dfile.encoding=UTF-8
-classpath D:\project\target\classes;
C:\.m2\repository\org\openjfx\javafx-controls\14\javafx-controls-14.jar;
C:\.m2\repository\org\openjfx\javafx-controls\14\javafx-controls-14-win.jar;
C:\.m2\repository\org\openjfx\javafx-graphics\14\javafx-graphics-14.jar;
C:\.m2\repository\org\openjfx\javafx-graphics\14\javafx-graphics-14-win.jar;
C:\.m2\repository\org\openjfx\javafx-base\14\javafx-base-14.jar;
C:\.m2\repository\org\openjfx\javafx-base\14\javafx-base-14-win.jar
org.pkg.pkg.App
If I'm right and understand Packages and Modules spec correctly
- 在类路径中定义的
- 模块JAR被视为未命名模块
- 未命名的模块会导出所有包,即所有可见的包
但是我得到一个错误
Error: JavaFX runtime components are missing, and are required to run this application
请问一下关于Java模块的工作原理,我在这里错了吗?
Could someoune please explain me where I'm wrong here, about how Java modules work?
提前谢谢!
推荐答案
警告:尽管下面显示了所需的功能,但将JavaFX模块放在类路径上是不支持.这意味着即使您自己的代码不是模块化的,也应该将JavaFX放在模块路径上.
Warning: Although the below shows what you want is possible, placing the JavaFX modules on the class-path is not supported. This means you should place JavaFX on the module-path, even when your own code is non-modular.
您可以将JavaFX放在类路径上,并完全忽略模块 1 .但是,一个警告是,您的主类不能再分配给 javafx.application.Application
2 .解决方法是创建一个单独的主类,该主类仅启动JavaFX应用程序.例如:
You can put JavaFX on the class-path and completely ignore modules1. However, the one caveat is that your main class can no longer be assignable to javafx.application.Application
2. The workaround is to create a separate main class that simply launches the JavaFX application. For example:
import javafx.application.Application;
public class Launcher {
public static void main(String[] args) {
// where YourApp.class is replaced with your Application class
Application.launch(YourApp.class, args);
}
}
1.您不能真的完全忽略模块.类路径上的所有代码都以未命名的模块结尾,并且运行时映像中的所有模块(即JDK/JRE)仍充当命名模块.
1. Your can't really ignore modules completely. All code on the class-path ends up in the unnamed module and all the modules in the run-time image (i.e. the JDK/JRE) still function as named modules.
2.这是由于实施细节.Java包含的代码允许您在没有主方法的情况下启动JavaFX应用程序,只要该主类可分配给 Application
.但是,当检测到主类可分配给 Application
时,它将在引导 ModuleLayer
中检查 javafx.graphics
,这意味着它必须位于模块路径-如果不存在,则认为缺少JavaFX.
2. This is due to an implementation detail. Java contains code which allows you to launch JavaFX applications without a main method, so long as the main class is assignable to Application
. But when it detects that the main class is assignable to Application
it checks for javafx.graphics
in the boot ModuleLayer
—meaning it must be on the module-path—and if not there then it assumes JavaFX is missing.
这篇关于为什么不将所有JavaFX deps放在类路径中而又不用担心Java模块呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!