Creating Jar Files

Jar File not working?

  1. Make sure the jar file contains your assets. Right-click on it and choose 7-zip > open archive. Click and drag and missing assets into the jar file
  2. Try running it from command line to see any error messages. Follow the directions to run a jar file from command line: Open Windows Powershell to access the command line to create the jar file, "cd" into the directory containing the jar file, now run the jar file. The following command should launch your game:

    H:\>java -jar spaceinvaders.jar

    If there are exceptions, they will be printed in the console.

Create a working JAR File in Windows

  1. You must use Windows Powershell to access the command line to create the jar file. You will need to make sure that the JDK programs are in the class path. If your JDK is located at "C:\Program Files\Java\jdk1.6.0_03\bin", you can temporarily add it as follows:

    H:\>set path="C:\Program Files\Java\jdk1.6.0_03\bin"

  2. Create a text file called mainClass.txt that is located in the same directory as Game.class. It should contain the following line:
     Main-Class: Game
     
    Note: There must be a hard-return at the end of the file, otherwise it will not work.
  3. Put everything in a jar file, all the class files and the image files. To create a jar file that works with spaceinvaders do the following:

    H:\>jar cmf mainClass.txt spaceinvaders.jar *.class sprites/*.gif

  4. Now test that your jar file works. The following command should launch your game:

    H:\>java -jar spaceinvaders.jar

Accessing Text Files in a JAR

Accessing Images and Sound in a JAR

Click here for a working example of Space Invaders!