JavaFX deployment library not found in active JDK

前端 未结 3 1739
暖寄归人
暖寄归人 2020-11-27 22:30

I am migrating to OpenJDK 11 and OpenJFX 11. I have successfully built both from the source, and as per the OpenJFX wiki used the --with-import-modules=[path_to_modular_sdk]

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 22:52

    As @mipa points out, you don't need to build neither Java 11 nor JavaFX 11 in order to migrate your existing projects.

    As for Apache NetBeans 9.0, the current ant build files for JavaFX project don't support JavaFX 11 yet. It is always looking for the JavaFX jar, and it will stop when it is not found. This is somehow legacy from the old days (before Java 8), when JavaFX was not part of the JDK and you had to add the jar manually.

    For instance, when you try to create a new JavaFX project, you will get this error:

    But this doesn't mean that you can't run JavaFX 11, with or without NetBeans 9.0.

    Running on terminal

    You can run your Java/JavaFX 11 project from a terminal. You can follow this getting started guide for a detailed step by step.

    In a nutshell, all you need is:

    export PATH_TO_FX=/path/to/javafx-sdk-11/lib
    javac --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX.java
    java --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX
    

    where HelloFX.java is:

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class HelloFX extends Application {
    
        @Override
        public void start(Stage stage) {
            String version = System.getProperty("java.version");
            Label l = new Label ("Hello, JavaFX 11, running on " + version);
            Scene scene = new Scene(new StackPane(l), 300, 200);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }
    

    Modular Project

    Create a modular project on NetBeans 9.0, and add a module (for the sake of simplicity I named it javafx11, which is not a recommended name).

    Before adding the code, let's add the JavaFX library to the module-path.

    This library is just the folder under my local download of JavaFX: /Users//Downloads/javafx-sdk-11/lib/. Of course, if you have built it yourself, you can point to that location instead.

    Now I can define the module:

    module javafx11 {
        requires javafx.controls;
    
        exports javafx11;
    }
    

    and add the above HelloFX class to the javafx11 package.

    Now the project will run perfectly fine.

    Even if you are still using ant, in this case, the build files are updated to the module-path changes, and there is nothing JavaFX specific, so the project runs without any issue.

    Java project

    If the modular project works, then a regular Java project will work as well on NetBeans 9.0.

    Create a Java project (again, not a JavaFX project), add the JavaFX library to the module-path as above, and now you will need to add these VM options both for compiling and running:

     --module-path=/path/to/javafx-sdk-11/lib --add-modules=javafx.controls
    

    The project should run fine.

    Maven

    Another approach is using Maven or Gradle instead of ant.

    Just create a Maven -> JavaFX project, modify the pom.xml file and add the JavaFX dependencies:

    
        
            org.openjfx
            javafx-controls
            11-ea+23
        
        
            org.openjfx
            javafx-fxml
            11-ea+23
        
    
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.7.0
                
                    11
                
                
                    
                        org.ow2.asm
                        asm
                        6.1.1
                        
                    
                
            
            
                org.codehaus.mojo
                exec-maven-plugin
                    1.2.1
                    
                        
                            
                                java
                            
                        
                    
                    
                        javafx11.HelloFX
                    
            
        
    
    

    Clean, build and run from NetBeans, it should work just fine.

    For Gradle, the same can be done, if you include the Gradle plugin for NetBeans.

提交回复
热议问题