0

SOLVED!

I have a JavaFX project built in Eclipse and it runs fine within Eclipse, but I can't seem to get it to run via the command line. I get many errors, but I think they all originate from this first error:

package javafx.application does not exist

When trying to run from the command line, I navigate to the SRC>Application>program.java and try to compile with javac program.java.

I had to go through a rather long-winded process to get JavaFX set up for Eclipse, so I think it is something to do with that. All help is appreciated.

Thanks in advance!

Java: 11.0.9.1, JavaFX: 15.0.1

Minimal Reproducible Example:

1.Create a JavaFX project in Eclipse. Mine is Structured as follows within my SRC folder:

SRC>Application>Shapes.java

Shapes.java has the following code:

package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Polygon;
import javafx.scene.control.Button;

public class Shapes extends Application {
    
    //Value used to get regular polygons.
    double sine_number = 50*Math.sqrt(3);
    
    //Method to create an initial shape (triangle)
    public Polygon create_triangle() {
        
        Polygon triangle = new Polygon();
        triangle.getPoints().addAll(
            100.0, 200.0+sine_number,
            300.0, 200.0+sine_number,
            200.0, 200.0-sine_number);
        
        triangle.setStroke(Color.BLACK);
        
        return triangle;
    }
    
    //Method used to create buttons
    public Button create_button(double x, double y, double w, double h, String text, String type, String condition, Polygon polygon) {
        
        Button button = new Button();
        //Set button position and size
        button.setLayoutX(x);
        button.setLayoutY(y);
        button.setPrefWidth(w);
        button.setPrefHeight(h);
        button.setText(text);
        
        //Set button style and action
        if (type == "shape"){
            
            button.setStyle("-fx-font-size:16;-fx-text-fill:black;-fx-background-color:white;");
            
            switch(condition) {
                case "triangle":
                    button.setOnAction(e -> polygon.getPoints().setAll(
                        100.0, 200.0+sine_number,  
                        300.0, 200.0+sine_number,
                        200.0, 200.0-sine_number));
                    break;
                    
                case "rectangle":
                    button.setOnAction(e -> polygon.getPoints().setAll(
                        125.0, 150.0,
                        275.0, 150.0, 
                        275.0, 250.0, 
                        125.0, 250.0));
                    break;
                    
                case "hexagon":
                    button.setOnAction(e -> polygon.getPoints().setAll(
                        150.0, 200-sine_number,
                        250.0, 200.0-sine_number,
                        300.0, 200.0, 250.0, 200.0+sine_number,
                        150.0, 200.0+sine_number,100.0, 200.0));
                    break;
                    
            }   
        }
        else if (type == "colour") {
            
            button.setStyle("-fx-font-size:16;-fx-text-fill:white;-fx-background-color:" + condition + ";");
            button.setOnAction(e -> polygon.setFill(Color.web(condition)));
        }
        
        
        return button;
        
    }
    
    //Method used to create all of the various nodes needed for the application and groups them
    public Group create_nodes() {
        
        Rectangle menuBox = new Rectangle(0, 400, 400, 150);
        menuBox.setStroke(Color.BLACK);
        Polygon polygon = create_triangle();

        Button triangleButton = create_button(25,430,100,30,"Triangle","shape","triangle",polygon);
        Button rectangleButton = create_button(150,430,100,30,"Rectangle","shape","rectangle",polygon);
        Button hexagonButton = create_button(275,430,100,30,"Hexagon","shape","hexagon",polygon);
        Button greenButton = create_button(25,490,100,30,"Green","colour","#5ac18e",polygon);
        Button greyButton = create_button(150,490,100,30,"Grey","colour","#808080",polygon);
        Button redButton = create_button(275,490,100,30,"Red","colour","#f6546a",polygon);
        
        Group group = new Group(menuBox,triangleButton, rectangleButton,hexagonButton, greenButton, greyButton, redButton,polygon);
        
        return group;
    }
    
    
    @Override
    public void start(Stage stage) {

        Group nodes_all = create_nodes();
        Group root = new Group(nodes_all);
        Scene scene = new Scene(root, 400, 550, Color.web("#dddddd",1.0));
        stage.setScene(scene);
        stage.show(); 
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

When trying to run this project in the command line I first typed:

set PATH_TO_FX="C:\Users\PC\Downloads\openjfx-15.0.1_windows-x64_bin-sdk\javafx-sdk-15.0.1\lib"

I then navigated to my project>src>application and typed:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls Shapes.java

Then for the final step, I move back up into the src folder in the cmd line and I type:

java --module-path %PATH_TO_FX% --add-modules javafx.controls application.Shapes

This will solve the error message:

Error: Could not find or load main class Shapes Caused by: java.lang.NoClassDefFoundError: application/Shapes (wrong name: Shapes)

Charmalade
  • 645
  • 1
  • 6
  • 14
  • What version of Java and JavaFX are you using? – Andreas Feb 02 '21 at 17:52
  • 1
    If you had to go through a "long-winded process" to setup JavaFX in Eclipse then I assume you are using a version of Java/JavaFX where JavaFX is not included in the JDK. You have to use the same arguments when you execute `javac` manually as you told Eclipse to use. – Slaw Feb 02 '21 at 17:56
  • 2
    Did you follow the instructions? [Getting Started with JavaFX](https://openjfx.io/openjfx-docs/#install-javafx) – Andreas Feb 02 '21 at 17:58
  • I just followed the instructions and the javac seemed to run without errors, but when trying to run the program I get: Error: Could not find or load main class program – Charmalade Feb 02 '21 at 19:14
  • [mcve] please .. here that would be the cmd lines and the complete stacktrace – kleopatra Feb 02 '21 at 19:51
  • @Charmalade And did you add the necessary arguments when you executed `java` as well? Remember that compiling in Java does not create some sort of statically-linked library. You need to point to needed dependencies and provide needed configuration at both compile-time _and_ run-time. Though note that some dependencies and configuration may only be needed for one or the other. For more specific help, please provide a [mre] and the exact errors in the question (as properly formatted text). – Slaw Feb 02 '21 at 22:37
  • I added some more information, hopefully it is enough for a reproducible example. Thanks for helping me all. – Charmalade Feb 02 '21 at 23:17
  • Currently Java is looking for the class `application.Shapes` in the working directory. That's not where the class is located. Based on your command to `javac` I assume it's actually in the `src` directory (something like `src/application/Shapes.class`). You need to tell Java where _your_ code is in addition to where the dependencies are. Try adding `-cp src` to your command (`-cp` is shorthand for `--class-path`). If you ever make your code modular you'd instead put it on the `--module-path` along with JavaFX (and any other modules) and launch the app with `--module`. – Slaw Feb 03 '21 at 02:09
  • This may help somewhat: https://stackoverflow.com/questions/55652036/how-to-start-javafx-11-application-outside-ide/55653652#55653652 – Slaw Feb 03 '21 at 02:12

0 Answers0