1

I'm using netbeans, and I have this javaFX code which should create a line graph. The code is not mine, I just first want to get javaFX working, and then I'm going to write the class I intended to write.

package javaapplication5;

import javafx.application.Application; 
import static javafx.application.Application.launch;
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 


public class LineGraph extends Application { 
   @Override 
   public void start(Stage stage) {
      //Defining the x axis             
      NumberAxis xAxis = new NumberAxis(1960, 2020, 10); 
      xAxis.setLabel("Years"); 

      //Defining the y axis   
      NumberAxis yAxis = new NumberAxis   (0, 350, 50); 
      yAxis.setLabel("No.of schools"); 

      //Creating the line chart 
      LineChart linechart = new LineChart(xAxis, yAxis);  

      //Prepare XYChart.Series objects by setting data 
      XYChart.Series series = new XYChart.Series(); 
      series.setName("No of schools in an year"); 

      series.getData().add(new XYChart.Data(1970, 15)); 
      series.getData().add(new XYChart.Data(1980, 30)); 
      series.getData().add(new XYChart.Data(1990, 60)); 
      series.getData().add(new XYChart.Data(2000, 120)); 
      series.getData().add(new XYChart.Data(2013, 240)); 
      series.getData().add(new XYChart.Data(2014, 300)); 

      //Setting the data to Line chart    
      linechart.getData().add(series);        

      //Creating a Group object  
      Group root = new Group(linechart); 

      //Creating a scene object 
      Scene scene = new Scene(root, 600, 400);  

      //Setting title to the Stage 
      stage.setTitle("Line Chart"); 

      //Adding scene to the stage 
      stage.setScene(scene);

      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

This code should run, by all accounts. But when I execute it, the program fails and I get the following error:

Error: JavaFX runtime components are missing, and are required to run this application

I'm not sure what's going on, but I need to get this working. Any ideas?

Jason Matthews
  • 65
  • 1
  • 1
  • 6

1 Answers1

1

If you have JDK installed with openJFX, you can easily fix that by the following :

right click in your project -> Properties -> Run -> In VM Options add this: --module-path "path to your OpenJFX folder/lib" --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics

Community
  • 1
  • 1
Diaf Badreddine
  • 550
  • 5
  • 8
  • I have a similar problem. I have the library and the VM options set up and the program runs fine in netbeans, but if I build it; it has the same "JavaFX runtime components are missing, and are required to run this application" error. This happens with any netbeans program I make that when I import the JavaFX libraries. – Edward Eddy67716 Jul 29 '21 at 07:30
  • 1
    You need to specify those vm options when you run your jar file (run the jar file of the app with cmd command with those vm options) – Diaf Badreddine Jul 29 '21 at 12:53
  • I tried that and it says all the paths to my images are invalid. – Edward Eddy67716 Jul 29 '21 at 22:01
  • Is there a way to build it without having to rely on external JavaFX libraries? I want the JavaFX libraries compiled with the program so it does not have to rely on having JavaFX installed on every computer the program is intended to run on. – Edward Eddy67716 Jul 30 '21 at 04:41
  • Yes there always a way, just take a look at javapackager to make a standalone application, or this link may help you [link](https://www.programmersought.com/article/85286990652/) – Diaf Badreddine Aug 01 '21 at 08:19