[英]A class from external jar throws exception while loading Resource
Hi I am working on a Maven project having dependency on a external jar which has a class ConfigLoader
having following loader()
method. 嗨,我正在一个依赖于外部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
}
and the directory structure is like this - 目录结构是这样的-
src
|...main
|.......java
|.......resources
|................dev
|................prod
both dev and prod have a file named runtimeConfiguration.xml and the code which uses this class is 开发人员和生产人员都有一个名为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();
}
}
The error I get is 我得到的错误是
could not find: runtimeConfiguration.xml
and the exception is thrown at the getResource() line in the class from jar.
I have tried adding the dev folder to classpath but still the same error. 我尝试将dev文件夹添加到classpath中,但仍然是相同的错误。 I want to run this code from linux terminal, and the command I am giving from trunk directory (where all my exernal jars and resource folder sits after maven build) is -
我想从linux终端运行此代码,而我从主干目录(我所有的外部jar和资源文件夹位于maven构建之后的位置)给出的命令是-
java -cp /resources/dev/*:configuration-loader.jar
I am using intelliJ 2017.2 and also tried to add the resources/dev folder as module dependency, but I keep on getting the same error. 我正在使用intelliJ 2017.2,并且还尝试将resources / dev文件夹添加为模块依赖项,但是我一直遇到相同的错误。 The resources folder is added as a library via project structure settings.
资源文件夹通过项目结构设置添加为库。 I tried to search a lot but have not found any question with this issue.
我试图进行大量搜索,但未发现与此问题有关的任何问题。 Kindly help me out as I am new to this environment based development.
由于我是这个基于环境的开发的新手,请帮助我。 Thanks!
谢谢!
ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
will try to get runtimeConfiguration.xml
from the same package a the ConfigLoader is defined and not from the root of classpath. 会尝试从定义
runtimeConfiguration.xml
的同一包中而不是从classpath的根目录中获取runtimeConfiguration.xml
。 Try appending /
to runtimeConfiguration.xml
. 尝试将
/
附加到runtimeConfiguration.xml
。
This should work ConfigLoader.getClass().getResource("/runtimeConfiguration.xml");
这应该可以工作
ConfigLoader.getClass().getResource("/runtimeConfiguration.xml");
or ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml");
或
ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml");
depending how you are adding resources to your classpath. 取决于您如何向类路径添加资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.