Must solve the JavaFx lack of JavaFX runtime component error and Application burst red problem

Article directory

  • Notice
  • maven project creation
  • Maven + javafx project configuration

Note

The following problems are purely because the new project is a normal project, not a Java FX project. If the new project is a Java FX project, then idea will automatically generate the corresponding required pom.xml file for you, and the operation will be normal.

Maven project creation

Create a normal project, the build system uses Maven

Project structure:

There are main and test under src

There are java and resources under main

There are java and test resources under test

The test resources are not automatically created. In order to improve the project structure, one will be created manually

The most critical pom.xml file initialization is like this

After that, configure the pom.xml file according to the needs of the project

Learning maven is learning how to configure the pom.xml file

maven + javafx project configuration

Want to create a javafx project

First, clarify the dependencies that may need to be added.
First add dependencies on javafx in pom.xml

Because javafx.fxml may be used in javafx, the dependency of javafx.fxml should also be added

Maybe you need to connect to the database later, so you need to add the dependency of mysql-connector-java

Configure pom.xml file
Before adding, it is necessary to understand the structure of the pom file

You can view the primary and secondary structure after formatting the document

Take the pom.xml file I configured this time as an example

<?xml version="1.0" encoding="UTF-8"?>

<!-- project is the highest level -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- Project coordinates -->
    <groupId>groupId</groupId>
    <artifactId>fx1</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <!-- Dependency addition -->
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
    </dependencies>

    <!-- Build items -->
    <build>
        <!-- Add plug-in -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <!-- <release>8</release>-->
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>sample.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>

        <!--I don’t know what this is, I’ll check it later-->
<!-- <resources>-->
<!-- <resource>-->
<!-- & amp;lt;! & amp;ndash; here is placed in src/main/java & amp;ndash; & amp;gt;-->
<!-- <directory>src/main/java</directory>-->
<!-- <includes>-->
<!-- <include>**/*.properties</include>-->
<!-- <include>**/*.fxml</include>-->
<!-- <include>**/fxml/*.fxml</include>-->
<!-- & amp;lt;! & amp;ndash; If you want to get a package name specifically for the fxml file, add the settings like the previous line & amp;ndash; & amp;gt;-->
<!-- & amp;lt;! & amp;ndash; After that, use getResource("fxml/xx.fxml") like this & amp;ndash; & amp;gt;-->
<!-- </includes>-->
<!-- <filtering>false</filtering>-->
<!-- </resource>-->
<!-- </resources>-->

    </build>

</project>


Add dependency template using alt+insert shortcut key in pom.xml file

Add the dependencies you think you may need above

There is a troublesome point when adding. You have to specify the version of the dependent package according to the version of the software you are using.

For example, in the above dependency management, the javafx-controls dependency package and javafx-fxml dependency package from org.openjfx company need to clearly know the version of jdk they are using, so as to indicate the versions of these two dependency packages.

The mysql-connector-java dependency package from mysql company needs to clearly know its own version of mysql, so as to indicate the version of the connection dependency package.

So you have to go to cmd to check the versions of some of your software.

But there is a little trick in idea. First fill in the name of the dependent package, and then press the space when filling in the other two. Idea will automatically display some versions, which may be accurate or inaccurate (for example, my mysql version is 8, But the version of the link dependency package shown to me by idea is only version 5). If it is not accurate, I have to check it myself and enter it.

Once versions are involved, there may be problems with software versions not corresponding to each other. For example, mysql8 cannot use version 5 of the connection dependency package.

? In addition, not only do you need to add dependencies, but you also need to add some corresponding plug-ins.

How do I know the name of the plug-in to add? I still don’t know.

After adding, click maven on the sidebar to refresh the configuration file

Wait for dependencies and plugins to download and the program is ready to run

It is much more convenient than the previous method of adding several jar packages.

When using many different jar packages, the convenience of maven, a dependency management tool, is even more evident.

When running a javafx program, you need to create two classes, an app class and an appLaunch class, because running a Javafx program requires checking the running components.

If you run the app class directly under the maven build, an error will be reported.

At this time, you need to create an appLaunch class to run the app class, and then run appLaunch to run normally.

There is only one sentence in this appLaunch class

Application.launch(Login.class);
// Note that the compiled class file is run

But at the same time, there are still some unresolved problems, which are very uncomfortable:
The detailed configuration of the pom.xml file is still unclear. Every configuration may require a lot of search and trial and error. You should continue to read some documents about pom files.
It’s not clear how to search for relevant required plug-ins in the pom.
Why can’t you directly run javafx programs under maven build? You must create an additional running class; just like running c/c++ programs directly in vscode will cause the running window to crash, and you must add a system(“pause”) ;
I’m tired today, so I’ll explore this first, and spend the rest of the time taking a look at the data structure. I’m going to be a teaching assistant tomorrow.