简体   繁体   中英

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. It uses Java 15, with Javafx 13. When I try to run the app, it has the error message:

Error: Could not find or load main class app.cleancode.Start

I have tried:

  1. refreshing the project,
  2. rebuilding from the maven project,
  3. and even deleting all of the eclipse files and reimporting the project.

None of them made any difference.

I can run any other project in Eclipse, but this one is just not working. I am doing this on Windows 10 home.

My pom file :

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

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. 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.

  1. First, rename Start.java to Starter.java (or whatever you want to call it)
  2. finally, create a new Start.java in the place of the old one:

Start.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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