0

解决了!

我有一个在 Eclipse 中构建的 JavaFX 项目,它在 Eclipse 中运行良好,但我似乎无法让它通过命令行运行。我收到很多错误,但我认为它们都源于第一个错误:

package javafx.application does not exist

当尝试从命令行运行时,我导航到 SRC>Application>program.java 并尝试使用 javac program.java 进行编译。

我必须经历一个相当冗长的过程才能为 Eclipse 设置 JavaFX,所以我认为这与此有关。感谢所有帮助。

提前致谢!

Java:11.0.9.1,JavaFX:15.0.1

最小可重现示例:

1.在Eclipse中创建一个JavaFX项目。我的 SRC 文件夹中的结构如下:

SRC>Application>Shapes.java

Shapes.java 有以下代码:

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);
    }
}

当尝试在命令行中运行这个项目时,我首先输入:

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

然后我导航到我的项目>src>应用程序并输入:

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

然后对于最后一步,我回到 cmd 行中的 src 文件夹并输入:

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

这将解决错误消息:

错误:无法找到或加载主类 Shapes 原因:java.lang.NoClassDefFoundError: application/Shapes(错误名称:Shapes)

4

0 回答 0