2

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!

MobsterSquirrel
  • 23
  • 1
  • 1
  • 3
  • what do you have at MainGameLoop.java ? – jpganz18 Sep 25 '15 at 22:24
  • `package test; import org.lwjgl.opengl.Display; import renderEngine.DisplayManager; public class MainGameLoop { public static void main(String[] args) { DisplayManager.createDisplay(); while( !Display.isCloseRequested() ) { DisplayManager.updateDisplay(); } DisplayManager.closeDisplay(); } }` – MobsterSquirrel Sep 25 '15 at 22:49
  • Looks like your package name is wrong for that class. It should be com.game.test. I suggest you do a little reading on java as a language. – Alex Fitzpatrick Sep 26 '15 at 01:41

3 Answers3

2

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.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I finally managed to solve this problem that I was struggling with by the help from a good friend of mine. There are two tricky points:

  1. First, when specifying the "Main-Class" in the manifest section, just use the following format "packageName.className", and there is no need to specify the "build" or "source" folders!
  2. Second, try and zip all the jar files that your "jar" file is going to depend on using "zipgroupfileset"

enter image description here

This image shows how I have commented out the "Class-Path" attribute that I would use for addressing the dependencies of the project, and have replaced it with the "zipgroupfileset". I hope that help you as well.

0

Small thing that can cause this issue is using uppercase letters in file extension. For example you can't use MainGameLoop.Java, The correct extension should be all lowercase like below.

MainGameLoop.java

Please check that first.

tk_
  • 16,415
  • 8
  • 80
  • 90