Skip to content

Commit

Permalink
Fix LoaderUtil saying there are duplicate classpath entries if there …
Browse files Browse the repository at this point in the history
…are none. (#979)

Also futureproofs the checks just in case the class names are changed.
  • Loading branch information
Gamebuster19901 authored Oct 9, 2024
1 parent 319c247 commit 38aa840
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/net/fabricmc/loader/impl/util/LoaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

public final class LoaderUtil {
private static final ConcurrentMap<Path, Path> pathNormalizationCache = new ConcurrentHashMap<>();
private static final String FABRIC_LOADER_CLASS = "net/fabricmc/loader/api/FabricLoader.class";
private static final String ASM_CLASS = "org/objectweb/asm/ClassReader.class";

public static String getClassFileName(String className) {
return className.replace('.', '/').concat(".class");
Expand Down Expand Up @@ -66,17 +68,21 @@ public static void verifyNotInTargetCl(Class<?> cls) {

public static void verifyClasspath() {
try {
List<URL> resources = Collections.list(LoaderUtil.class.getClassLoader().getResources("net/fabricmc/loader/api/FabricLoader.class"));
List<URL> resources = Collections.list(LoaderUtil.class.getClassLoader().getResources(FABRIC_LOADER_CLASS));

if (resources.size() != 1) {
if (resources.size() > 1) {
// This usually happens when fabric loader has been added to the classpath more than once.
throw new IllegalStateException("duplicate fabric loader classes found on classpath: " + resources.stream().map(URL::toString).collect(Collectors.joining(", ")));
} else if (resources.size() < 1) {
throw new AssertionError(FABRIC_LOADER_CLASS + " not detected on the classpath?! (perhaps it was renamed?)");
}

resources = Collections.list(LoaderUtil.class.getClassLoader().getResources("org/objectweb/asm/ClassReader.class"));
resources = Collections.list(LoaderUtil.class.getClassLoader().getResources(ASM_CLASS));

if (resources.size() != 1) {
if (resources.size() > 1) {
throw new IllegalStateException("duplicate ASM classes found on classpath: " + resources.stream().map(URL::toString).collect(Collectors.joining(", ")));
} else if (resources.size() < 1) {
throw new IllegalStateException("ASM not detected on the classpath (or perhaps " + ASM_CLASS + " was renamed?)");
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to get resources", e);
Expand Down

0 comments on commit 38aa840

Please sign in to comment.