zoukankan      html  css  js  c++  java
  • 创建可执行的jar包

    If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

    Main-Class: classname
    

    The value classname is the name of the class that is your application's entry point.

    Recall that the entry point is a class having a method with signature public static void main(String[] args).

    After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

    java -jar JAR-name
    

    The main method of the class specified in the Main-Class header is executed.

    An Example

    We want to execute the main method in the class MyClass in the package MyPackage when we run the JAR file.

    We first create a text file named Manifest.txt with the following contents:

    Main-Class: MyPackage.MyClass
    

    Warning: 

    The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.


    We then create a JAR file named MyJar.jar by entering the following command:

    jar cfm MyJar.jar Manifest.txt MyPackage/*.class
    

    This creates the JAR file with a manifest with the following contents:

    Manifest-Version: 1.0
    Created-By: 1.6.0 (Sun Microsystems Inc.)
    Main-Class: MyPackage.MyClass
    

    When you run the JAR file with the following command, the main method of MyClass executes:

    java -jar MyJar.jar
    

    Setting an Entry Point with the JAR Tool

    The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file.
    For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:

    jar cfe app.jar MyApp MyApp.class
    

    You can directly invoke this application by running the following command:

    java -jar app.jar
    

    If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

    jar cfe Main.jar foo.Main foo/Main.class

    Adding Classes to the JAR File's Classpath

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

    For example, in a typical situation an applet is bundled in a JAR file whose manifest references a different JAR file (or several different JAR files) that serves as utilities for the purposes of that applet.

    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-name jar2-name directory-name/jar3-name
    

    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.


    Note: 

    The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.


    An Example

    We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.

    We first create a text file named Manifest.txt with the following contents:

    Class-Path: MyUtils.jar
    

    Warning: 

    The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.


    We then create a JAR file named MyJar.jar by entering the following command:

    jar cfm MyJar.jar Manifest.txt MyPackage/*.class
    

    This creates the JAR file with a manifest with the following contents:

    Manifest-Version: 1.0
    Class-Path: MyUtils.jar
    Created-By: 1.6.0 (Sun Microsystems Inc.)
    

    The classes in MyUtils.jar are now loaded into the class path when you run MyJar.jar.

  • 相关阅读:
    python socks5 代理服务
    F# 语言参考【摘自MSDN】
    vs2003版本的程序转换为vs2008版本时出现的一些问题及解决办法
    【转】SQL to Mongo Mapping Chart
    jQuery UI Download
    jQuery UI Multiselect
    jqGrid search oper
    apache下用expires_module让浏览器缓存静态文件
    【转】谈谈浏览器的缓存过期时间
    USACO 1.1 Friday the Thirteenth
  • 原文地址:https://www.cnblogs.com/az19870227/p/2195540.html
Copyright © 2011-2022 走看看