【问题标题】:Is it possible to put resources file into JAR files?是否可以将资源文件放入 JAR 文件中?
【发布时间】:2012-03-20 17:50:01
【问题描述】:

我有在 tomcat 上运行的 Web 应用程序。我想把一些资源放在

resources.jar

例如

configuration.xml

但是当我尝试时:

  new File("/conf/configuration.xml")

没找到。

我应该以其他方式配置路径吗?

【问题讨论】:

  • 你是否在应用的classpath中配置了jar
  • load file within a jar 的可能重复项
  • 你在类路径中的意思是什么?这个 jar 在 webapps/mywebapplication/WEB-INF/lib 中。我认为这个文件夹中的所有 jar 都在类路径中。
  • 看看我在此处链接的问题,它并不能准确描述您的问题,但也适用于您(使用类加载器)。

标签: java web-applications resources jar classpath


【解决方案1】:

在网络应用程序中运行时,应使用ContextClassLoader

Thread.currentThread().getContextClassLoader().getResource("/conf/configuration.xml");

Thread.currentThread().getContextClassLoader().getResourceAsStream("/conf/configuration.xml");

第一个将返回URL,第二个将返回InputStream

【讨论】:

    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 2020-05-27
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    相关资源
    最近更新 更多