Java JAR - 提取特定文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25972910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:36:54  来源:igfitidea点击:

JAR - extracting specific files

javajarextractarchive

提问by user3521479

I have .class and .java files in JAR archive. Is there any way to extract only .java files from it? I've tried this command but it doesn't work:

我在 JAR 存档中有 .class 和 .java 文件。有没有办法只提取 .java 文件?我试过这个命令,但它不起作用:

jar xf jar-file.jar *.java

采纳答案by Pierre Rust

You can use the unzipcommand which accepts wildcards in its arguments (which is not the case of the jarcommand). Something like this should do the trick ( disclaimer : not tested)

您可以使用unzip在其参数中接受通配符的命令(该jar命令不是这种情况)。这样的事情应该可以解决问题(免责声明:未经测试)

unzip youFile.jar "*.java"

回答by Anders R. Bystrup

From the source:

来源:

To extract only certain files from a jar file, supply their filenames:

C:\Java> jar xf myFile.jar foo bar

要仅从 jar 文件中提取某些文件,请提供它们的文件名:

C:\Java> jar xf myFile.jar foo bar

Using wildcards is a shell thing, and you should not expect it to work when extractingfrom a JAR file (which, as you've realized, is the case).

使用通配符是 shell 的事情,在从 JAR 文件中提取时,您不应该期望它起作用(正如您已经意识到的那样)。

What you cando, is supply a list of the files you want to extract, via the @modifier:

可以做的是通过@修饰符提供要提取的文件列表:

jar xf my.jar @myfiles.lst

where myfiles.lstwould be:

在哪里myfiles.lst

com/my/MyClass.java
com/my/MySecondClass.java

This file could easily be created automatically with creative use of jar tvf, awkand your desired extraction pattern. I'll leave that as an exercise and/or another SO question :-)

通过创造性地使用和您想要的提取模式jar tvf,可以轻松地自动创建此文件awk。我将把它作为练习和/或另一个 SO 问题:-)

Or you could use unzip- a JAR file is basically a zip-file.

或者您可以使用unzip- JAR 文件基本上是一个 zip 文件。

Cheers,

干杯,

回答by Adrian Ng

There shouldn't be any *.java files in a jar, only class files. You can check the content of the jar first by running:

jar 中不应该有任何 *.java 文件,只有类文件。您可以先通过运行来检查 jar 的内容:

jar tvf jar-file.jar