java - In what situation is a file placed in the resources of a JAR, but is not accessible in a class' ClassLoader? -


i'm trying read in simple .txt file java project using this.class.getresourceasstream(filename). have several files within main/resources, , of them return object when try them input stream. object can't read in text file.

i have placed file of other resource files readable classloader, appears file wasn't placed in class' classloader whatever reason. if unzip jar, file still included jar in same directory of other resources, seems being built correctly.

i guess i'm asking @ point tell java files want included resource in class' classloader? should done when jar built if things in correct place (i.e main/resources)?

here code looks like, , it's respective return values, when running file can find , file can't, both located in same place.

// not found. both placed @ src/main/resources def tmpdict = this.class.getresourceasstream("dict.txt")  println tmpdict // null  // found def tmpdict2 = this.class.getresourceasstream("calc.config")  println tmpdict2 // sun.net.www.protocol.jar.jarurlconnection$jarurlinputstream@2dae5a79 

without more info i'd path wrong. when used "file.txt" path got npe

i used method read stream. file located @ \src\main\resources\static\file.txt worked in eclipse, packaged jar , worked there too.

public string getfile() throws exception {     inputstream in = controller.class.getclassloader().getresourceasstream("static/file.txt");     bufferedreader reader = new bufferedreader(new inputstreamreader(in, charset.defaultcharset()));     stringbuilder out = new stringbuilder();     string line;     while ((line = reader.readline()) != null) {         out.append(line);     }             return out.tostring(); } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -