Help ANT and LWJGL !

Hi all,

I would like to use ‘Ant’ to compile and create the ‘.jar’ of my project.
But I can not properly integrate the LWJGL library, I wonder what I should put in the ‘Ant task’ which I use at compile time, the launch and creation of the executable file (’.jar’).

The three ‘Ant task’:
Compilation:


<target name="compile" depends="init"
		description=" : compilation des programmes sous le repertoire sources">
      <mkdir dir="${pathClass}"/>
      <javac srcdir="${pathSrc}" destdir="${pathClass}" source="${source}"
        debug="on" debuglevel="lines,vars,source"
		  compiler="${compiler}" includeantruntime="false">
        <classpath refid="projectClasspath"/>
      </javac>
</target>

Running:


<target name="run" depends="compile"
		description=" : execution de la classe principale de l application">
      <java classname="${MainClass}" classpath="${pathClass}" fork="true">
        <assertions>
            <enable/>
        </assertions>
      </java>
</target>

Creat .jar:


<target name="build" depends="compile"
		description=" : construction d une application double-clickable">
      <jar destfile="${pathBuild}/${JarFile}" index="true">
            <manifest>
                <attribute name="Main-Class" value="${MainClass}" />
                <attribute name="Built-By"   value="${user.name}"/>
            </manifest>
            <fileset dir="${pathClass}"/>
            <fileset dir="${basedir}">
                <include name="${dirRes}/"/>
            </fileset>
      </jar>
</target>

How do I do to integrate the library in my project?

  1. You need to include the lwjgl jars in the “projectClasspath”:


  2. You need to add -Djava.library.path to point to the lwjgl native files in the run task:
    <java…>


I’ve this error :


     [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
     [java] 	at net.battlesquare.src.Battlesquare.<init>(Battlesquare.java:16)
     [java] 	at net.battlesquare.src.BattlesquareImpl.<init>(BattlesquareImpl.java:14)
     [java] 	at net.battlesquare.client.BattlesquareClient.main(BattlesquareClient.java:21)
     [java] Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
     [java] 	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
     [java] 	at java.security.AccessController.doPrivileged(Native Method)
     [java] 	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
     [java] 	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
     [java] 	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
     [java] 	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
     [java] 	... 3 more

tom is right. Your run task must set both the Java library path and the class path. If you want, I can show you an example of script doing the same thing, I just need to adapt it for LWJGL. In mine, I use several tasks to create the JAR, sign all JARs, create the JNLP file, run the game, etc…

You need to use some conditions to build the Java library path correctly whatever the OS and the architecture. I did it and it works fine :slight_smile:

I fixed the bug for the Compile and Run Ant Task… but… don’t for the Creat .jar task…

My build.xml :


<?xml version="1.0" encoding="utf-8"?>
<project name="..." default="but">
  <target name="init" depends="initprojet,initPerso,initstructure,initClasspath"/>

  <target name="but"
		description=" : cible par defaut" depends="run"/> 

  <target name="initprojet" 
			description=" : initialisation des proprietes du projet">
	<property name="MainClass"      value="MainClass"/>
	<property name="TestClass"      value="TestClass"/>
	<property name="build.compiler" value="javac1.5"/>
	<property name="JarFile"        value="JarFile.jar"/>
    <property name="charset"        value="utf8"/>
	<property name="compiler"       value="javac1.6"/>
	<property name="source"         value="1.6"/>
    <!-- You OS : macosx, linux, windows -->
    <property name="os"             value="macosx"/>
  </target>
  
  <target name="initPerso"
			description=" : initialisation des chemins sur son ordinateur">
    <property name="pathAPI"    	value="file:///Ressources/javadoc/api"/>
  </target>
  
  <target name="initstructure"
			description=" : initialisation des proprietes liees a la stucture">
      <property name="dirRes"     value="ress"/>
      <property name="dirClass"   value="class"/>
      <property name="dirDocs"    value="doc"/>
      <property name="dirSource"  value="src"/>
      <property name="dirBuild"   value="build"/>
      <property name="dirLib"     value="lib"/>

      <property name="pathSrc"    value="${basedir}/${dirSource}"/>
      <property name="pathClass"  value="${basedir}/${dirClass}"/>
      <property name="pathDocs"   value="${basedir}/${dirBuild}/${dirDocs}"/>
      <property name="pathRes"    value="${basedir}/${dirRes}"/>
      <property name="pathBuild"  value="${basedir}/${dirBuild}"/>
      <property name="pathLib"    value="${basedir}/${dirLib}"/>
	  
	  <mkdir dir="${pathClass}"/>
  </target>
  
  <target name="initClasspath"
		description=" : initialisation des chemins d'acces aux classes">
	  <path id="projectClasspath">
		<pathelement path="${pathClass}/"/>
		<pathelement path="${pathLib}/junit/junit-4.8.2.jar"/>
		<pathelement path="${pathLib}/lwjgl/lwjgl.jar"/>
		<pathelement path="${pathLib}/lwjgl/lwjgl_util.jar"/>
	  </path>
      <property name="pathNativeLwjgl" value="${pathLib}/lwjgl/${os}" />
  </target>

   <target name="compile" depends="init"
		description=" : compilation des programmes sous le repertoire sources">
      <mkdir dir="${pathClass}"/>
      <javac srcdir="${pathSrc}" destdir="${pathClass}" source="${source}"
        debug="on" debuglevel="lines,vars,source"
		  compiler="${compiler}" includeantruntime="false">
        <classpath refid="projectClasspath"/>
      </javac>
      <!-- For uzip all library in the class path
      <unzip dest="${pathClass}">
        <fileset dir="${pathLib}">
        </fileset>
      </unzip>
      -->
   </target>
   
   <target name="tests" depends="compile"
		description=" : tests du modele">
      <java classname="${TestClass}" fork="true">
		<classpath refid="projectClasspath"/>
        <assertions>
            <enable/>
        </assertions>
      </java>
   </target>
   
   <target name="doc" depends="compile"
		description=" : documentation des programmes sous le repertoire sources">
      <mkdir dir="${pathDocs}"/>
      <javadoc destdir="${pathDocs}" Author="true" Version="true" Private="true"
			encoding="${charset}" charset="${charset}" docencoding="${charset}">
         <fileset dir="${pathSrc}"/>
         <classpath refid="projectClasspath"/>
         <link href="${pathAPI}"/>
      </javadoc>
   </target>
	   
   <target name="run" depends="compile"
		description=" : execution de la classe principale de l application">
      <java classname="${MainClass}" fork="true">
		<classpath refid="projectClasspath"/>
        <assertions>
            <enable/>
        </assertions>
      <jvmarg value="-Djava.library.path=${pathNativeLwjgl}"/>
      </java>
   </target>
	
   <target name="build" depends="compile"
		description=" : construction d une application double-clickable">
      <jar destfile="${pathBuild}/${JarFile}" index="true">
            <manifest>
                <attribute name="Main-Class" value="${MainClass}" />
                <attribute name="Built-By"   value="${user.name}" />
            </manifest>
            <fileset dir="${pathClass}"/>
            <fileset dir="${basedir}">
                <include name="${dirRes}/"/>
                <include name="${dirLib}/"/>
            </fileset>
      </jar>
   </target>
   
   <target name="clean" depends="init"
	 description=" : nettoyage des .class et de la documentation">
		<delete dir="${dirClass}" />
		<delete dir="${dirDocs}" />
   </target>
   
   <target name="cleanall" depends="init"
	 description=" : nettoyage des .class, de la documentation et de l'application double-clickable.">
		<delete dir="${dirClass}" />
		<delete dir="${dirDocs}" />
        <delete dir="${dirBuild}" />
   </target>
</project>

How can I do for the simple double click the jar file can be run taking into account the ‘-Djava.library.path = $ {pathNativeLwjgl}’?

Using a single JAR file is a bad idea because double clicking on it rarely run the application because of WinRAR, Ark, etc… Do as you wish but applets and Java Web Start are more reliable. Maybe you can set the Java library path in the manifest.

I had just the idea of ​​creating a ‘jar’ which is one of my application launcher and launch just the ‘jar’ that needs libraries that seems a good idea right?