Wednesday, May 28, 2008

What does 'e' option do with 'jar' command in Java?

Question: What does the option 'e' do with 'jar' command in Java?

Answer: This option is used to set an entry point to a stand-alone application bundled as a JAR file. This option was introduced in Java 6.0 and it either creates or overrides the Main-Class attribute value set in the manifest file of the JAR (which is also used for the same purpose). We can use this option either while creating the JAR or while updating it and it saves the effort of either editing or creating the manifest file.

Example: Creating a JAR file and setting the entry point:-

Option 'c' is for create and 'f' indicates that the output of the command will go to a file instead of going to the standard output.

jar cfe JarFileName.jar PackageName.EntryPointClass PackageName/EntryPointClass.class

Running the stand-alone application using the entry point:

java -jar JarFileName.jar

The execution will start with the main() method of the class named EntryPointClass as we set the entry point of the stand-alone application as this class.

Similarly, we can set the entry point while updating a JAR file using the 'u' option for update and then we can simply run the JAR file as we do in the above case:-

jar ufe JarFileName.jar PackageName.EntryPointClass PackageName/EntryPointClass.classJava -jar JarFileName.jar


No comments: