Netbeans 9.0与JavaFx 11和JDK 11的搭配

4
我尝试在NetBeans 9上运行JavaFX 11,但由于JDK 11不再具有JavaFX,我无法使NetBeans运行JavaFX项目。它显示:"无法自动设置JavaFX平台。"
然后我从https://gluonhq.com/products/javafx/下载了javafx11,在按照教程操作后,我能够通过终端正常编译和运行JavaFX类。我只能通过Maven添加JavaFX,但即使构建成功,我也无法运行该应用程序。
    Error: JavaFX runtime components are missing, and are required to run this application
 Command execution failed.

有没有其他方法在NetBeans或者Eclipse中运行JavaFX 11?

1
现在它已经分成了 OpenJFX,因为它已经有一段时间适用于Linux。 - Joop Eggen
但是我无法在IDE中运行它。 - kalix
1
请参考此问题,已经有了NetBeans 9的解决方案。 - José Pereda
我尝试过了,但它也没有起作用。就像我说的,在终端上运行的方法对我有效。模块化项目:模块总是出错。Maven方式:它可以构建,但无法运行。 - kalix
我能运行该应用程序的唯一方法是通过编辑项目属性->操作中的执行目标为"exec:java",这当然不是最好的做法。 - kalix
显示剩余5条评论
2个回答

3

也许有点晚了,但是,在NetBeans 9中可以创建JavaFX 11 Maven项目。

  1. Maven不支持在主类中扩展Application。您应该使用两个类:

Main.java:

public class Main {

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

MainApp.java:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MainApp extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        String a;
        a = new String("test");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}
  1. Your pom.xml should look like this:

        <?xml version="1.0" encoding="UTF-8"?>
        <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>
    
            <groupId>YOUR GROUP ID</groupId>
            <artifactId>YOUR ARTIFACT ID</artifactId>
            <version>YOUR VERSION</version>
    
            <dependencies>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-controls</artifactId>
                    <version>11</version>
                </dependency>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-media</artifactId>
                    <version>11</version>
                </dependency>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-graphics</artifactId>
                    <version>11</version>
                </dependency>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-fxml</artifactId>
                    <version>11</version>
                </dependency>
            </dependencies>
    
            <build>
    
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.8.0</version>
                        <configuration>
                            <source>11</source>
                            <target>11</target>
    
                            <fork>true</fork>
                            <executable>PATH TO javac binary i.e. /usr/local/java/jdk-11.0.1/bin/javac</executable>
    
    
                            <compilerArgs>
                                <arg>-verbose</arg>
                                <arg>-Xlint:unchecked</arg>
                            </compilerArgs>
    
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>Main</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.2.1</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <mainClass>Main</mainClass>
                        </configuration>
                    </plugin>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>3.1.0</version>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>Main</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <name>YOUR ARTIFACT ID</name>
        </project>
    

这在NetBeans中可以毫无问题地实现。基本上,您需要启动新的Maven项目,完成项目设置后只需替换pom.xml文件即可。

这样,您的代码将可以从IDE中运行,并且将正确编译以便您可以使用“java --jar”命令从命令行运行它。此外,您还将能够运行NB调试器。


Netbeans 10 的一个帮助我的更新:https://www.youtube.com/watch?v=RCdBmPfzeyM - user1156544
你也在创建Maven项目。我不明白有什么不同? - Igor Smitran
这个人给出了完整的答案,包括依赖项和nbactions.xml更改,我按照他的方法在Netbeans 10中成功实现了预期的效果。 - user1156544

1
自Java 11启动器以来,会检查主类是否扩展javafx.application.Application。如果是这种情况,则需要在模块路径上具有javafx.graphics模块,否则会显示此错误。
javafx文档中获取。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接