错误:无法找到或加载主类 com.github.shia5347.terratoolbox.App

如何解决错误:无法找到或加载主类 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 打印程序,而不是扩展 BasicGameslick2d 窗口程序,结果却奏效了。

我也尝试将 <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

转到终端中的文件夹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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?