javamavenvisual-studio-codepom.xmlslick2d

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


The problem

When I try to run my jar executable by specifying the slick2d library as a classpath: java -jar TerraToolBox-1.0-SNAPSHOT.jar -classpath org.newdawn.slick ; it gives an error saying Error: Could not find or load main class com.github.shia5347.terratoolbox.App. I also tried a simple hello world printing program rather than the slick2d window program extending BasicGame and that worked instead.

I had also tried adding <addClasspath>true</addClasspath> to the maven-jar-plugin section but it still did not work.

Using 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

main

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>

Manfiest file after setting addClasspath to true in pom.xml

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

Solution

  • Ok I just decided to use libgdx instead. It worked with maven using the maven-assembly-plugin. I justed typed mvn clean compile assembly:single which would then use the assembly plugin. Here is the plugin for it:

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