eclipse找不到maven项目的主类

raogr8fs  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(298)

在您将此标记为副本之前,请注意,我已经阅读了其他几个类似的问题,但没有一个解决了这个问题。我有一个使用maven的eclipse项目。它使用Java15和JavaFX13。当我尝试运行应用程序时,它会显示错误消息:
错误:找不到或加载主类app.cleancode.start
我试过:
刷新项目,
从maven项目重建,
甚至删除所有eclipse文件并重新导入项目。
他们都没什么不同。
我可以在eclipse中运行任何其他项目,但这个项目不起作用。我在windows10home上做这个。
我的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);
}
}

module-info.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,它就可以正常工作。不知道为什么。

f45qwnt8

f45qwnt81#

回答我自己的问题,最简单的解决方案是创建一个新的main类,调用原始start的main。
首先,将start.java重命名为starter.java(或者任何你想叫它的名字)
最后,在旧的start.java中创建一个新的start.java:
开始.java:

package app.cleancode;

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

然后就可以运行新的start.java了。

相关问题