accessing jar-files from tomcat

匆匆过客 提交于 2019-12-11 13:53:46

问题


he there,

been stuck for a while now. What im trying to do comes down to this:

  • 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)
  • I import the jar-file into a classical java-project, it works fine. File is found and read
  • I import the SAME jar-file into tomcat-project which looks as follows:
  • a .jsp that calls a java-class
  • inside the java-class i call my library, which is located in the tomcat/lib-directory
  • now when i try to access the file i cant (null-pointer)

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...


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.


回答1:


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");



回答2:


Put the JAR file in the WEB-INF/lib directory of your application.



来源:https://stackoverflow.com/questions/5487081/accessing-jar-files-from-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!