jar file

 

The most common command to make a new jar file users the following syntax:

Jar cvf JARFileName File1 File2...

Jar cvfm jarname manifest.mf class1 class2

The option:

-c  create new archive file

-t  list archive file

-x  Extract the archived specified (or all) files

-u  update an existing jar file

-v  In the standard output to generate verbose output

-f  Specify the archive file name

-m  The list of files that contain a specified list of information

-e  Bundled into the executable jar file for the separate applications

     Specify the application entry point

-0  Only storage; do not use any ZIP compression

-M  Does not create entries in the list of documents

-i  generate index info for Specify a jar file

-C  update to specify dictionary and content it file

The Manifest:

Is called MANIFEST.MF and is located in a special META-INF subdirectory of the jar file

The minimum legal manifest is quite boring –just   Manifest-Version: 1.0

 

Complex manifests can have many more entries. The manifest entries are grouped into sections . the first ection in the manifest is calld the main section. It applies to the whole JAR fiile .Subsequent entries can specify properties of named entites such as in dividual  files ,package ,or URLs. Those entries must begin with  a Name entry.Seciton are separated by blank lines.For example:

     Manifest-Version: 1.0

Lines describing this archive

 

Name:Woozle.class

Lines describing this file

Sealing

       The manifest like this:

       Name: aken/xml/

       Sealed: true

      

       Name: aken1/xml/

       Sealed: false

Packaging Beans in JAR Files

Pattern:

Manifest-Version: 1.0

 

Name: com/horstmann/corejava/ImageViewerBean.class

Java-Bean: True

 

We place our example beans into the package com.horstmann.corejava because some builder environments have problems loading beans from the default package.

If your bean contains multiple class files, you just mention in the manifest those class files that are beans and that you want to have displayed in the toolbox. For example, you could place ImageViewerBean and FileNameBean into the same JAR file and use the manifest

Manifest-Version: 1.0
 
Name: com/horstmann/corejava/ImageViewerBean.class
Java-Bean: True
 
Name: com/horstmann/corejava/FileNameBean.class
Java-Bean: True

Make sure that there are no spaces after the ends of each line, that there are blank lines after the version and between bean entries, and that the last line ends in a newline.

### 使用 Java 中的 JarFile 类 `JarFile` 是 `java.util.zip` 包中的一个类,用于表示 JAR 文件并允许访问其中的内容。以下是关于如何使用 `JarFile` 的详细介绍。 #### 创建 JarFile 对象 要创建一个 `JarFile` 实例,可以传递 JAR 文件的路径作为参数给其构造函数。例如: ```java import java.util.jar.JarFile; try (JarFile jarFile = new JarFile("example.jar")) { // 访问 JAR 文件内容的操作 } catch (Exception e) { e.printStackTrace(); } ``` 此代码片段展示了如何打开名为 `example.jar` 的 JAR 文件,并将其封装在一个尝试资源声明 (`try-with-resources`) 中以确保文件被正确关闭[^4]。 #### 列出 JAR 文件中的条目 一旦有了 `JarFile` 对象,就可以通过调用 `entries()` 方法来获取该 JAR 文件中所有条目的枚举器。下面是一个例子: ```java import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; try (JarFile jarFile = new JarFile("example.jar")) { Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); System.out.println(entry.getName()); } } catch (Exception e) { e.printStackTrace(); } ``` 这段代码打印出了 `example.jar` 文件内的每一个条目名称[^5]。 #### 读取特定条目数据 如果想从 JAR 文件里提取某个具体条目,则可以通过它的名字得到对应的 `InputStream` 流对象。如下所示: ```java import java.io.InputStream; import java.util.jar.JarFile; import java.util.jar.JarEntry; try (JarFile jarFile = new JarFile("example.jar")) { JarEntry entry = jarFile.getJarEntry("path/to/resource.txt"); try (InputStream inputStream = jarFile.getInputStream(entry)) { int data; while ((data = inputStream.read()) != -1) { System.out.print((char)data); } } } catch (Exception e) { e.printStackTrace(); } ``` 这里展示的是怎样定位到名为 `"path/to/resource.txt"` 的条目,并逐字节地输出它所包含的数据流[^5]。 --- ### 注意事项 当处理大型或者复杂的 JAR 文件时需要注意性能问题;另外,在实际应用开发过程中也应考虑异常情况的发生,比如目标文件不存在等情况都需要妥善管理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值