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)