jar command practice

jar -h

非法选项: h
用法: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
选项:
    -c  创建新档案
    -t  列出档案目录
    -x  从档案中提取指定的 (或所有) 文件
    -u  更新现有档案
    -v  在标准输出中生成详细输出
    -f  指定档案文件名
    -m  包含指定清单文件中的清单信息
    -n  创建新档案后执行 Pack200 规范化
    -e  为捆绑到可执行 jar 文件的独立应用程序
        指定应用程序入口点
    -0  仅存储; 不使用任何 ZIP 压缩
    -P  保留文件名中的前导 '/' (绝对路径) 和 ".." (父目录) 组件
    -M  不创建条目的清单文件
    -i  为指定的 jar 文件生成索引信息
    -C  更改为指定的目录并包含以下文件
如果任何文件为目录, 则对其进行递归处理。
清单文件名, 档案文件名和入口点名称的指定顺序
与 'm', 'f' 和 'e' 标记的指定顺序相同。

示例 1: 将两个类文件归档到一个名为 classes.jar 的档案中:
       jar cvf classes.jar Foo.class Bar.class
示例 2: 使用现有的清单文件 'mymanifest' 并
           将 foo/ 目录中的所有文件归档到 'classes.jar' 中:
       jar cvfm classes.jar mymanifest -C foo/ .

1. Points to note about instructions

1. Do not add a dash- in the {ctxui}[vfmn0PMe] behind the jar, only -C can use a dash-, otherwise an error will be reported;

2. The {ctxui}[vfmn0PMe] behind the jar cannot be written separately, otherwise an error will be reported;

3. One of the four parameters {ctxui} must be selected, [vfmn0PMe] is an optional parameter, and the file name is also required;

4. The specified manifest file must be followed by m, and M means not to create a manifest file;

5. The order of manifest file name, archive file name and entry point name must be the same as the specified order of 'm', 'f' and 'e' tags;

6. -C is followed by a directory, which means that only the files in the directory are packaged. -C directory/ .midpoint . means any file in the directory . If you want to pack certain files in the directory, you need to list the files one by one. The full path name of the ;

7. When -C is not used, the files to be packaged must be listed after the jar command;

8. files ... represents a file or multiple files;

9. e specifies the entry point of the executable jar package, which must be a full path name, such as com.crland.Welcome;

2. Practice

1. Project catalog

package com.crland;

public class Welcome {
    public static void main(String[] args) {
        Teacher.greeting();
    }
}
package com.crland;

public class Teacher {

    public static void greeting() {
        System.out.println("welcome xiaoliu");
    }
}

        

2. Edit the source file

         To compile source files, you need to use the javac command, first enter the project directory, and then use the javac command:

E:\codes\idea\HelloCode\testjar> javac -d out -sourcepath src .\src\main\java\com\crland\Teacher.java .\src\main\java\com\crland\Welcome.java

        After the command is executed, the class class file will be generated in the out directory

        It can be seen that the generated class files are also organized according to the original package directory. 

        Let's briefly look at the usage of the javac command:

用法: javac <options> <source files>
其中, 可能的选项包括:
  -g                         生成所有调试信息
  -g:none                    不生成任何调试信息
  -g:{lines,vars,source}     只生成某些调试信息
  -nowarn                    不生成任何警告
  -verbose                   输出有关编译器正在执行的操作的消息
  -deprecation               输出使用已过时的 API 的源位置
  -classpath <路径>            指定查找用户类文件和注释处理程序的位置
  -cp <路径>                   指定查找用户类文件和注释处理程序的位置
  -sourcepath <路径>           指定查找输入源文件的位置
  -bootclasspath <路径>        覆盖引导类文件的位置
  -extdirs <目录>              覆盖所安装扩展的位置
  -endorseddirs <目录>         覆盖签名的标准路径的位置
  -proc:{none,only}          控制是否执行注释处理和/或编译。
  -processor <class1>[,<class2>,<class3>...] 要运行的注释处理程序的名称; 绕过默认的搜索进程
  -processorpath <路径>        指定查找注释处理程序的位置
  -parameters                生成元数据以用于方法参数的反射
  -d <目录>                    指定放置生成的类文件的位置
  -s <目录>                    指定放置生成的源文件的位置
  -h <目录>                    指定放置生成的本机标头文件的位置
  -implicit:{none,class}     指定是否为隐式引用文件生成类文件
  -encoding <编码>             指定源文件使用的字符编码
  -source <发行版>              提供与指定发行版的源兼容性
  -target <发行版>              生成特定 VM 版本的类文件
  -profile <配置文件>            请确保使用的 API 在指定的配置文件中可用
  -version                   版本信息
  -help                      输出标准选项的提要
  -A关键字[=值]                  传递给注释处理程序的选项
  -X                         输出非标准选项的提要
  -J<标记>                     直接将 <标记> 传递给运行时系统
  -Werror                    出现警告时终止编译
  @<文件名>                     从文件读取选项和文件名

3. Pack bytecode files

1. Package all bytecode files and provide entry points, and package them into executable jar packages

E:\codes\idea\HelloCode\testjar> jar vcfe test1.jar com.crland.Welcome -C out/ . 
已添加清单
正在添加: com/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: com/crland/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: com/crland/Teacher.class(输入 = 413) (输出 = 293)(压缩了 29%)
正在添加: com/crland/Welcome.class(输入 = 322) (输出 = 237)(压缩了 26%)

        The packaging results are as follows:

         Copy test1.jar to another directory, and then use the java command to run the jar package, executable:

C:\Users\liuqinhou\Desktop\test> java -jar .\test1.jar
welcome xiaoliu

        It can be seen that the jar package is executed successfully.

important point:

1. The order of fe parameters needs to be consistent with the order of test1.jar com.crland.Welcome.

2. The last point. means to pack all the files in the out directory

2. If you only want to package the specified files in the directory, you need to specify the file path at the end of the command

 E:\codes\idea\HelloCode\testjar> jar vcfe test2.jar com.crland.Welcome -C out/ com/crland/Welcome.class

        The above command is to only package the com/crland/Welcom.class file in the out/ directory, and the packaging result is:

 

Guess you like

Origin blog.csdn.net/liuqinhou/article/details/131860780