简体   繁体   中英

ClassLoader.getResources() returns empty enumeration

I have been using the getResources() function of the ClassLoader class to load classes in a certain Plugin I am coding in Minecraft.

The package the main class is found in is: "rr.aesir". I tried every possibility with this package name with slashes before, after, dots, inbetween and everything returned an empty Enumeration. (Spigot version: 1.12.1). tried to call it from other classes aswell.

ClassLoader loader = this.getClass().getClassLoader();
Enumeration<URL> urls = this.loader.getResources("/rr/aesir");
String path = urls.nextElement().getPath();

This code is placed in my main class that extends JavaPlugin. I am getting an error saying NoSuchElementException. I have not called nextElement() anywhere in the class so it's not that I skipped any existing elements.

I am out of options, any help is accepted.

Complete Stack Trace:

[03:07:43 INFO]: [Aesir] Enabling Aesir v1.0
[03:07:43 INFO]: [Aesir] Aesir has been enabled.
[03:07:43 ERROR]: Error occurred while enabling Aesir v1.0 (Is it up to date?)
java.util.NoSuchElementException: null
        at sun.misc.CompoundEnumeration.nextElement(Unknown Source) ~[?:1.8.0_151]
        at rr.aesir.Aesir.load(Aesir.java:72) ~[?:?]
        at rr.aesir.Aesir.onEnable(Aesir.java:47) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:384) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:333) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:755) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.Bukkit.reload(Bukkit.java:525) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:651) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand(CraftServer.java:637) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at net.minecraft.server.v1_12_R1.DedicatedServer.aP(DedicatedServer.java:444) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:407) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]

Picture of class structure:

Class structure inside project.

Remove the leading / .

To make code compile, also remove this. before loader .

ClassLoader loader = this.getClass().getClassLoader();
Enumeration<URL> urls = loader.getResources("rr/aesir");
String path = urls.nextElement().getPath();

Note that getPath() may not return the string you think it does.

url.toString() returns something like file:/C:/foo/bar/rr/aesir
url.getPath() returns /C:/foo/bar/rr/aesir

If you want an actual path, call Paths.get(url.toURI()).toString()
It returns C:\\foo\\bar\\rr\\aesir

If you are pre-Java 7, call new File(url.toURI()).toString()
That of course won't work if your code is running from a jar file.

I think that you're a bit misunderstanding what getResources exactly does, assuming that you want to list all the resources within the rr/aesir directory.

According to the javadoc :

Finds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

There can be multiple resources with a specific name, that's why it returns a enumeration. And does not list resources in the directory.

The solution in the following question is what you can use to list the resources: How do I list the files inside a JAR file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM