javaregexlinuxshelljar

Extract class names from JAR with special formatting


How would I extract all the available classes from a Jar file and dump the output, after some simple processing, to a txt file.

For example, if I run jar tf commons-math3-3.1.6.jar a subset of the output would be:

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

I would like to convert all / to .

And all $ to .

Finally I would also like to remove the .class that appears at the end of every string.

For instance:

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

would become

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

Solution

  • String path = "org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.class";
    path = path.replaceAll("/", ".")
               .replaceAll("\\$(\\d+)\\.class", "\\.$1");