Solved ClassLoader.getResources() returns empty enumeration

Discussion in 'Spigot Plugin Development' started by RonFarkash, Jun 15, 2018.

  1. I have been using the getResources() function of the ClassLoader class to load classes in a plugin.

    The package the main class is found in is: "example.example2". 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.

    Code (Java):
    ClassLoader loader = this.getClass().getClassLoader();
    Enumeration<URL> urls = this.loader.getResources("/example/example2");
    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.

    Tried File.separator in any spot possible.

    Complete Stack Trace:

    Code (Text):
    [03:07:43 INFO]: [Test] Enabling Testv1.0
    [03:07:43 INFO]: [Test] Testhas been enabled.
    [03:07:43 ERROR]: Error occurred while enabling Test v1.0 (Is it up to date?)
    java.util.NoSuchElementException: null
           at sun.misc.CompoundEnumeration.nextElement(Unknown Source) ~[?:1.8.0_151]
           at example.example2.Test.load( ~[?:?]
           at example.example2.Test.onEnable( ~[?:?]
           at ~[spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.plugin.SimplePluginManager.enablePlugin( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.Bukkit.reload( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.command.defaults.ReloadCommand.execute( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.command.SimpleCommandMap.dispatch( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at net.minecraft.server.v1_12_R1.DedicatedServer.aP( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at net.minecraft.server.v1_12_R1.DedicatedServer.D( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at net.minecraft.server.v1_12_R1.MinecraftServer.C( [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at [spigot-1.12.1.jar:git-Spigot-da42974-8f47214]
           at Source) [?:1.8.0_151]
    #1 RonFarkash, Jun 15, 2018
    Last edited: Jun 16, 2018
  2. you have to get the resources as streams because you cant get paths to files when theyre in jars
  3. Providing path using the getResourceAsStream returns the same result, null.
  4. Needed to compile the project with Maven, another plugin developer explained me that Maven puts the entire project in the metadata that way and getResources will be able to fetch what I wanted.

Share This Page