从tomcat访问jar文件 [英] accessing jar-files from tomcat

查看:436
本文介绍了从tomcat访问jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他在那里,

现在被卡住了一段时间.我试图做的事情归结为:

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

  • 我已经开发了一个小库,正在以jar文件的形式进行部署.它包含一个我需要访问的文件,该文件位于"json2/json.js"中(是,doin jscript/rhino东西)
  • 我将jar文件导入到经典的Java项目中,效果很好.找到文件并读取
  • 我将SAME jar文件导入到tomcat-project中,如下所示:
  • 调用Java类的.jsp
  • 我在我的类库中调用了我的库,该库位于tomcat/lib目录中
  • 现在当我尝试访问我无法访问的文件时(空指针)
  • 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)

来源:

        InputStream in = ClassLoader.getSystemResourceAsStream("json2/json.js");

当我在tomcat中运行它时(in == null).

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]);
        }

使用经典的Java类,它仅生成我的main-class-file.在tomcat中,它向我显示了"tomcat/lib"目录的内容.

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

编辑

我忘记添加的一个细节(确实磨碎了我的装备)

one detail that i forgot to add (and that really grinds my gears):

SParser.class.getClassLoader().getParent().getResource("")

出现一个null-ptr. wtf?我没有访问特定的资源,仍然没有结果.

comes up with a null-ptr. wtf? im not accessing a particular ressource, still no result.

推荐答案

请勿使用getSystemResourceAsStream().使用getResourceAsStream().在Tomcat webapp环境中,系统类加载器不了解Tomcat/lib也不了解webapp库.

Do not use getSystemResourceAsStream(). Use getResourceAsStream(). On a Tomcat webapp environment, the system classloader has no knowledge of Tomcat/lib nor the webapp libraries.

另外,在抓取ClassLoader时,您最好抓取当前线程的上下文类加载器.

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

这篇关于从tomcat访问jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆