From 41609ee63f1df4b42a1daf537e05dafc1d0f18f7 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Fri, 16 Oct 2020 23:38:46 +0200 Subject: [PATCH] [BAEL-4533] get class names from a jar --- core-java-modules/core-java-jar/.gitignore | 1 + .../baeldung/jar/GetClassNamesFromJar.java | 43 ++++++++++++++++++ .../jar/GetClassNamesFromJarUnitTest.java | 39 ++++++++++++++++ .../example-jar/stripe-0.0.1-SNAPSHOT.jar | Bin 0 -> 10082 bytes 4 files changed, 83 insertions(+) create mode 100644 core-java-modules/core-java-jar/.gitignore create mode 100644 core-java-modules/core-java-jar/src/main/java/com/baeldung/jar/GetClassNamesFromJar.java create mode 100644 core-java-modules/core-java-jar/src/test/java/com/baeldung/jar/GetClassNamesFromJarUnitTest.java create mode 100644 core-java-modules/core-java-jar/src/test/resources/example-jar/stripe-0.0.1-SNAPSHOT.jar diff --git a/core-java-modules/core-java-jar/.gitignore b/core-java-modules/core-java-jar/.gitignore new file mode 100644 index 0000000000..ba516c02b5 --- /dev/null +++ b/core-java-modules/core-java-jar/.gitignore @@ -0,0 +1 @@ +!src/test/resources/example-jar/stripe-0.0.1-SNAPSHOT.jar diff --git a/core-java-modules/core-java-jar/src/main/java/com/baeldung/jar/GetClassNamesFromJar.java b/core-java-modules/core-java-jar/src/main/java/com/baeldung/jar/GetClassNamesFromJar.java new file mode 100644 index 0000000000..0d8a67dbd6 --- /dev/null +++ b/core-java-modules/core-java-jar/src/main/java/com/baeldung/jar/GetClassNamesFromJar.java @@ -0,0 +1,43 @@ +package com.baeldung.jar; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +public class GetClassNamesFromJar { + + public static Set getClassNamesFromJarFile(File givenFile) throws IOException { + Set classNames = new HashSet<>(); + try (JarFile jarFile = new JarFile(givenFile)) { + Enumeration e = jarFile.entries(); + while (e.hasMoreElements()) { + JarEntry jarEntry = e.nextElement(); + if (jarEntry.getName().endsWith(".class")) { + String className = jarEntry.getName() + .replace("/", ".") + .replace(".class", ""); + classNames.add(className); + } + } + return classNames; + } + } + + public static Set getClassesFromJarFile(File jarFile) throws IOException, ClassNotFoundException { + Set classNames = getClassNamesFromJarFile(jarFile); + Set classes = new HashSet<>(classNames.size()); + try (URLClassLoader cl = URLClassLoader.newInstance(new URL[] { new URL("jar:file:" + jarFile + "!/") })) { + for (String name : classNames) { + Class clazz = cl.loadClass(name); // Loading the class by its name + classes.add(clazz); + } + } + return classes; + } +} diff --git a/core-java-modules/core-java-jar/src/test/java/com/baeldung/jar/GetClassNamesFromJarUnitTest.java b/core-java-modules/core-java-jar/src/test/java/com/baeldung/jar/GetClassNamesFromJarUnitTest.java new file mode 100644 index 0000000000..7af3ff59fd --- /dev/null +++ b/core-java-modules/core-java-jar/src/test/java/com/baeldung/jar/GetClassNamesFromJarUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.jar; + +import com.google.common.collect.Sets; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +public class GetClassNamesFromJarUnitTest { + private static final String JAR_PATH = "example-jar/stripe-0.0.1-SNAPSHOT.jar"; + private static final Set EXPECTED_CLASS_NAMES = Sets.newHashSet( + "com.baeldung.stripe.StripeApplication", + "com.baeldung.stripe.ChargeRequest", + "com.baeldung.stripe.StripeService", + "com.baeldung.stripe.ChargeRequest$Currency", + "com.baeldung.stripe.ChargeController", + "com.baeldung.stripe.CheckoutController"); + + @Test + public void givenJarFilePath_whenLoadClassNames_thenGetClassNames() throws IOException, URISyntaxException { + File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI()); + Set classNames = GetClassNamesFromJar.getClassNamesFromJarFile(jarFile); + Assert.assertEquals(EXPECTED_CLASS_NAMES, classNames); + } + + @Test + public void givenJarFilePath_whenLoadClass_thenGetClassObjects() throws IOException, ClassNotFoundException, URISyntaxException { + File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI()); + Set classes = GetClassNamesFromJar.getClassesFromJarFile(jarFile); + Set names = classes.stream().map(Class::getName).collect(Collectors.toSet()); + Assert.assertEquals(EXPECTED_CLASS_NAMES, names); + } +} diff --git a/core-java-modules/core-java-jar/src/test/resources/example-jar/stripe-0.0.1-SNAPSHOT.jar b/core-java-modules/core-java-jar/src/test/resources/example-jar/stripe-0.0.1-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..213d7f94001cf375a3bfc9b7d4ee0500851e572b GIT binary patch literal 10082 zcmb7K1yqz<*GB0^1f-<9TUuJWTT&Qu$e~NRyOB`3QzWINyBnmWr9ncFf4F+Zi{88b z@2quZ-dS_@b84S`_Pd`gF9ivO0C7{wa^w~Mdie1H5B`=BRTiX|kQHN;|IrK%q7>YW zb5lAS2>#m}TrqxaCL<^-AttJ%%pfCnB-7U`Ek(~Tj4VY@Gtf8qR*`9pb#c{}PI91& zPKsU#4)#-)YAhCQGnrFuiX1IPujWGsTShz(Lq3z+?fO{cyu5;eLGWwpHuj~(arqr z02bCjeLDc?mv%Q(1^@N4K!KkOtStVc1@ey;FZ2OGLwif(|7d!%ZZ}{*n}Y0YO|1bx zCE)&=3#+8X+bwWQ`+E=&h~RIamGKLG19Ju^3!qA*oOvq~Qv2RH#)n0Nl(=$c$rKh2 zNkuz8vl4R4@_jbe~0to@ft|PQ+3u3MC?i(qqhAozGZ{kXJik1y0 zi;iF`3Mv%2>-WYI9x?Z;;AGK=E@WB^`(VOws zl?pF;1OdI6s8@;i^}TCr^T~q*RLG(0^8K=t09BHr`KF@z?KqMM-RI8a`Of53k}wkp z*`i!k7huino<7eemqfUbe|!AEf0}GHS9xZ7!-tyT4bqg@Pp$httoxsu6llS_lzkrp z0uwx}TZOU(fb4;Ge+|t&y4 zf=1W!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