How does a Java application stored within a jar file reference/edit/read other files (like .txt, or data files,) that are also within the jar file? Answer: Classes located in a JAR archive are loaded via a class loader whose purpose is to load classes form JAR archives. This ClassLoader implements the getResource and getResourceAsStream methods to retrieve files from the JAR file. So you can take any class from the JAR and say ClassName.class.getClassLoader().getResource("fname"); to get the resource and use it.
jar tvf test.jar
即可列出jar文件里的所有内容
一般的解压缩文件都可以打开的,
你可以把他的后缀名改成.zip,
然后打开,
一切OK!
jar 是java的一个压缩文件名 (java archive),但是格式仍是zip的,所以你可以用winzip等支持zip格式的软件打开,如果你是想自己生成一个jar文件,你可以用 jar工具来做。在命令行打 jar
即可看到它的帮助:用法:jar {ctxu}[vfm0M] [jar-文件] [manifest-文件] [-C 目录] 文件名 ...
选项:
-c 创建新的归档
-t 列出归档内容的列表
-x 展开归档中的命名的(或所有的〕文件
-u 更新已存在的归档
-v 生成详细输出到标准输出上
-f 指定归档文件名
-m 包含来自指定的清单(manifest〕文件的清单(manifest〕信息
-0 只存储方式;未用ZIP压缩格式
-M 不产生所有项的清单(manifest〕文件
-i 为指定的jar文件产生索引信息
-C 改变到指定的目录,并且包含下列文件:
如果一个文件名是一个目录,它将被递归处理。
清单(manifest〕文件名和归档文件名都需要被指定,按'm' 和 'f'标志指定的相同顺序。示例1:将两个class文件归档到一个名为 'classes.jar' 的归档文件中:
jar cvf classes.jar Foo.class Bar.class
示例2:用一个存在的清单(manifest〕文件 'mymanifest' 将 foo/ 目录下的所有
文件归档到一个名为 'classes.jar' 的归档文件中:
jar cvfm classes.jar mymanifest -C foo/ .
jar tvf test.jar 查看它所包含文件
jar cvfz /user/a.tgz . // zip the . directory file to a.tgz
jar xvfz a.tgz // unzip the a.tgz
(like .txt, or data files,) that are also within the jar file?
Answer: Classes located in a JAR archive are loaded via a class loader whose purpose
is to load classes form JAR archives. This ClassLoader implements the getResource
and getResourceAsStream
methods to retrieve files from the JAR file. So you can take any class from the JAR
and say ClassName.class.getClassLoader().getResource("fname");
to get the resource and use it.