Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

java - JavaFX WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @...'

I just downloaded JavaFX and set it up, I have done nothing else. I ran the sample code and this was the warning that popped up, although everything compiled. I'm using IntelliJ.

This is in Main.java:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

This is in sample.fxml:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>

When running, everything compiles, the window pops up, but I get the warning as stated in the title.

I never used JavaFX before so I'm unsure of where to locate this module.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Technically JavaFX only supports being loaded as named modules. As of version 16 a warning is now emitted if JavaFX ends up in the unnamed module. In other words, JavaFX only supports being loaded from the module-path and does not support being loaded from the class-path. You need to make sure JavaFX is resolved as modules, which can be done in at least one of three ways:

  1. If your code is not modular then place JavaFX on the --module-path and make sure to add the needed modules to the --add-modules argument. For example, if your application needs the javafx.controls and javafx.fxml modules you'd have something like:

    java --module-path <path-to-fx> --add-modules javafx.controls,javafx.fxml ...
    

    The javafx.graphics and javafx.base modules are required by those other modules and so will be implicitly pulled in.

  2. If your code is modular then you'll have a module-info descriptor which has requires directives for the JavaFX modules. Here you just need to put all the modules on the module-path and then launch the application as a module. For example:

    module app {
      requires javafx.controls;
      requires javafx.fxml;
    
      // export Application subclass's package to at least javafx.graphics
      exports com.example.app to javafx.graphics;
    
      // open any FXML controller class' packages to at least javafx.fxml
      // ...
    }
    
    java --module-path <path> --module app/com.example.app.Main
    
  3. Use a Java distribution that includes JavaFX. Now JavaFX is part of the run-time image and will be loaded as modules automatically like any other Java module (e.g. java.base) that's in the run-time image. This is true regardless of if your code is modular or not. Though if your code is not modular then you may still need --add-modules.

    Note you can make use of this idea with jlink / jpackage when deploying your application.

If you're using an IDE and/or build tool, and you don't know where to set the --module-path or --add-modules arguments, then check out Getting Started with JavaFX. And if you don't actually know what modules are in Java then Understanding Java 9 Modules gives a good overview.

That all being said, as far as I can tell nothing currently seems to break if you put JavaFX on the class-path instead of the module-path. Except for one caveat: Your main class cannot be a subclass of javafx.application.Application so you'll need to make a separate main class that simply launches JavaFX. In other words, you can ignore the warning if you really want to. But I would recommend putting JavaFX on the module-path (even if your own code is non-modular) if you can do so without too much trouble.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...