Solved Load File from jar

Discussion in 'Spigot Plugin Development' started by Kiwanga, Mar 18, 2017.

  1. I'm trying to load a file, which is in my project in eclipse...
    The Packages look like this:
    • src
      • com
        • kiwanga
          • game
            • messages
              • MessageConfig.class
      • resources
        • messages.yml
    MY message.yml looks like this:
    Code (Text):
    messages:
      noPermission: '&7Sry, but you do not have the &cpermission&7!'
    So in my MessageConfig I'm doing this:
    Code (Text):
        private File messageYML = new File(getClass().getResource("/resources/messages.yml").getFile());
        private FileConfiguration MessageCfg = YamlConfiguration.loadConfiguration(messageYML);
    If I try now to copy the message from the messages.yml to another file it is null... But doesn't give me any error in the console...
    Code (Text):
    ConfigCfg.set("messages.noPermission", MessageCfg.get(messages.noPermission)); //COPYS String to another YAML-Configuration
    saveConfig(); //Saves the YAML-Configuration
     
  2. the contents of the resources folder gets copied into the root of the jar, so the correct path is "messages.yml" (no need for the initial forward slash either)
     
    • Informative Informative x 1
  3. Code (Text):
    new File(getClass().getResource("messages.yml").getFile());
    There you go :)
     
    • Useful Useful x 1
  4. Go check out my plugin Core, which has a nice config manager that may make life easier for you. The wiki explains all you need to know.
     
    • Like Like x 1
  5. If I try now this:
    Code (Text):
    private File messageYML = new File(getClass().getResource("messages.yml").getFile());
    I get in this line an NPE:
    Code (Text):
    [11:03:49] [Server thread/ERROR]: Error occurred while enabling KingDefence v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.Patheria.KingDefence.Configs.Messages.MessageConfig.<init>(MessageConfig.java:17) ~[?:?]
        at me.Patheria.KingDefence.Main.Main.onEnable(Main.java:23) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) ~[spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:375) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:325) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:749) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.Bukkit.reload(Bukkit.java:540) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:649) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:635) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    I also have a screenoshot of the packages hierarchy:
    Unbenannt.png
     
  6. I'm pretty sure what was said above is incorrect and you are getting a NullPointerException because you do need to do "/messages.yml". However, instead of getting the resource yourself JavaPlugin provides a nice JavaPlugin#getResource method that it uses already to get the config.yml. I'd suggest taking a look at that and using that instead as the work is pretty much already done for you.
     
    • Useful Useful x 1
  7. Hmmmm still get the same NPE with "/messages.yml"...

    I tried it with getResource("") like this:
    Code (Text):
        private File ConfigFile;
        private FileConfiguration ConfigCfg;
       
        private File messageYML; //new File(getClass().getResource("/messages.yml").getFile());
        private FileConfiguration MessageCfg;
       
        public MessageConfig(Main plugin) {
            ConfigFile = new File("plugins/KingDefence", "messages.yml");
            ConfigCfg = YamlConfiguration.loadConfiguration(ConfigFile);
           
            InputStream input = plugin.getResource("messages.yml");
           
            try {
                FileUtils.copyInputStreamToFile(input, messageYML); //GET HERE THE NEW NPE
            } catch (IOException e) {
                e.printStackTrace();
            }
           
            MessageCfg = YamlConfiguration.loadConfiguration(messageYML);
        }
    And here is the stackrtrace:
    Code (Text):
    [12:09:30] [Server thread/ERROR]: Error occurred while enabling KingDefence v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:352) ~[spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:326) ~[spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.apache.commons.io.FileUtils.copyInputStreamToFile(FileUtils.java:1510) ~[spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at me.Patheria.KingDefence.Configs.Messages.MessageConfig.<init>(MessageConfig.java:31) ~[?:?]
        at me.Patheria.KingDefence.Main.Main.onEnable(Main.java:22) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) ~[spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:375) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:325) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:749) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.Bukkit.reload(Bukkit.java:540) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:649) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:635) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot_server.jar:git-Spigot-6f7aabf-c8ff651]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Also I copied the messages.yml in the src folder, cause you said it also loads the config.yml... So I did it in the same folder like a config.yml os supposed to be:
    Unbenannt.png
     
  8. So I'm guessing it's saying something with your file is null. However, instead of using the FileUtils I'd suggest using again the Bukkit provided methods. I believe there may be a JavaPlugin#getResourceAsFile method or something along those lines.
     
    • Like Like x 1
  9. Thanks to all who helped me!

    I found by googling about the stacktrace the result. Here is the code:
    Code (Text):
        private File ConfigFile;
        private FileConfiguration ConfigCfg;
     
        private File messageYML;
        private FileConfiguration MessageCfg;
     
        public MessageConfig() {
            ConfigFile = new File("plugins/KingDefence", "messages.yml");
            ConfigCfg = YamlConfiguration.loadConfiguration(ConfigFile);
         
            InputStream input = this.getClass().getResourceAsStream("/messages.yml");
         
            File tempFile = null;
            try {
                tempFile = File.createTempFile("messages", ".yml");
                tempFile.deleteOnExit();
                FileOutputStream out = new FileOutputStream(tempFile);
                IOUtils.copy(input, out);
            } catch (IOException e) {
                e.printStackTrace();
            }
            messageYML = tempFile;
           
         
            MessageCfg = YamlConfiguration.loadConfiguration(messageYML);
        }
    You have to put a / in front if the path when you use getResourceAsStream cause it is in the src
     
  10. Not really every time.
    For example your configuration file is in the same package as your class that want access.

    Your.java
    Config.yml

    Or
    Your.java
    subPackage/Config.yml

    You can access it with this.getClass().getResourceAsStream("Config.yml")
    or
    this.getClass().getResourceAsStream("subPackage/Config.yml")



    Currently you are accessing your file through a absolute path. My example is relative.
    Absolute: /yourpath/yourFile
    Relative: yourpath/yourFile
     
    • Funny Funny x 1

Share This Page