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

Java 使用特殊格式从JAR中提取类名,java,regex,linux,shell,jar,Java,Regex,Linux,Shell,Jar,如何从Jar文件中提取所有可用的类,并在经过一些简单的处理后将输出转储到txt文件中 例如,如果我运行jar tf commons-math3-3.1.6.jar输出的一个子集将是: org/apache/commons/math3/analysis/differentiation/UnivariateVectorFunctionDifferentiator.class org/apache/commons/math3/analysis/differentiation/FiniteDiffere

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

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

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
我想将所有/转换为

以及所有$

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

例如:

org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator$2.class
将成为

org.apache.commons.math3.analysis.differentiation.FiniteDifferencesDifferentiator.2

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

以本例为例,我们将在
/path/to/jar/file.jar中用jar文件中包含的所有Java类的列表填充类名。

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()));
    }
}
List classNames=new ArrayList();
ZipInputStream zip=newzipinputstream(newfileinputstream(“/path/to/jar/file.jar”);
for(ZipEntry entry=zip.getnextery();entry!=null;entry=zip.getnextery()){
如果(!entry.isDirectory()&&entry.getName().endsWith(“.class”)){
//这个ZipEntry代表一个类。现在,它代表什么类?
String className=entry.getName().replace(“/”,“.”)。replace(“$”,“);//包括“.class”
add(className.substring(0,className.length()-“.class.length());
}
}

信用证:

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

以本例为例,我们将在
/path/to/jar/file.jar中用jar文件中包含的所有Java类的列表填充类名。

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()));
    }
}
List classNames=new ArrayList();
ZipInputStream zip=newzipinputstream(newfileinputstream(“/path/to/jar/file.jar”);
for(ZipEntry entry=zip.getnextery();entry!=null;entry=zip.getnextery()){
如果(!entry.isDirectory()&&entry.getName().endsWith(“.class”)){
//这个ZipEntry代表一个类。现在,它代表什么类?
String className=entry.getName().replace(“/”,“.”)。replace(“$”,“);//包括“.class”
add(className.substring(0,className.length()-“.class.length());
}
}
学分: