Error: Could not find or load main class com.github.shia5347.terratoolbox.App

Error: Could not find or load main class com.github.shia5347.terratoolbox.App

问题

当我尝试通过将 slick2d 库指定为类路径来 运行 我的 jar 可执行文件时: java -jar TerraToolBox-1.0-SNAPSHOT.jar -classpath org.newdawn.slick ;它给出了一个错误 Error: Could not find or load main class com.github.shia5347.terratoolbox.App。我还尝试了一个简单的 hello world 打印程序,而不是扩展 BasicGame 的 slick2d window 程序,但它起作用了。

我也曾尝试将 <addClasspath>true</addClasspath> 添加到 maven-jar-plugin 部分,但它仍然不起作用。

使用 jdeps

jdeps TerraToolBox-1.0-SNAPSHOT.jar

TerraToolBox-1.0-SNAPSHOT.jar -> not found
TerraToolBox-1.0-SNAPSHOT.jar -> /usr/lib/jvm/java-8-openjdk/jre/lib/rt.jar
   com.github.shia5347.terratoolbox (TerraToolBox-1.0-SNAPSHOT.jar)
      -> java.lang                                          
      -> java.util.logging                                  
      -> org.newdawn.slick                                  not found

主要

package com.github.shia5347.terratoolbox;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class App extends BasicGame
{
    public App(String gamename)
    {
        super(gamename);
    }

    @Override
    public void init(GameContainer gc) throws SlickException {}

    @Override
    public void update(GameContainer gc, int i) throws SlickException {}

    @Override
    public void render(GameContainer gc, Graphics g) throws SlickException
    {
        g.drawString("Howdy!", 10, 10);
    }

    public static void main(String[] args)
    {
        try
        {
            AppGameContainer appgc;
            appgc = new AppGameContainer(new App("Simple Slick Game"));
            appgc.setDisplayMode(640, 480, false);
            appgc.start();
        }
        catch (SlickException ex)
        {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.github.shia5347.terratoolbox</groupId>
  <artifactId>TerraToolBox</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>TerraToolBox</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

<dependency>
    <groupId>javax.jnlp</groupId>
    <artifactId>jnlp-api</artifactId>
    <version>7.0</version>
    <scope>system</scope>
    <systemPath>/home/shahroz/netx-0.5/netx.jar</systemPath>
</dependency>


  <!-- https://mvnrepository.com/artifact/org.slick2d/slick2d-core -->
<dependency>
    <groupId>org.slick2d</groupId>
    <artifactId>slick2d-core</artifactId>
    <version>1.0.2</version>
</dependency>

  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

      <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
              <addClasspath>true</addClasspath>
                            <mainClass>com.github.shia5347.terratoolbox.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>


      </plugins>
    </pluginManagement>
  </build>
</project>

在 pom.xml

中将 addClasspath 设置为 true 后的 Manfiest 文件
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: shahroz
Class-Path: slick2d-core-1.0.2.jar lwjgl-2.9.3.jar lwjgl-platform-2.9.
 3-natives-windows.jar lwjgl-platform-2.9.3-natives-linux.jar lwjgl-pl
 atform-2.9.3-natives-osx.jar jinput-2.0.5.jar jutils-1.0.0.jar jinput
 -platform-2.0.5-natives-linux.jar jinput-platform-2.0.5-natives-windo
 ws.jar jinput-platform-2.0.5-natives-osx.jar jorbis-0.0.17.jar
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_275
Main-Class: com.github.shia5347.terratoolbox.App

首先,确保您的 java 环境配置正确。

我觉得可能是因为代码中加入了包名,在编译的.class文件中也生成了包名,但是在[=处找不到com.github.shia5347.terratoolbox 30=]次。

在Terminal中打开文件夹terratoolbox,然后运行javac -d. .\App.java,会生成一个.class文件。

然后还在terratoolbox文件夹下,运行java com.github.shia5347.terratoolbox.App.

试试这个,看看问题是否消失。

好的,我只是决定改用 libgdx。它使用 maven-assembly-plugin 与 maven 一起工作。我刚刚输入 mvn clean compile assembly:single ,然后将使用程序集插件。这是它的插件:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
      <addClasspath>true</addClasspath>
        <mainClass>com.github.shia5347.terratoolbox.App</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>