Getting a resource out of the jar

Discussion in 'Plugin Development' started by Bone008, Jun 22, 2011.

Thread Status:
Not open for further replies.
  1. I'm really confused with the getResourceAsStream(...)-method. At first there a a dozens of ways to access it, let it be a ClassLoader got from the JavaPlugin, or just the Class with ClassName.class, or even this.getClass() or this.getClass().getClassLoader() ...
    I tested all of them and nothing seems to work.

    I'm trying to write a default config file that is stored in the jar-file (as a resource I suppose).

    Code:java
    1. private void writeDefaultConfig(File cfgFile){
    2. InputStream in = this.getClass().getClassLoader().getResourceAsStream("/defaults/config.yml");
    3. if(in == null){
    4. throw new NoClassDefFoundError("Resource of the default config not found!");
    5. }
    6. // writing the stuff
    7. }

    I can't get this to work! I tried everything at the first line of the body.
    this.getClass().getClassLoader().getResourceAsStream("/defaults/config.yml");
    this.getClass().getResourceAsStream("/defaults/config.yml");
    this.getClassLoader().getResourceAsStream("/defaults/config.yml");
    DeathControl.class.getResourceAsStream("/defaults/config.yml");

    But my exception is always thrown -.-
    I created the config.yml with Eclipse. First a new folder called "defaults" in the root of the project (next to the src-folder). Then just a "File" inside of this folder called config.yml.
    I check it to export and it also is located in the jar under defaults/config.yml

    So why isn't the resource found?
     
  2. Offline

    nisovin

    This is what I use:

    Code:
     public void loadConfigFromJar() {
    
            File configFile = new File(this.getDataFolder(), "config.yml");
    
            if (!configFile.exists()) {
    
                InputStream fis = getClass().getResourceAsStream("/config.yml");
    
                FileOutputStream fos = null;
    
                try {
    
                    fos = new FileOutputStream(configFile);
    
                    byte[] buf = new byte[1024];
    
                    int i = 0;
    
                    while ((i = fis.read(buf)) != -1) {
    
                        fos.write(buf, 0, i);
    
                    }
    
                } catch (Exception e) {
    
                    getServer().getLogger().log(Level.SEVERE, "Failed to load config from JAR", e);
    
                } finally {
    
                    try {
    
                            if (fis != null) {
    
                                fis.close();
    
                            }
    
                            if (fos != null) {
    
                                fos.close();
    
                            }
    
                    } catch (Exception e) {                         
    
                    }
    
                }
    
            }
    
    }
    
    Note that I didn't write this myself, someone posted it once and I stole it.
     
  3. Where do you have your config.yml located in the .jar-file?
    The problem with me is that getResourceAsStream doesn't find the resource and thus returns null ...
     
  4. Offline

    Shamebot

    @nisovinDoes this work when you replace your jar with a new version of it and reload the server?
    For I know of some plugins using a similar method and it doesn't work for them in this case.
     
  5. @Shamebot (I supoose you meant me)
    I'm not sure what you mean with your statement. I usually export the jar directly at the plugins/-directory of my local server and overwrite the old one. Then I just use /reload.

    Now I tried deleting the jar, starting the server without the plugin, stopping it again, putting the jar back in and starting it again. And it worked!
    I've no idea what made the difference, it's maybe just something about the GC of the JRE or sth like that oO
     
  6. Offline

    Shamebot

    @Bone008 I know of some plugins which use a method similar to nisovin's.
    When you reload the server without touching the jars everything works fine, but when you replace the jar in the plugins folder with a new version and reload the server you get an exception.
    So I wanted to know from nisovin whether his code works in this case.

    However you seem to have tested your code by reloading the server and had therefor the same issue.
     
Thread Status:
Not open for further replies.

Share This Page