Skip to content

Commit b1b9d2e

Browse files
committed
[BAEL-4533] get class names from a jar
1 parent f8ae698 commit b1b9d2e

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!src/test/resources/example-jar/stripe-0.0.1-SNAPSHOT.jar
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.jar;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import java.net.URLClassLoader;
7+
import java.util.Enumeration;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
import java.util.jar.JarEntry;
11+
import java.util.jar.JarFile;
12+
13+
public class GetClassNamesFromJar {
14+
15+
public static Set<String> getClassNamesFromJarFile(File givenFile) throws IOException {
16+
Set<String> classNames = new HashSet<>();
17+
try (JarFile jarFile = new JarFile(givenFile)) {
18+
Enumeration<JarEntry> e = jarFile.entries();
19+
while (e.hasMoreElements()) {
20+
JarEntry jarEntry = e.nextElement();
21+
if (jarEntry.getName().endsWith(".class")) {
22+
String className = jarEntry.getName()
23+
.replace("/", ".")
24+
.replace(".class", "");
25+
classNames.add(className);
26+
}
27+
}
28+
return classNames;
29+
}
30+
}
31+
32+
public static Set<Class> getClassesFromJarFile(File jarFile) throws IOException, ClassNotFoundException {
33+
Set<String> classNames = getClassNamesFromJarFile(jarFile);
34+
Set<Class> classes = new HashSet<>(classNames.size());
35+
try (URLClassLoader cl = URLClassLoader.newInstance(new URL[] { new URL("jar:file:" + jarFile + "!/") })) {
36+
for (String name : classNames) {
37+
Class clazz = cl.loadClass(name); // Loading the class by its name
38+
classes.add(clazz);
39+
}
40+
}
41+
return classes;
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.jar;
2+
3+
import com.google.common.collect.Sets;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.net.URISyntaxException;
10+
import java.util.Arrays;
11+
import java.util.Objects;
12+
import java.util.Set;
13+
import java.util.stream.Collectors;
14+
15+
public class GetClassNamesFromJarUnitTest {
16+
private static final String JAR_PATH = "example-jar/stripe-0.0.1-SNAPSHOT.jar";
17+
private static final Set<String> EXPECTED_CLASS_NAMES = Sets.newHashSet(
18+
"com.baeldung.stripe.StripeApplication",
19+
"com.baeldung.stripe.ChargeRequest",
20+
"com.baeldung.stripe.StripeService",
21+
"com.baeldung.stripe.ChargeRequest$Currency",
22+
"com.baeldung.stripe.ChargeController",
23+
"com.baeldung.stripe.CheckoutController");
24+
25+
@Test
26+
public void givenJarFilePath_whenLoadClassNames_thenGetClassNames() throws IOException, URISyntaxException {
27+
File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI());
28+
Set<String> classNames = GetClassNamesFromJar.getClassNamesFromJarFile(jarFile);
29+
Assert.assertEquals(EXPECTED_CLASS_NAMES, classNames);
30+
}
31+
32+
@Test
33+
public void givenJarFilePath_whenLoadClass_thenGetClassObjects() throws IOException, ClassNotFoundException, URISyntaxException {
34+
File jarFile = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI());
35+
Set<Class> classes = GetClassNamesFromJar.getClassesFromJarFile(jarFile);
36+
Set<String> names = classes.stream().map(Class::getName).collect(Collectors.toSet());
37+
Assert.assertEquals(EXPECTED_CLASS_NAMES, names);
38+
}
39+
}
Binary file not shown.

0 commit comments

Comments
 (0)