简体   繁体   English

如何使用Java获取.jar文件中的文件?

[英]How can I get files in .jar file using java?

My problem is getting some CSS files from a foo.jar file. 我的问题是从foo.jar文件中获取一些CSS文件。 How can I get these CSS files from foo.jar? 如何从foo.jar获取这些CSS文件? I found some ways, firstly by unzipping this .jar and copying files and then by deleting the unzipped folder. 我找到了一些方法,首先是解压缩此.jar并复制文件,然后删除已解压缩的文件夹。 Is that a good way? 那是个好方法吗?

My JAR file location is C:\\foo.jar 我的JAR文件位置是C:\\ foo.jar

public static void main(String[] args) throws IOException {
        {
            URL url = getClass().getClassLoader().getResource("css/demo.css");
            URL url2 = getClass().getClassLoader().getResource("");
            System.out.println("url = " + url);
            System.out.println("url_2 = " + url2);

        }
    }

Output: 输出:

url = jar:file:/C:/foo.jar!/css/demo.css 网址= jar:文件:/ C:/foo.jar!/css/demo.css

url_2 = file:/C:/ url_2 =文件:/ C:/

So, I don't need to extract foo.jar. 因此,我不需要提取foo.jar。 I can copy files within JAR like this way. 我可以像这样在JAR中复制文件。 Thank you. 谢谢。

The following example will make you clear Let's extract some files from the TicTacToe JAR file we've been using in previous sections. 下面的示例将使您清楚。让我们从上一部分中一直使用的TicTacToe JAR文件中提取一些文件。 Recall that the contents of TicTacToe.jar are: 回想一下TicTacToe.jar的内容是:

META-INF/MANIFEST.MF
TicTacToe.class
TicTacToe.class
TicTacToe.java
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
example1.html
images/
images/cross.gif
images/not.gif

Suppose you want to extract the TicTacToe class file and the cross.gif image file. 假设您要提取TicTacToe类文件和cross.gif图像文件。 To do so, you can use this command: 为此,可以使用以下命令:

jar xf TicTacToe.jar TicTacToe.class images/cross.gif

This command does two things: 此命令有两件事:

It places a copy of TicTacToe.class in the current directory. 它将TicTacToe.class的副本放置在当前目录中。 It creates the directory images, if it doesn't already exist, and places a copy of cross.gif within it. 如果目录图像尚不存在,它将创建目录图像,并在其中放置cross.gif的副本。

The original TicTacToe JAR file remains unchanged. 原始的TicTacToe JAR文件保持不变。

As many files as desired can be extracted from the JAR file in the same way. 可以按照相同的方式从JAR文件中提取所需数量的文件。 When the command doesn't specify which files to extract, the Jar tool extracts all files in the archive. 当命令未指定要提取的文件时,Jar工具将提取存档中的所有文件。 For example, you can extract all the files in the TicTacToe archive by using this command: 例如,您可以使用以下命令来提取TicTacToe存档中的所有文件:

jar xf TicTacToe.jar

Put that jar into the class path of the project. 将该jar放入项目的类路径中。 Then you can access the resources you needed. 然后,您可以访问所需的资源。

The basic command to use for extracting the contents of a JAR file is: 用于提取JAR文件内容的基本命令是:

jar xf myjarfile.jar; jar xf myjarfile.jar;

for more info please check this link out : 有关更多信息,请查看此链接:

http://docs.oracle.com/javase/tutorial/deployment/jar/unpack.html http://docs.oracle.com/javase/tutorial/deployment/jar/unpack.html

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM