[英]A class from external jar throws exception while loading Resource
嗨,我正在一个依赖于外部jar的Maven项目中,该jar具有一个具有以下loader()
方法的ConfigLoader
类。
public class ConfigLoader {
public void initialize() {
loader();
}
private static void loader() {
URL configURL = ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
//some other method calls to which configURL is an argument.
}
//other methods of ConfigLoader class
}
目录结构是这样的-
src
|...main
|.......java
|.......resources
|................dev
|................prod
开发人员和生产人员都有一个名为runtimeConfiguration.xml的文件,使用此类的代码为
public class Application {
private Application application;
public static void main(String []args){
application = new Application();
application.invokeConfigLoader();
//additional code
}
private void invokeConfigLoader() {
configLoader.initialize();
}
}
我得到的错误是
could not find: runtimeConfiguration.xml
and the exception is thrown at the getResource() line in the class from jar.
我尝试将dev文件夹添加到classpath中,但仍然是相同的错误。 我想从linux终端运行此代码,而我从主干目录(我所有的外部jar和资源文件夹位于maven构建之后的位置)给出的命令是-
java -cp /resources/dev/*:configuration-loader.jar
我正在使用intelliJ 2017.2,并且还尝试将resources / dev文件夹添加为模块依赖项,但是我一直遇到相同的错误。 资源文件夹通过项目结构设置添加为库。 我试图进行大量搜索,但未发现与此问题有关的任何问题。 由于我是这个基于环境的开发的新手,请帮助我。 谢谢!
ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
会尝试从定义runtimeConfiguration.xml
的同一包中而不是从classpath的根目录中获取runtimeConfiguration.xml
。 尝试将/
附加到runtimeConfiguration.xml
。
这应该可以工作ConfigLoader.getClass().getResource("/runtimeConfiguration.xml");
或ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml");
取决于您如何向类路径添加资源。
有关更多信息,请参见javadoc 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.