5 things you don't know about jars: Java archive is more than just a bunch of classes

Source: Internet
Author: User

For most Java developers, the JAR file and its "close relatives" WAR and EAR are just the end result of a lengthy Ant or Maven process. The standard step is to copy a JAR to the appropriate location in the server (or, in a few cases, the user machine) and then forget about it.

In fact, the jar can do more than just store source code, you should know what the jar can do and how to do it. In this series of 5 events, you will be shown how to maximize the use of Java Archive files (sometimes WAR and EAR), especially at deployment time.

With many Java developers using spring (because the spring framework poses some unique challenges to traditional jar use), here are a few specific tips for handling jars in a spring application.

About this series

Do you think you know Java programming? The fact is, most developers are only learning about the Java platform's fur, and they are only enough to cope with the work. In this series, Ted Neward deep into the core functions of the Java platform, revealing some little-known facts to help you solve the toughest programming difficulties.

I'll start with a simple example of a standard Java Archive file generation process, which will be the basis for the following techniques.

Put it in the JAR.

Typically, after the source code is compiled, you need to build a jar file, use the Jar command-line utility, or, more commonly, an Ant jar task that collects Java code (already wrapped in a packet) into a separate collection, and the process is straightforward, and I don't want to do too much explaining here, You will continue to explain how to build the JAR later. Now, I just need to archive Hello, a standalone console utility that is useful for performing print messages to the console. As shown in Listing 1:

Listing 1. Archive Console Utility

package com.tedneward.jars;

public class Hello 
{
   public static void main(String[] args)
   {
     System.out.println("Howdy!");
   }
}

The Hello utility does not have much content, but it is a useful "scaffolding" for studying JAR files, starting with executing this code.

1. The JAR is executable

Languages such as. NET and C + + are always OS-friendly, and you can start an application simply by referencing its name on the command line (HelloWorld.exe), or by double-clicking its icon in the GUI shell. In Java programming, however, the initiator program-java-the JVM into the process, and we need to pass a command-line argument (Com.tedneward.Hello) specifying the class of the main () method that you want to start.

These additional steps make it more difficult to create an interface-friendly application using Java. Not only does the end-user need to enter all the parameters at the command line (the end user prefers to avoid it), and it is highly likely that he or she will err and return an incomprehensible error.

This solution makes the jar file executable so that the Java launcher automatically recognizes which class will start when it executes the jar file. All we have to do is introduce a portal into the jar file manifest file (MANIFEST.MF in the Meta-inf subdirectory of the jar), like this:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.