
Question:
he there,
been stuck for a while now. What im trying to do comes down to this:
<ul><li>I have developed a little library which i am deploying in form of a jar-file. it contains a file i need to access, which is located in "json2/json.js" (yep, doin jscript/rhino things)</li> <li>I import the jar-file into a classical java-project, it works fine. File is found and read</li> <li>I import the SAME jar-file into tomcat-project which looks as follows: <i></i></li> <li>a .jsp that calls a java-class</li> <li>inside the java-class i call my library, which is located in the tomcat/lib-directory</li> <li>now when i try to access the file i cant (null-pointer) </li> </ul>sources:
InputStream in = ClassLoader.getSystemResourceAsStream("json2/json.js");
when i run it inside the tomcat, (in==null).
for diagnosis i improvised the following:
File fu = new File(new URI(this.getClass().getClassLoader().getResource("").toString()));
String[] l = fu.list();
for (int i = 0; i < l.length; i++) {
System.out.println(i+"||"+l[i]);
}
with the classic java-class it only produces my main-class-file. in tomcat it shows me the content of the "tomcat/lib"-directory.
any ideas? would be greatly appreciated...
<hr />EDIT
one detail that i forgot to add (and that really grinds my gears):
SParser.class.getClassLoader().getParent().getResource("")
comes up with a null-ptr. wtf? im not accessing a particular ressource, still no result.
Answer1:Do not use getSystemResourceAsStream()
. Use getResourceAsStream()
. On a Tomcat webapp environment, the system classloader has no knowledge of Tomcat/lib
nor the webapp libraries.
Also, when grabbing the ClassLoader
, you should preferably grab the context class loader of the current thread.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("/json/json.js");
Answer2:Put the JAR file in the WEB-INF/lib
directory of your application.