Creating a Stand-Alone Java Application

  1. Write a main method that runs your program. You can write this method anywhere. In this example, I'll write my main method in a class called Main, that has no other methods. For example:
    public class Main
    {
      public static void main(String[] args)
      {
        Game.play();
      }  
    }
    
    The main method must be public static void, and it must take in an array of strings (and no other parameters).
  2. Make sure your code is compiled, and that you have tested it thoroughly.
  3. If you're using Windows, you will need to set your path to include Java, if you haven't done so already. This is a delicate operation. Open Explorer, and look inside C:\ProgramFiles\Java, and you should see some version of the JDK. Open this folder, and then open the bin folder. Select the complete path from the top of the Explorer window, and press Ctrl-C to copy it.

    Next, find the "My Computer" icon (on your Start menu or desktop), right-click it, and select properties. Click on the Advanced tab, and then click on the Environment variables button. Look at the variables listed for all users, and click on the Path variable. Do not delete the contents of this variable! Instead, edit the contents by moving the cursor to the right end, entering a semicolon (;), and pressing Ctrl-V to paste the path you copied earlier. Then go ahead and save your changes. (If you have any Cmd windows open, you will need to close them.)

  4. If you're using Windows, go to the Start menu and type "cmd" to run a program that brings up a command prompt window. If you're using a Mac or Linux machine, run the Terminal program to bring up a command prompt.
  5. In Windows, type dir at the command prompt to list the contents of the current directory. On a Mac or Linux machine, type ls to do this.
  6. Now we want to change to the directory/folder that contains your compiled code. Look at the listing of sub-directories within this directory, and identify which one contains your code. Type cd followed by the name of that directory, to change to that directory. For example, to change to a directory called Desktop, you would type:

    cd Desktop

    To change to the parent directory, type:

    cd ..

    Every time you change to a new directory, list the contents of that directory to see where to go next. Continue listing and changing directories until you reach the directory that contains your .class files.

  7. If you compiled your program using Java 1.6, but plan to run it on a Mac, you'll need to recompile your code from the command line, by typing:

    javac -target 1.5 *.java

  8. Now we'll create a single JAR file containing all of the files needed to run your program.

    If you have a relatively new version of Java, you can create a JAR by entering something like this:

    jar cfe game.jar Main *.java *.class *.gif

    This particular example will create a JAR file called game.jar, whose main method is found in a class called Main. The JAR file created will contain all of the .java files in this directory, plus all of the .class files and all of the .gif files.

    If your version of Java doesn't support building a JAR file with the 'e' flag used above, then here's what you'll need to do instead. Open Notepad (on Windows) or TextEdit (on a Mac, making sure to select the "make plain text" option). In a new text file, type:

    Main-Class: Main

    You must spell/capitalize/punctuate your text exactly like this, with exactly one space after the colon. The name on the right should be the name of the class with your main method. There must be exactly one carriage return after the name of your class file, so that if you arrow down to the end of the file, your cursor should land at the beginning of the second line (which should be blank). Save this file as manifest.txt in the directory with your compiled code. Make sure it appears in your directory listing. Then enter something like:

    jar cfm game.jar manifest.txt *.java *.class *.gif

    This particular example will create a JAR file called game.jar, which will contain all of the .java files in this directory, plus all of the .class files and all of the .gif files.

    Regarding which files to store in your JAR file, the .class files are the important ones. And if your program uses any image files, etc., you'll need to include these, too. (The .java files aren't necessary to run your program, but it's a good idea to bundle them up with your JAR, in case you ever want to recover your code, or if you want to allow other people to read and modify your code.)

  9. Test your JAR file by running something like:

    java -jar game.jar

    With any luck, your program will run successfully. Next, try moving the JAR file you created to a completely different directory, and double-click to run it. If it runs here, then your JAR file is a success!