The main entry class is a class with the main() method to start a stand-alone application, version information about packages, etc. The use of Class-Path does not affect only JARs that are "executable" (have a Main-Class header specified in their manifest file and run with java -jar ...), but can influence the loaded classes for compilation and for any Java application execution in which the JAR with the Class-Path header-containing manifest file lies on the classpath. A colleague recently ran into an issue that surprised me because it proved that a JAR file's Manifest's Class-Path entry also influences the compile-time classpath when the containing JAR is included on the classpath while running javac. The order in which you specify the new JAR file name and the manifest file name must match the order of options m and f. For example, you can change the above command by specifying the f and m options in a different order as follows: This command will add a manifest file with the following contents to the test.jar file: If you do not specify the Manifest-Version and Created-By attribute in your manifest file, the tool adds them. A manifest file must end with a new line. referenced from this attribute must be found relatively to the jar file itself, usually in the same This is an intriguing line of discussion. The following command will create a test.jar file without adding a default manifest file: The jar command gives you an option to customize the contents of the manifest file. This task is often used to work around command line limitations on Windows when using very long All rights reserved. the filename for the Jar which will contain the manifest that will use the property this A directory name must end with a forward slash. For some reason the Class-Path entry in the manifest does not show up when inspecting the classpath (e.g. When you run the test.jar file using the following java command, this CLASSPATH will be used to search and load classes. This classpath must not be empty, and is required. and its main entry class. The code listings are shown next for these. When deploying a shadowed JAR as an execution JAR, it is important to note that any non-bundled runtime dependencies must be deployed in the location specified in the Class-Path entry in the manifest. Each section contains name-value pairs. In this case the entries are put in the JAR manifest and cannot be changed after the capsule has been built. I disagree. By default, all packages in a JAR file are not sealed. I formerly would have expected compilation to fail when javac would be unable to find PersonIF.jar in a separate subdirectory. I will now attempt to compile the Main.class from Main.java with only the current directory on the classpath. The following features are available when specifying a Classpath entry: Add project dependency classpath. The final demonstration for this post involves removing the Class-Path header and associated value from the JAR file and trying to compile with javac and the same command-line-specified classpath. The Person.class file will exist in its own Person.jar JAR file and that JAR file includes a MANIFEST.MF file with a Class-Path header referencing PersonIF.jar in the relative subdirectory. JVM automatically uses all file entries listed in the Class-Path attributes The output of javac -verbose provides the "search path for source files" and the "search path for class files". JAR Manifest Class-Path is Not for Java Application Launcher Only, Adding Classes to the JAR File's Classpath, aware of what's in a particular JAR's manifest file, Developer It is a space-separated list of jar files, zip files, and directories. element, where the defined property is re-referenced. Use the option e with the jar tool when you create/update a jar file. I'm using Tomcat 5.5 and I wanted to add additional classpath entry to my webapp. The next screen snapshot demonstrates that compilation with javac fails now because, as expected, PersonIF.class is not explicitly specified on the classpath and is no longer made available by reference from the MANIFEST.MFClass-Path header of a JAR that is on the classpath. The default manifest file contains only two attributes: Manifest-Version and Created-By. later be created with
with a nested element You can use the option M to tell the jar tool to omit the default manifest file. Entries in the individual section apply to a particular entry. The following command will add com.java2s.Main as the value of the Main-Class in the MANIFEST.MF file in the test.jar file: The following command will add com.java2s.Main as the value of the Main-Class in the MANIFEST.MF file in an existing test.jar file by using the option u for update: You can set the CLASSPATH for a JAR file in its manifest file. The following is a sample manifest file: The above manifest file has one section with four attributes: There are two kinds of sections in a manifest file: the main section and the individual section. find any corresponding file. It turns out that the Class-Path entry in a JAR's manifest affects the Java compiler (javac) just as it impacts the Java application launcher (java). Select this and click "OK" (the special dependency attribute will be added to the classpath entry and the value for the attribute will be the runtime path for the resolved JARs, "/WEB-INF/lib"). The manifest file is normally used to define following tasks : Define the entry point of the Application, make the Jar executable. I will intentionally place the PersonIF.class file in its own JAR called PersonIF.jar and will store that JAR in a (different) subdirectory. To seal a package in a JAR file, include two attributes: Name and Sealed. For this task to work properly the relative path from the file given in the jarfile The attribute name is called Class-Path, which you must specify in a custom manifest file. the name of the property to set. This same section also states, "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." This quick tutorial provides different ways of defining an entry point into a Spring Boot application via Maven and Gradle. Under the servlet spec 2.4, it states ... "The application developer depending on such an extension or extensions must provide a META-INF/MANIFEST.MF entry in the WAR file listing all extensions needed by the WAR. Note that the property value created may be longer than a manifest's maximum 72 characters per The FAQ entry on manifests goes on to say: "The Class-Path attribute is entirely file-centric, whereas Java Web Start and JNLP is web-centric, i.e., based on URLs. Converts a Path into a property whose value is appropriate for A colon separates a name and its corresponding value. When you run the command mvn package to package project into a Jar, the following meta-inf/manifest.mf file will be generated and added into the final Jar file automatically. The manifest file above contains three sections: one main section and two individual sections. Background: Java archive include JAR files, which can be EJBJAR files, Application Client JAR files, Web Fragment JAR files, and simple utility JAR files. # Configuring the JAR Manifest Beyond the automatic configuration of the Class-Path entry, the shadowJar manifest is configured in a number of ways. jar Application. Suppose you have a file named manifest.txt with one attribute entry in it. I believe the name of main class can be found in the MANIFEST.MF file inside the jar It should still get searched for classes though. Put differently, the maximum number of. A manifest file is divided into sections separated by a blank line. These two sentences essentially summarize how I've always thought of the Class-Path header in a manifest file: as the classpath for the containing JAR being executed via the Java application launcher (java executable). The answer can be seen by running javac with the -verbose flag. See the original article here. The format of the manifest entry should follow standard JAR manifest format. The long class path normally specified on the command line is replaced by a single (possibly empty) jar file which an in-manifest Class-Path attribute whose value lists all the jar and zip files the classpath should contain. The Created-By is defaulted to the JDK version you use. to be when deploying the jar. here and here; those examples use the property "java.class.path" but my testing shows that ClassLoader.getURLs() behaves the same). Make sure to add a new line at the end of the file. relatively to the build/ directory that will contain acme.jar, which can Class. The next screen snapshot demonstrates running the newly compiled Main.class class and having the dependency PersonIF.class picked up from archive/PersonIF.jar without it being specified in the value passed to the Java application launcher's java -cp flag. The section "Adding Classes to the JAR File's Classpath" of the Deployment Trail of The Java Tutorials states, "You specify classes to include in the Class-Path header field in the manifest file of an applet or application." Dynamically with the capsule.classpath system property on the command-line used to run the capsule. The Person.jar manifest file in this example did not specify a Main-Class header and only specified a Class-Path header, but was still able to use this classpath content at runtime when invoked with java. The following entries in a manifest file will seal a package named com.java2s. This file need not exist yet, but its parent directory must exist. I've known almost since I started learning about Java that the Class-Path header field in a Manifest file specifies the relative runtime classpath for executable JARs (JARs with application starting point specified by another manifest header called Main-Class). The manifest file contains information about the JAR file and its entries. Opinions expressed by DZone contributors are their own. A manifest file can contain information about the CLASSPATH setting for the JAR file, and its main entry class. What you are doing sounds correct. The long class path normally specified on the command As can be seen from the code listings above, class Main depends upon (uses) class Person and class Person depends upon (implements) PersonIF. This property must not already be set. line, but will be properly wrapped as per the Jar specification by the There is a blank line between the two sections. There are many places from which a Java classloader may load classes for building and running Java applications. Export or … The second individual section is for an entry called images/logo.bmp. The "search path for class files" was the significant one in this case because I had moved the PersonIF.java and Person.java source files to a completely unrelated directory not in those specified search paths. If you want to seal the JAR file itself, you can include a Sealed This will allow you to put both your new-config and the application.jar on the classpath in the required order: e.g. We can also add the Main-Class attribute value in the manifest file without creating our own manifest file. Another use of the Class-Path attribute is to generate an index of all packages using the option i of the jar tool. Note though that it silently ignores entries for which it cannot However, it doesn't fail! Suppose this Class-Path setting is included in the manifest file for the test.jar file. It defaults the Manifest-Version to 1.0. The following command will generate an index for all packages in all JAR files listed in the Class-Path attribute of the manifest file in the test.jar file: Sealing a package in a JAR file means that all classes declared in that package must be archived in the same JAR file. It states that the content type of the entry is an image of bmp type. Marketing Blog, The importance of the JAR's manifest file is sometimes understated, but this topic is a reminder of the usefulness of being, This topic is a reminder of the insight that can be gleaned from running, Whenever you place a JAR on the classpath of the. Published at DZone with permission of Dustin Marx, DZone MVB. The jar command can create a default manifest file and add it to the JAR file. The following code shows how to run a Java program by using the java command and specifying the class name that has the main() method as follows: We can run a jar file using the -jar option with the java command as follows: When you run the above command, the JVM will look for the value of the Main-Class attribute in the MANIFEST.MF file in the test.jar file and attempt to run that class. class paths when launching an application. a path defined elsewhere using the syntax. The package name must end with a forward slash (/). In this case, the JAR containing Person.class is called Person2.jar and the following screen snapshot demonstrates that its MANIFEST.MF file does not have a Class-Path header. I expected the runtime behavior to be this way, though admittedly I had never tried it or even thought about doing it with a JAR whose MANIFEST.MF file did not have a Main-Class header (non-executable JAR). An individual entry starts with a "Name" attribute, whose value is the name of the entry in the JAR file and it is followed by other attributes for that entry. A blank line must separate any two sections. (assuming "new-config" is a directory containing the overridden properties file) java -cp new-config: application. The following entries in a manifest file will seal all packages in the JAR file, except the book/tutorial/ package: java2s.com | © Demo Source and Support. Join the DZone community and get the full member experience. line is replaced by a single (possibly empty) jar file which an in-manifest Class-Path of a jar to locate/load classes. It's interesting to see that the search path for class files (as well as the search path for source files) includes archive/PersonIF.jar even though I did not specify this JAR (or even its directory) in the value of -cp. A manifest file can contain information about the CLASSPATH setting for the JAR file, The first individual section indicates that the package book/data is not sealed. A new line separates each name-value pair. To demonstrate this, I'm going to use a simple interface (PersonIF), a simple class (Person) that implements that interface, and a simple class Main that uses the class that implements the interface. Entries in the main section apply to the entire JAR file. file to the path entry. attribute whose value lists all the jar and zip files the classpath should contain. The maximum number of parent directories one is allowed to traverse to navigate from the jar The file's contents are as follows: To add the Main-Class attribute value from manifest.txt file in a new test.jar file by including all class files in the current working directory, you execute the following command: When you specify the option m, you must also specify the manifest file name. task will set. A JAR file may optionally contain a manifest file named MANIFEST.MF in the META-INF directory. The maven-jar-plugin generates the "Class-Path" entry for the jar manifest using the maven dependencies, and there doesn't appear to be any way of adding arbitrary entries. As this post has demonstrated, the Class-Path header of a JAR's MANIFEST.MF file is another touch point for influencing which classes the classloader will load both at runtime and at compile time. attributed, as shown: Sealing the JAR file will seal all packages in that JAR file. The above entry has three items for the CLASSPATH: a JAR file Main.jar, a directory using the file protocol file:/c:/book/, and another JAR file using a HTTP protocol http://www.java2s.com/tutorial.jar. Without that JAR available, javac is unable to find PersonIF.class and reports the error message: "class file for PersonIF not found.". Thus, the two models do not merge easily." A selection will be visible for the classpath container entry just added (will be prefixed by "(Classpath) ")). When you run a JAR file with the -jar option using the java command, any CLASSPATH setting outside the manifest file of the JAR file (test.jar file in the above case) is ignored. This demonstrates that the Oracle-provided Java compiler considers the classpath content specified in the Class-Path header of the MANIFEST.MF of any JAR on specified on the classpath. The jar command will read the name-value pairs from the specified manifest file and add them to the MANIFEST.MF file. Over a million developers have joined DZone. The files referenced from this attribute must be found relatively to the jar file itself, usually in the same directory. Assuming a path with id=classpath was already defined, convert this path An attribute in an individual section overrides the same attribute in the main section. An issue recently proved that a JAR file's Manifest's Class-Path entry also influences the compile-time classpath when the containing JAR is included on the classpath while running javac. The files a Manifest's Class-Path attribute. attribute to the elements of the nested classpath must be the same as you expect them that lists an . However, you can override it by not sealing a package individually. directory. The Class-Path attribute in a manifest file looks like. The main entry class is a class with the main() method to start a stand-alone application, version information about packages, etc. Syntax of Classpath entries. This individual section attribute of "Sealed: false" will override the main section's attribute of "Sealed: true". This seemed surprising to me. Main. The value for the Name attribute is the name of the package and the Sealed attribute has value as true. Why did this compile when I had not explicitly specified PersonIF.class (or a JAR containing it) as the value of classpath provided via the -cp flag? Second, as above, with a manifest class path entry made obsolete by adjustments made to the packaging structure of an application. A path-like element, which can be defined in-place, or refer to This post demonstrates this new-to-me nuance. The option m specifies our file that has the contents for the manifest file. We see from the previous screen snapshot that the search paths for source files and for class files no longer include archive/PersonIF.jar. If you have not included a Main-Class attribute in the test.jar file, the above command will generate an error.