Does JavaFX after removing it from jdk cross-platf

2020-02-09 04:53发布

问题:

Does JavaFX 11 cross-platform? As i saw in openjfx.org that i have to download releases for each platform. And if so, can i in any way use it in a cross-platform form? And should i use each javafx release for each java release? Don't tell me to use maven or something like that. I want to program a cross-platform software. And not a multi-release software each release for each OS. I thout to load the modules after checking the OS but can i do that? Any way there is a lot of libraries that are cross-platform even if it have native code so why OpenJFX doesn't do that?

回答1:

JavaFX 11 is not part of the JDK any more, and it is distributed now in two flavors:

  • As SDK from https://gluonhq.com/products/javafx/.
  • Via Maven Central, like https://search.maven.org/artifact/org.openjfx/javafx-controls/11/jar

When you download the JavaFX SDK you have to choose a platform, the same as when you install the JDK for your platform. It remains cross-platform in terms of API, so your code will look the same on every platform, but it is platform-specific in terms of SDK/dependencies.

The reason for that is the native libraries that are bundled with each SDK.

If you have a look at the three JavaFX 11 SDK bundles each of them zipped has around 40 MB:

  • Windows: 39.7 MB zipped, 64.8 MB in dll libraries (jfxwebkit.dll only has 59.2 MB), and 15.5 MB in jars and source.
  • Mac: 39.7 MB zipped, 72.2 MB in dylib libraries (libjfxwebkit.dylib only has 67.8 MB), and 15.6 MB in jars and source.
  • Linux: 43.8 MB zipped, 84.9 MB in so libraries (libjfxwebkit.so only has 80.1 MB), and 15.8 MB in jars and source.

Now the question is: would you bundle the three platforms in one? Total zipped size of 120 MB, unzipped native libraries for three platforms around 220 MB?

There was a debate about this issue precisely in the openjfx-dev mailing list, but I believe the taken approach is the correct one.

If you use Maven/Gradle and retrieve your dependencies from Maven Central, on Gradle you have to specify the classifier for your plaform. See the Getting Started sample. Maven does it internally, so even if you don't set the classifier, it only download the jars for your platform.

So for a regular project where you don't use WebView, it could make sense, as you will download only the required modules dependencies for your platform: javafx.base, javafx.graphics, javafx.controls and javafx.fxml.

But so far, and that's not something that can be changed now, you get your platform-agnostic JavaFX API but with platform-specific JavaFX SDK/modules.

Note that when you distribute a JavaFX application, you will use the Jpackager and you will release three different versions for the three different platforms, so either way you are in need of a platform-specific distribution.



回答2:

JavaFX is written in Java, which is ran on the JVM, which is cross-platform. So that would mean JavaFX is also cross-platform.

You download different releases to run the JVM on your machine. But your JavaFX code can be run on any of these releases, so it's cross-platform.