首页 > 解决方案 > Eclipse找不到maven项目的主类

问题描述

在您将此标记为重复之前,请注意我已阅读其他几个类似的问题,但没有一个解决了这个问题。我有一个使用 Maven 的 Eclipse 项目。它使用 Java 15 和 Javafx 13。当我尝试运行该应用程序时,它有错误消息:

错误:无法找到或加载主类 app.cleancode.Start

我努力了:

  1. 刷新项目,
  2. 从maven项目重建,
  3. 甚至删除所有的eclipse文件并重新导入项目。

他们都没有任何区别。

我可以在 Eclipse 中运行任何其他项目,但是这个项目不起作用。我在 Windows 10 家庭版上执行此操作。

我的 pom 文件

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>app.cleancode</groupId>
  <artifactId>javafx-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
  <java.version>15</java.version>
  <javafx.version>13</javafx.version>
  </properties>
  <dependencies>
  <dependency>
  <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>${javafx.version}</version>
  </dependency>
  <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-swing</artifactId>
            <version>${javafx.version}</version>
        </dependency>
  </dependencies>
  <build>
  <plugins>
  <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${java.version}</release>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <compilerArgs>--enable-preview</compilerArgs>
                </configuration>
            </plugin>
  <plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.3</version>
    <configuration>
    <mainClass>app.cleancode.Start</mainClass>
    </configuration>
  </plugin>
  </plugins>
  </build>
</project>

app.cleancode.Start:

package app.cleancode;

import app.cleancode.game.GameListener;
import app.cleancode.game.GameLoop;
import app.cleancode.game.GameObject;
import app.cleancode.game.PhysicalLaw;
import app.cleancode.game.physics.Gravity;
import app.cleancode.game.physics.Movement;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Start extends Application {
    private static GameListener[] gameListeners = new GameListener[] {
            
    };
    @SuppressWarnings("unchecked")
    private static GameObject<Node> [] gameObjects = new GameObject[] {
            
    };
    private static PhysicalLaw[] laws = new PhysicalLaw[] {
            new Movement(),
            new Gravity()
    };
public static void main(String[] args) {
    launch(args);
}
private Pane nodes = new Pane();
 private Pane gamePane = new Pane();
@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(gamePane);
    scene.getStylesheets().add("/app/cleancode/app.css");
    nodes.getChildren().add(new Text("Loading"));
    primaryStage.setTitle("Game");
    primaryStage.setFullScreen(true);
    primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    primaryStage.setScene(new Scene(nodes));
    primaryStage.show();
    for(GameListener listener : gameListeners) {
        for(String gameObjectName : listener.getGameObjects()) {
            for(GameObject<Node> gameObject : gameObjects) {
                gameObject.addNode = this::addNode;
                if(gameObject.getName().equalsIgnoreCase(gameObjectName)) {
                    try {
                        var gameObjectField = listener.getClass().getDeclaredField(gameObjectName);
                        gameObjectField.set(listener, gameObject);
                        break;
                    }catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        listener.startup();
    }
    scene.setFill(Color.BLACK);
    primaryStage.setScene(scene);
    primaryStage.setFullScreen(true);
    GameLoop loop = new GameLoop(this::tick);
    loop.start();
}
public void tick() {
    for(GameListener gameListener : gameListeners) {
        gameListener.update();
    }
    for(PhysicalLaw law : laws) {
        for(GameObject<Node> gameObject : gameObjects) {
            law.handle(gameObject);
        }
    }
}
public void addNode(Node node) {
    gamePane.getChildren().add(node);
}
}

** 模块信息.java **:

module app.cleancode.javafx-app {
    exports app.cleancode.axis;
    exports app.cleancode.animation;
    exports app.cleancode;
    exports app.cleancode.sound;
    exports app.cleancode.game.physics;
    exports app.cleancode.bounds;
    exports app.cleancode.game.snake;
    exports app.cleancode.map;
    exports app.cleancode.sprite;
    exports app.cleancode.game;

    requires java.desktop;
    requires javafx.base;
    requires javafx.graphics;
    requires javafx.media;
    requires javafx.swing;
}

我刚刚发现的另一条信息是,如果我只用一个 main 方法创建一个新的 Start.java,它就可以正常工作。不知道为什么。

标签: javaeclipsemavenjavafx

解决方案


要回答我自己的问题,最简单的解决方案是创建一个新的主类,该类调用原始启动的主类。

  1. 首先,将 Start.java 重命名为 Starter.java(或任何你想调用的名称)
  2. 最后,创建一个新的 Start.java 来代替旧的:

启动.java:

package app.cleancode;

public class Start {
public static void main(String[] args) {
Starter.main(args);
}
}

然后你就可以运行新的 Start.java。


推荐阅读