Solved ClassLoader.getResources() returns empty enumeration

Discussion in 'Spigot Plugin Development' started by RonFarkash, Jun 15, 2018 at 1:26 AM.

  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.

    EDIT:
    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(Aesir.java:72) ~[?:?]
           at example.example2.Test.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]
     
    #1 RonFarkash, Jun 15, 2018 at 1:26 AM
    Last edited: Jun 16, 2018 at 6:04 PM
  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