Sky Archive

Java

[Java] window에서 자바 아카이브(JAR)로 압축하기

햇살처럼 2021. 11. 24. 09:54

JAR(Java Archive)란?

이름에서 알 수 있듯 Archive파일로 여러 개의 자바 클래스와 리소스 등의 집합체이다. 

JAR 파일은 실제로 ZIP 파일 포맷으로 이루어진 압축 파일로서, 파일 확장자는 .jar이다. 
사용자들은 JDK에 포함된 jar 명령어를 이용하여 JAR 파일을 만들거나 압축을 풀 수 있다.

실행 가능한 JAR 파일은 OS에 따라 직접 실행 시킬 수 있고 커맨드 라인을 통해 실행시킬 수도 있다. 
실행시키기 위해서는 java -jar foo.jar라고 입력하여 실행할 수 있다.


 

JDK가 설치 되어 있지 않다면?

C:\Users\administer\Desktop\mng\e.g-Archive>javac -version
'jarvac'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는
배치 파일이 아닙니다.


JDK의 설치가 완료 되었다면 jar의 정보를 확인할 수 있다. 

C:\Users\administer\Desktop\mng\e.g-Archive>jar --help
Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files] ...
jar creates an archive for classes and resources, and can manipulate or
restore individual classes or resources from an archive.

 Examples:
 # Create an archive called classes.jar with two class files:
 jar --create --file classes.jar Foo.class Bar.class
 # Create an archive using an existing manifest, with all the files in foo/:
 jar --create --file classes.jar --manifest mymanifest -C foo/ .
 # Create a modular jar archive, where the module descriptor is located in
 # classes/module-info.class:
 jar --create --file foo.jar --main-class com.foo.Main --module-version 1.0
     -C foo/ classes resources
 # Update an existing non-modular jar to a modular jar:
 jar --update --file foo.jar --main-class com.foo.Main --module-version 1.0
     -C foo/ module-info.class
 # Create a multi-release jar, placing some files in the META-INF/versions/9 directory:
 jar --create --file mr.jar -C foo classes --release 9 -C foo9 classes

To shorten or simplify the jar command, you can specify arguments in a separate
text file and pass it to the jar command with the at sign (@) as a prefix.

 Examples:
 # Read additional options and list of class files from the file classes.list
 jar --create --file my.jar @classes.list


 Main operation mode:

  -c, --create               Create the archive
  -i, --generate-index=FILE  Generate index information for the specified jar
                             archives
  -t, --list                 List the table of contents for the archive
  -u, --update               Update an existing jar archive
  -x, --extract              Extract named (or all) files from the archive
  -d, --describe-module      Print the module descriptor, or automatic module name

 Operation modifiers valid in any mode:

  -C DIR                     Change to the specified directory and include the
                             following file
  -f, --file=FILE            The archive file name. When omitted, either stdin or
                             stdout is used based on the operation
      --release VERSION      Places all following files in a versioned directory
                             of the jar (i.e. META-INF/versions/VERSION/)
  -v, --verbose              Generate verbose output on standard output

 Operation modifiers valid only in create and update mode:

  -e, --main-class=CLASSNAME The application entry point for stand-alone
                             applications bundled into a modular, or executable,
                             jar archive
  -m, --manifest=FILE        Include the manifest information from the given
                             manifest file
  -M, --no-manifest          Do not create a manifest file for the entries
      --module-version=VERSION    The module version, when creating a modular
                             jar, or updating a non-modular jar
      --hash-modules=PATTERN Compute and record the hashes of modules
                             matched by the given pattern and that depend upon
                             directly or indirectly on a modular jar being
                             created or a non-modular jar being updated
  -p, --module-path          Location of module dependence for generating
                             the hash

 Operation modifiers valid only in create, update, and generate-index mode:

  -0, --no-compress          Store only; use no ZIP compression

 Other options:

  -?, -h, --help[:compat]    Give this, or optionally the compatibility, help
      --help-extra           Give help on extra options
      --version              Print program version

 An archive is a modular jar if a module descriptor, 'module-info.class', is
 located in the root of the given directories, or the root of the jar archive
 itself. The following operations are only valid when creating a modular jar,
 or updating an existing non-modular jar: '--module-version',
 '--hash-modules', and '--module-path'.

 Mandatory or optional arguments to long options are also mandatory or optional
 for any corresponding short options.

JAR 파일을 만들거나 압축을 해제하는데는, 자바 개발 키트에 포함된 jar 명령을 이용할 수 있다.

 

※ ZIP 파일을 해제하는 표준 소프트웨어의 경우 JAR를 지원하지 않는 경우가 많기 때문에 JAR파일을 만들기에는 적합하지 않다. 


 

- jar파일 만들기

표준 출력에서 상세 출력으로 진행 (상세정보 옵션 : -v)

 

[jar -cf foo.jar]

  -c, --create               Create the archive.

  -v, --verbose             Generate verbose output on standard output.
  -f, --file=FILE            The archive file name. When omitted, either stdin or stdout is used based on the operation.

C:\Users\administer\Desktop\mng\e.g-Archive>jar cvf e.g-Archive.jar ./
added manifest
adding: config/(in = 0) (out= 0)(stored 0%)
adding: config/context-dbPool.xml(in = 1273) (out= 363)(deflated 71%)
adding: config/ehcache.xml(in = 2529) (out= 896)(deflated 64%)
adding: config/jdbcDriverConf.xml(in = 541) (out= 259)(deflated 52%)
adding: io/project/api/web/(in = 0) (out= 0)(stored 0%)
adding: io/project/api/web/DemoController.class(in = 14646) (out= 6364)(deflated 56%)
adding: log4jdbc.log4j2.properties(in = 110) (out= 88)(deflated 20%)
ignoring entry META-INF/
ignoring entry META-INF/MANIFEST.MF
adding: META-INF/maven/(in = 0) (out= 0)(stored 0%)
adding: META-INF/maven/project/(in = 0) (out= 0)(stored 0%)
adding: META-INF/maven/project/lib/(in = 0) (out= 0)(stored 0%)
adding: META-INF/maven/project/lib/pom.properties(in = 219) (out= 165)(deflated 24%)
adding: META-INF/maven/project/lib/pom.xml(in = 12334) (out= 2747)(deflated 77%)
adding: META-INF/spring-configuration-metadata.json(in = 4849) (out= 521)(deflated 89%)

 


 

- jar파일 압축풀기

[jar -xf foo.jar]
  -x, --extract              Extract named (or all) files from the archive.
  -f, --file=FILE            The archive file name. When omitted, either stdin or stdout is used based on the operation.

C:\Users\administer\Desktop\mng>jar xvf e.g-Archive.jar
 inflated: META-INF/MANIFEST.MF
  created: META-INF/
  created: config/
  created: io/
  created: io/project/
  created: io/project/
  created: io/project/api/web/
  created: META-INF/maven/
  created: META-INF/maven/project/
  created: META-INF/maven/project/lib/
 inflated: META-INF/maven/project/lib/pom.properties
 inflated: META-INF/spring-configuration-metadata.json
 inflated: config/ehcache.xml
 inflated: config/context-dbPool.xml
 inflated: config/jdbcDriverConf.xml
 inflated: io/project/api/web/DemoController.class
 inflated: log4jdbc.log4j2.properties
 inflated: META-INF/maven/project/lib/pom.xml