简体   繁体   English

是否可以将资源文件放入JAR文件中?

[英]Is it possible to put resources file into JAR files?

I have web application that runs on tomcat. 我有在tomcat上运行的Web应用程序。 I would like to put some resources in 我想把一些资源放进去

resources.jar

eg 例如

configuration.xml

But when I try: 但是当我尝试:

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

it didn't found. 它没有找到。

May be I should configure path in another way? 可能是我应该以另一种方式配置路径?

When running in a web application, you should use the ContextClassLoader : 在Web应用程序中运行时,您应该使用ContextClassLoader

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

or 要么

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

The first one will return a URL , the second one an InputStream . 第一个将返回一个URL ,第二个将返回一个InputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM