Packaging Jar with External Libraries in Eclipse

I have a class called Game in the package net.aGameName and am trying to export a runnable Jar using eclipse. In the project I have two external Jar files added, lwjgl.jar and lwjgl_util.jar.

When I go to File->Export->Runnable Jar File and choose the Launch configuration I use for testing (which works successfully) I am able to export the Jar file without any errors. Finally I open cmd and use java aGameName.jar and receive the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: aGameName/jar
Caused by: java.lang.ClassNotFoundException: aGameName.jar
at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
Could not find the main class: aGameName.jar.  Program will exit.


When I open the manifest file it says:

Manifest-Version: 1.0
Class-Path: .
Main-Class: net.aGameName.Game


Can anyone easily spot the error that would be causing this? Thanks

• As you add more dependencies, you may find Eclipse's export functions don't cut the mustard; eventually you might want to look into Ant or Maven. – Tom G Oct 25 '11 at 14:59

Try java -jar aGameName.jar.

java aGameName.jar will try to run a class file named aGameName/jar.class.

• Unfortunately that still results in a similar error message except the main class that it is looking for is now net.aGameName.Game – tgrosinger Oct 25 '11 at 14:59
• What are you using for your Library Handling during export? – skyuzo Oct 25 '11 at 19:35

I actually found a small little program that would allow me to join multiple jar files together. I used this along with all the dependencies I needed and the jar that Eclipse generated to create a runable jar. Simple enough and for this small project it works fine.

http://ninjacave.com/jarsplice

To run the jar you have to specify java -jar JARFILENAME as @skyuzo indicated.

Below is an ant xml file that should generate the correct runnable jar using eclipse's jar in jar loader. Feel free to add any libraries to the Rsrc-Class_Path (space seperated), also make sure your jars are in a folder called lib (or edit that part in the xml) and make sure jar-in-jar-loader.zip is next to your build.xml (or change it to an absolute location of that file, I think it is somewhere in the eclipse installation folder)

Note: this might require minor changes to suit your project.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="jar" name="Create Runnable Jar with Jar-in-Jar Loader">
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist" location="dist"/>
<property name="lib" location="lib"/>

<path id="master-classpath">
<fileset dir="${lib}" includes="*.jar"/> </path> <target name="clean"> <delete dir="${build}"/>
<delete dir="${dist}"/> </target> <target name="compile" depends="clean" description="Compiles the project's source code"> <mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}" debug="on" source="1.5" target="1.5" includeantruntime="false">
<classpath refid="master-classpath"/>
</javac>
</target>

<target name="jar" depends="compile">
<mkdir dir="${dist}"/> <jar destfile="${dist}/aGameName.jar">
<manifest>
<fileset dir="${build}"/> <zipfileset dir="${lib}" includes="*.jar"/>