加载资源时,来自外部罐子的类引发异常



嗨,我正在从事一个Maven项目,该项目依赖于具有以下loader()方法的外部JAR。

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 的文件使用此类的代码为

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终端运行此代码,以及我从Trunk Directory提供的命令(MAVEN构建后我所有的Exernal Jars和Resource文件夹均位于其中)是 -

java -cp /resources/dev/*:configuration-loader.jar 

我正在使用Intellij 2017.2,并且还尝试将资源/DEV文件夹作为模块依赖关系,但我继续遇到相同的错误。通过项目结构设置添加资源文件夹作为库。我试图搜索很多,但在这个问题上没有发现任何问题。请帮助我,因为我是基于环境的开发的新手。谢谢!

ConfigLoader.getClass().getResource("runtimeConfiguration.xml");将尝试从同一软件包中获取 runtimeConfiguration.xml configloader是定义的,而不是从classpath的根中获取CC_4。尝试将/附加到runtimeConfiguration.xml

这应该起作用ConfigLoader.getClass().getResource("/runtimeConfiguration.xml");ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml");,具体取决于您如何向类Path添加资源。

请参阅Javadoc。

最新更新