Sunday 12 November 2017

Adding Classes to the JAR File's Classpath

You may need to reference classes in other JAR files from within a JAR file.

You specify classes to include in the Class-Path header field in the manifest file of an applet or application. The Class-Path header takes the following form:

Class-Path: jar1 jar2 directory-name/jar3

By using the Class-Path header in the manifest, you can avoid having to specify a long -classpath flag when invoking Java to run the your application.

Example :

create jar file using

jar cfm HelloWorld.jar modifyManifest.txt MyPackage/*.class

The modifyManifest.txt contains the following info

Main-Class: HelloWorld
Class-Path: jar1 jar2 directory-name/jar3

After creating jar, the MANIFEST.MF will have the added content

Manifest-Version: 1.0
Created-By: 1.7.0_80 (Oracle Corporation)
Main-Class: HelloWorld
Class-Path: jar1 jar2 directory-name/jar3

The classes in jar1,jar2, directory-name/jar3 are now loaded into the class path when you run HelloWorld.jar.

No comments:

Post a Comment