I’m not going to go into what a java archive (JAR) file is. You already know what it is and why you’d want to make one, or else you would never have found your way to this page. You just want to know how to make one executable, so that when people double-click on it, it executes just like a regular non-Java program. Okay: Here’s how:

  • Create and test your application. If it doesn’t work OUTSIDE of a jar file, it certainly won’t work INSIDE one.
  • Create a text file. Name it “Manifest.txt” and inside it place the following line:

    Main-Class: AppName.

    Naturally, you’d replace the word “AppName” with the main class of your application… the class you want to execute when somebody double-clicks on the JAR file. The “Main-Class” text needs to appear as it does in the line above. You’d place this text file in the directory with the class file(s).

  • Create the jar with the following on the command line (in the directory where your class files are):

    jar cfm appname.jar manifest.txt *.class

    “jar” is the call to the executable that makes the jar.
    “cfm” are the commands that you are passing to that jar-making executable. The “c” means create a new file. The “f” means that you will be including the name of the new file on the command line. The “m” means that you will also be including the name of a manifest file (this is what makes the jar executable). After the commands, you have the name of your JAR file that will be created and the name of the manifest file that you created earlier. Finally you have a list of files or directories that you are packaging in the archive. In this example, I am including everything that ends with “.class”. Note that the filenames that you are passing need to be in the order they are declared with the “cfm” statement. Basically, if you had put “cmf” instead of “cfm” then the total command line entry would be:

    jar cmf manifest.txt appname.jar *.class

    See how the manifest file and the jar filename switched places?

  • Test your jar file.
  • You’re done.

Note the use of the word “simple” in the title of this article. If you are doing something fancy with classpaths or JAR files inside other JAR files, then obviously you need a higher level of understanding, and the instructions aren’t nearly as straightforward as the ones presented here.

Popularity: 19% [?]

  • Java: Using ONE-JAR (2)
  • RTFM Doesn't Help (1)
  • One Response to “JAVA: Making a simple executable JAR”

    1. free java games says:

      nice blog.thanks for the info

    Leave a Reply