首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用特殊格式从JAR中提取类名

用特殊格式从JAR中提取类名
EN

Stack Overflow用户
提问于 2016-10-15 16:32:50
回答 2查看 335关注 0票数 0

如何从Jar文件中提取所有可用的类,并在经过一些简单处理后将输出转储到txt文件。

例如,如果我运行jar tf commons-math3-3.1.6.jar,输出的一个子集将是:

代码语言:javascript
复制
org/apache/commons/math3/analysis/differentiation/UnivariateVectorFunctionDifferentiator.class
org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator$2.class
org/apache/commons/math3/analysis/differentiation/SparseGradient$1.class
org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator$1.class
org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionRuleFactory.class
org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.class
org/apache/commons/math3/analysis/integration/gauss/HermiteRuleFactory.class
org/apache/commons/math3/analysis/integration/gauss/LegendreRuleFactory.class
org/apache/commons/math3/analysis/integration/gauss/GaussIntegratorFactory.class

我想把所有的/转换成.

以及$ to .

最后,我还想删除出现在每个字符串末尾的.class

例如:

代码语言:javascript
复制
org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator$2.class

会变成

代码语言:javascript
复制
org.apache.commons.math3.analysis.differentiation.FiniteDifferencesDifferentiator.2
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-15 16:38:45

代码语言:javascript
复制
String path = "org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.class";
path = path.replaceAll("/", ".")
           .replaceAll("\\$(\\d+)\\.class", "\\.$1");
票数 2
EN

Stack Overflow用户

发布于 2016-10-15 16:39:42

在我看来,在程序中执行shell命令不是很好,所以您可以做的是以编程的方式检查文件。

以这个例子为例,我们将在classNames中填充/path/to/jar/file.jar.中包含在jar文件中的所有Java类的列表

代码语言:javascript
复制
List<String> classNames = new ArrayList<String>();
ZipInputStream zip = new ZipInputStream(new FileInputStream("/path/to/jar/file.jar"));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
    if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
        // This ZipEntry represents a class. Now, what class does it represent?
        String className = entry.getName().replace('/', '.').replace('$',''); // including ".class"
        classNames.add(className.substring(0, className.length() - ".class".length()));
    }
}

信贷:Here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40061549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档