Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以将资源文件放入JAR文件中?_Java_Web Applications_Resources_Jar_Classpath - Fatal编程技术网

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

Java 是否可以将资源文件放入JAR文件中?,java,web-applications,resources,jar,classpath,Java,Web Applications,Resources,Jar,Classpath,我有一个在tomcat上运行的web应用程序。我想投入一些资源 resources.jar e、 g 但当我尝试时: new File("/conf/configuration.xml") 没有找到 可能我应该以另一种方式配置路径?在web应用程序中运行时,您应该使用ContextClassLoader: Thread.currentThread().getContextClassLoader().getResource("/conf/configuration.xml"); 或 第一

我有一个在tomcat上运行的web应用程序。我想投入一些资源

resources.jar
e、 g

但当我尝试时:

  new File("/conf/configuration.xml")
没有找到


可能我应该以另一种方式配置路径?

在web应用程序中运行时,您应该使用
ContextClassLoader

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


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

您是否在应用程序的类路径中配置了jar可能与您在类路径中的意思重复?此jar位于webapps/mywebapplication/WEB-INF/lib中。我认为这个文件夹中的所有JAR都在类路径中。请看一下我在这里链接的问题,它并没有准确描述您的问题,但也会对您有用(使用类加载器)。
Thread.currentThread().getContextClassLoader().getResource("/conf/configuration.xml");
Thread.currentThread().getContextClassLoader().getResourceAsStream("/conf/configuration.xml");