简体   繁体   English

Eclipse 找不到 Maven 项目的主类

[英]Eclipse cannot find main class of maven project

Before you flag this as a duplicate, please note that I have read several other similar questions, but none of them fixed the issue.在将其标记为重复之前,请注意我已经阅读了其他几个类似的问题,但没有一个解决了该问题。 I have an Eclipse project using maven.我有一个使用 maven 的 Eclipse 项目。 It uses Java 15, with Javafx 13. When I try to run the app, it has the error message:它使用 Java 15 和 Javafx 13。当我尝试运行该应用程序时,它有错误消息:

Error: Could not find or load main class app.cleancode.Start错误:无法找到或加载主类 app.cleancode.Start

I have tried:我试过了:

  1. refreshing the project,刷新项目,
  2. rebuilding from the maven project,从 maven 项目重建,
  3. and even deleting all of the eclipse files and reimporting the project.甚至删除所有 eclipse 文件并重新导入项目。

None of them made any difference.他们都没有任何区别。

I can run any other project in Eclipse, but this one is just not working.我可以在 Eclipse 中运行任何其他项目,但这个项目不起作用。 I am doing this on Windows 10 home.我正在 Windows 10 家庭版上执行此操作。

My pom file :我的 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: 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 **: ** 模块信息.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;
}

Another piece of information I just found out is that if i create a new Start.java with just a main method, it works fine.我刚刚发现的另一条信息是,如果我只用一个 main 方法创建一个新的 Start.java,它就可以正常工作。 Not sure why.不知道为什么。

To answer my own question, the easiest solution is to create a new main class that calls the main of the original start.要回答我自己的问题,最简单的解决方案是创建一个新的主类,该类调用原始 start 的 main。

  1. First, rename Start.java to Starter.java (or whatever you want to call it)首先,将 Start.java 重命名为 Starter.java(或任何您想称呼的名称)
  2. finally, create a new Start.java in the place of the old one:最后,在旧的地方创建一个新的 Start.java:

Start.java:开始.java:

package app.cleancode;

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

Then you can just run the new Start.java.然后你就可以运行新的 Start.java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM