July 26, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Java ARchive (JAR) Files 101

  • September 30, 2009
  • By Sridhar M S
  • Send Email »
  • More Articles »

JAR File in App Deployment

Now that you have a JAR file, you may want to use it as part of your application deployment process. Typically, you will form many JARs for different application components (client and server, for instance) and use them in an application.

The class MyApplication.java, which is bundled in the JAR file, is only an example. For actual deployments, you would bundle only the .class files instead of the .java files. Assuming that you have now bundled the MyApplication.class file, you can execute this class file. The java command has an option for this.

The command java –jar –MyApplication.jar would result in the following output:

Failed to load Main-Class manifest attribute from
MyApplication.jar

For this command to work as expected, you need to do one important thing. The MANIFEST.MF in the MyApplication.jar file has to be updated with the following line:
Main-Class: com.mycomp.demo.MyApplication

Now the modified MANIFEST.MF file will have the following contents:

Manifest-Version: 1.0
Created-By: 1.5.0_19 (Sun Microsystems Inc.)
Main-Class: com.mycomp.demo.MyApplication

Also, in the Java ARchive_src.zip code download, the MyApplication.java file is updated as follows with the string main method of MyApplication:

package com.mycomp.demo;
 
public class MyApplication
{
    public static void main(String args[])
    {
        System.out.println("main method of MyApplication.");
    }
}

The addition of the main class is also referred to as defining the entry point for the JAR file.

To update your MyApplication.jar file, repackage the class file and the updated MANIFEST.MF file. Now, execute the command java –jar –MyApplication.jar to get the desired result.


Tags: Java SE, JAR



Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel