来自外部 jar 的 class 在加载资源时抛出异常
A class from external jar throws exception while loading Resource
您好,我正在开发一个依赖于外部 jar 的 Maven 项目,该项目具有 class ConfigLoader
和以下 loader()
方法。
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
dev 和 prod 都有一个名为 runtimeConfiguration.xml 的文件
使用此 class 的代码是
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 终端 运行 这段代码,我从 t运行k 目录(我所有的外部 jars 和资源文件夹都位于 Maven 构建之后)给出的命令是 -
java -cp /resources/dev/*:configuration-loader.jar
我正在使用 intelliJ 2017.2 并且还尝试将 resources/dev 文件夹添加为模块依赖项,但我不断收到相同的错误。通过项目结构设置将资源文件夹添加为库。我尝试了很多搜索,但没有发现与此问题有关的任何问题。请帮助我,因为我是这种基于环境的开发的新手。
谢谢!
ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
将尝试从定义了 ConfigLoader 的同一个包中获取 runtimeConfiguration.xml
,而不是从类路径的根目录中获取。尝试将 /
附加到 runtimeConfiguration.xml
.
这应该 ConfigLoader.getClass().getResource("/runtimeConfiguration.xml");
或 ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml");
有效,具体取决于您将资源添加到类路径的方式。
有关更多信息,请参阅 javadoc。
您好,我正在开发一个依赖于外部 jar 的 Maven 项目,该项目具有 class ConfigLoader
和以下 loader()
方法。
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
dev 和 prod 都有一个名为 runtimeConfiguration.xml 的文件 使用此 class 的代码是
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 终端 运行 这段代码,我从 t运行k 目录(我所有的外部 jars 和资源文件夹都位于 Maven 构建之后)给出的命令是 -
java -cp /resources/dev/*:configuration-loader.jar
我正在使用 intelliJ 2017.2 并且还尝试将 resources/dev 文件夹添加为模块依赖项,但我不断收到相同的错误。通过项目结构设置将资源文件夹添加为库。我尝试了很多搜索,但没有发现与此问题有关的任何问题。请帮助我,因为我是这种基于环境的开发的新手。 谢谢!
ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
将尝试从定义了 ConfigLoader 的同一个包中获取 runtimeConfiguration.xml
,而不是从类路径的根目录中获取。尝试将 /
附加到 runtimeConfiguration.xml
.
这应该 ConfigLoader.getClass().getResource("/runtimeConfiguration.xml");
或 ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml");
有效,具体取决于您将资源添加到类路径的方式。
有关更多信息,请参阅 javadoc。