带有 JavaFx 11 和 JDK 11 的 Netbeans 9.0
Netbeans 9.0 with JavaFx 11 and JDK 11
我正在尝试 运行 NetBeans 9 上的 JavaFX 11
由于 JDK 11 不再有 JavaFX,我无法让 NetBeans 进入 运行 JavaFX 项目,它说:"Failed to automatically set-up a JavaFX Platform."
然后我从这个网站下载了 javafx11 https://gluonhq.com/products/javafx/,
遵循本教程后,我能够通过终端正常编译和 运行 JavaFX class。
我可以添加 JavaFX 的唯一方法是使用 Maven,但我无法 运行 应用程序,即使它已成功构建。
Error: JavaFX runtime components are missing, and are required to run this application
Command execution failed.
是否有任何其他方式 运行 JavaFX 11 与 NetBeans?或者 Eclipse?
也许是一个迟到的答案,但是,javafx 11 maven 项目 + NetBeans 9 是可能的。
- Maven 不喜欢在主类中扩展应用程序。你应该使用两个 类:
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();
}
}
您的 pom.xml 应如下所示:
<?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 调试器。
This error is shown since the Java 11 launcher checks if the main class extends javafx.application.Application. If that is the case, it is required to have the javafx.graphics module on the module-path.
我正在尝试 运行 NetBeans 9 上的 JavaFX 11
由于 JDK 11 不再有 JavaFX,我无法让 NetBeans 进入 运行 JavaFX 项目,它说:"Failed to automatically set-up a JavaFX Platform."
然后我从这个网站下载了 javafx11 https://gluonhq.com/products/javafx/, 遵循本教程后,我能够通过终端正常编译和 运行 JavaFX class。 我可以添加 JavaFX 的唯一方法是使用 Maven,但我无法 运行 应用程序,即使它已成功构建。
Error: JavaFX runtime components are missing, and are required to run this application
Command execution failed.
是否有任何其他方式 运行 JavaFX 11 与 NetBeans?或者 Eclipse?
也许是一个迟到的答案,但是,javafx 11 maven 项目 + NetBeans 9 是可能的。
- Maven 不喜欢在主类中扩展应用程序。你应该使用两个 类:
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();
}
}
您的 pom.xml 应如下所示:
<?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 调试器。
This error is shown since the Java 11 launcher checks if the main class extends javafx.application.Application. If that is the case, it is required to have the javafx.graphics module on the module-path.