Could not find or load main class Java

10,124

Your code depends on libraries. You have successfully added the libraries in the classpath to compile your code, and have thus successfully created an executable jar files containing your classes. But these classes depend on libraries to run. And when running the jar file, you don't secify anywhere that Java should look for classes in libraries in addition to your jar file.

See Generate manifest class-path from <classpath> in Ant for how to add a Class-Path entry to the manifest of your executbale jar file. Beware: the paths of the libraries must be relatie to the path of the jar.

Ant is a bit outdated. If I were you, I'd try using gradle, which has a neat application plugin doing all that for you, and much more.

Share:
10,124
MobsterSquirrel
Author by

MobsterSquirrel

Updated on June 04, 2022

Comments

  • MobsterSquirrel
    MobsterSquirrel almost 2 years

    Good evening Stack Overflow!

    I started learning Java quite a few days ago, but not using an IDE once my computer is a bit slow. So, I decided to use Sublime Text, and compile things using ant on the cmd, since it seems to be the most reasonable thing to do.

    Today I started ( at least tried ) to follow along a series of LWJGL tutorials (which are really cool), from ThinMatrix, but I can't manage to solve an error which I get every time I try to compile the 'project'.

    This is the structure of my project:

    • LWJGL

      • src
        • com
          • game
            • test
              MainGameLoop.java
            • renderEngine
              DisplayManager.java

      build.xml

    And ladies and gentlemen... the build.xml (following Ant's official HelloWorld tutorial):

    <project name="LWJGL" basedir="." default="main">
    
    <property name="src.dir"     value="src"/>
    
    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="lib.dir"    value="lib"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>
    
    <property name="main-class"  value="com.game.test.MainGameLoop"/>
    
    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    
    </path>
    
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>
    
    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" classpathref="classpath" />
    </target>
    
    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>
    
    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
            <jvmarg value="-Djava.library.path=lib/natives-win" />
        </java>
    </target>
    
    <target name="clean-build" depends="clean,jar"/>
    
    <target name="main" depends="clean,run"/>
    

    Every time I run ant on the command line, inside the root folder of my project, I get this:

    [java] Error: Could not find or load main class com.game.test.MainGameLoop
    [java] Java Result: 1
    

    I'm struggling to understand what is the root problem of this, I kinda sexually abused Google Search trying to find an answer on Java forums, Blogs, and even here...

    I don't usually like populating Stack Overflow with noob questions, but I have to admit I don't know what to do.

    Thanks in advance!