Solved IO Exception

Discussion in 'Spigot Plugin Development' started by Lifeonblack, May 24, 2017.

  1. Code (Java):
    [16:52:36 WARN]: java.io.IOException: The system cannot find the path specified
    [16:52:36 WARN]:        at java.io.WinNTFileSystem.createFileExclusively(Native
    Method)
    [16:52:36 WARN]:        at java.io.File.createNewFile(Unknown Source)
    [16:52:36 WARN]:        at org.lifeonblack.ConfigAccessor.setup(ConfigAccessor.j
    ava:25)
    [16:52:36 WARN]:        at org.lifeonblack.CustomRecipe.onEnable(CustomRecipe.ja
    va:33)
    [16:52:36 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in.java:271)
    [16:52:36 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:337)
    [16:52:36 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager.java:405)
    [16:52:36 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlu
    gin(CraftServer.java:376)
    [16:52:36 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlu
    gins(CraftServer.java:326)
    [16:52:36 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(Cr
    aftServer.java:750)
    [16:52:36 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:540)
    [16:52:36 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(Rel
    oadCommand.java:25)
    [16:52:36 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:141)
    [16:52:36 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchC
    ommand(CraftServer.java:650)
    [16:52:36 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchS
    erverCommand(CraftServer.java:636)
    [16:52:36 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.aM(Dedi
    catedServer.java:437)
    [16:52:36 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.D(Dedic
    atedServer.java:400)
    [16:52:36 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.C(Minec
    raftServer.java:678)
    [16:52:36 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.run(Min
    ecraftServer.java:576)
    [16:52:36 WARN]:        at java.lang.Thread.run(Unknown Source)
    so I am creating my file with this code
    Code (Java):
    public void setup(JavaPlugin p) {
            file = new File(p.getDataFolder(),"recipes.yml");
            if(!file.exists()) {
                try {
                    file.createNewFile();
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
            config = YamlConfiguration.loadConfiguration(file);
        }
    and I used ConfigAccessor.getInstance().setup(this); onEnable();

    and yet the system cannot find the path specified.
     
  2. Are you sure the folder 'JavaPlugin#getDataFolder()' exists?.. I doubt that.
     
  3. JanTuck

    Supporter

    Do you call this in a static way? I see you said you did it in onEnable but do you do it somewhere else too?
     
  4. I doubt it at the first place too, if it is necessary to create one. How would you create one? data folder is not created since I am not using server config.yml.
     
  5. yes in a static way, I used it somewhere but there is no error yet within that case.
     
  6. There should be something like File.mkdirs() that should do the trick.
     
  7. JanTuck

    Supporter

    If you call it static the p.getDataFolder() might not be set or created. Please provide some more of the source code if can.
     
  8. .getDataFolder() returns a field set on instantiation of the JavaPlugin, so that shouldn't be the problem.
     
  9. Code (Java):
    package org.lifeonblack;

    import java.io.File;
    import java.io.IOException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class ConfigAccessor {
       
        private File file;
        private FileConfiguration config;
        private ConfigAccessor() { }
       
        static ConfigAccessor instance = new ConfigAccessor();
       
        public static ConfigAccessor getInstance() {
            return instance;
        }
       
        public void setup(JavaPlugin p) {
            file = new File(p.getDataFolder(),"recipes.yml");
            if(!file.exists()) {
                try {
                    file.createNewFile();
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
            config = YamlConfiguration.loadConfiguration(file);
        }
       
        public void save() {
            try {
                config.save(file);
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
       
        public void reload() {
            config = YamlConfiguration.loadConfiguration(file);
        }
       
        public FileConfiguration getConfig() {
            return config;
        }

    }
    Code (Java):
    // main class
    ConfigAccessor config = ConfigAccessor.getInstance();
    // on enable
    config.setup(this);
     
  10. p.getDataFolder().mkdir(). In this way you generate the plugin folder. No need to check if already exist, since it create the folder only if it not already exist.
     
  11. that works :)