java - .getClassLoader().getResourceAsStream(path) caches result -


i load file @ servlet, use .getclassloader().getresourceasstream(path), path in web-inf/classes dir, found after changed path file content, file servlet loads same, don't change, file cached.

example code:

this method gets same result every time, after change test.key content

private string getkey(string param){     string name = "keys/"+param+"/test.key";     inputstream in = xxxservlet.class.getclassloader().getresourceasstream(name);     stringbuilder builder = new stringbuilder();     try {         bufferedreader reader = new bufferedreader(new inputstreamreader(in));         string line = null;         while((line = reader.readline()) != null){             builder.append(line).append("\n");         }     } catch (ioexception ignoreexception) {      }finally{         try {             in.close();         } catch (ioexception e) {             e.printstacktrace();         }     }      string result = builder.tostring();     return result; } 

=================================================================

change these 2 lines code, works fine

    string name = "/web-inf/classes/keys/"+param+"/test.key";     inputstream in = getservletcontext().getresourceasstream(name); 

ankur right. existing class loader trying class or resource (regardless of name , path file/inputstream) never reload if name , associated content has been loaded once class loader or it's parent. performance reasons. way create new instance of class loader , again. then, @ least classes, you'll have worry incompatible classes running in system. can't assign instance of new class variable typed class loaded first class loader instance since they're technically different classes.

pabrantes thinks different because liu not loading 'class' per say, key: "keys/"+param+"/test.key"; he's using classloader , rules same on loading 'name' respect getresourceasstream(name). doesn't matter if it's class or not, classloader think "oh here go, i've loaded byte stream 'name'". pulls right out of permgen. interested - if create/implement own version of new classloader reloads every time - make sure specific paths or name patterns. keep in mind every copy load space in permgen on time permgen grow out of control unless unload.

so - that's why doesn't work. contextloader use. :-)

dan c.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -