4

I am trying to run JavaFX 11 on NetBeans 9 Since JDK 11 does not have JavaFX anymore I couldn't get NetBeans to run a JavaFX project, it says: "Failed to automatically set-up a JavaFX Platform."

I then downloaded the javafx11 from this website https://gluonhq.com/products/javafx/, after following the tutorial I was able to compile and run a JavaFX class normally through terminal. The only way I could add JavaFX is by using Maven, but I can't run the application even that it was built successfully.

    Error: JavaFX runtime components are missing, and are required to run this application
 Command execution failed.

Is there any other way to run JavaFX 11 with NetBeans? or maybe Eclipse?

Outman
  • 3,100
  • 2
  • 15
  • 26
kalix
  • 57
  • 1
  • 6
  • 1
    It is separated into **OpenJFX** now, as it was already some time for Linux. – Joop Eggen Sep 27 '18 at 09:35
  • exactly but i cant run it in IDE – kalix Sep 27 '18 at 09:40
  • 1
    See this [question](https://stackoverflow.com/questions/51987518/javafx-deployment-library-not-found-in-active-jdk), already has a solution for NetBeans 9 – José Pereda Sep 27 '18 at 09:43
  • i did try it but it also didn't work as i said Running on terminal method worked with me. Modular Project :always gives an error in module. maven way: it build but doesn't run. – kalix Sep 27 '18 at 09:51
  • Then try to follow the steps provided in the answer, all of the different possibilities should be working for you too. – José Pereda Sep 27 '18 at 10:09
  • i think the problem is with exec-maven-plugin because netbeans error says: --- exec-maven-plugin:1.5.0:exec (default-cli) @ last2 --- Error: JavaFX runtime components are missing, and are required to run this application but it works when i run it in terminal using: mvn compile exec:java – kalix Sep 27 '18 at 14:40
  • the only way i could run the app is by editing the execute Goals in the Project Properties ->Actions to "exec:java" which is of course not the best way to do it – kalix Sep 27 '18 at 15:43
  • Looks like your using version `1.5.0` of the exec-maven-plugin. The latest version is `1.6.0`. Try using the latest version to see if that helps. – Slaw Sep 28 '18 at 15:14
  • unfortunately i did try it with 1.6.0 but still no luck – kalix Sep 28 '18 at 15:40
  • Can you edit your question and post the main files of your project, a screenshot of the project structure, and something that allows us to reproduce your issue? – José Pereda Sep 28 '18 at 17:06

2 Answers2

3

Maybe a late answer, but, it is possible to have javafx 11 maven project + NetBeans 9.

  1. Maven doesn't like extending Application in a main clas. You should use two classes:

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>
    

This will work in netbeans without any issues. Basically, what you need to do is start new maven project and after you finish setting project just replace pom.xml.

This way your code will run from IDE, and will compile correctly so that you can run it from command line with java --jar. Also, you will be able to run NB debugger.

1

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.

Stolen from javafx documentation

Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41