在类路径中找不到属性文件 application.properties

Properties file application.properties not found in classpath

我有两个使用 Camel's Main 的独立模块。它们都包含:

main.setPropertyPlaceholderLocations( "classpath:application.properties" );

我也试过了:

main.setPropertyPlaceholderLocations( "application.properties" );

如果我 运行 它们在 Eclipse 中都工作正常(已将 <project>/target 添加到 运行 配置' 类路径).

如果我从 cmd 行 运行 它们:

...\target> java -jar <module>.jar

with target contains both application.properties and <module>.jar, 一个工作正常。其他结果:

Exception in thread "main" org.apache.camel.RuntimeCamelException: java.io.FileNotFoundException:
Properties file application.properties not found in classpath

我看过 但我的 application.properties src/main/resources 中的 ,并在 [=] 期间复制到 target 25=].

更新 1

澄清一下。在我的项目 POM 中,我使用:

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>application.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>application.properties</include>
            </includes>
            <targetPath>..</targetPath>  <!-- relative to target/classes -->
        </resource>
    <resources>

防止 application.properties 驻留在 <module>.jar.

更新 2

90   [main] INFO  <module>  - Starting Camel...
182  [main] INFO  org.apache.camel.impl.DefaultCamelContext  - Apache Camel 2.22.0 (CamelContext: camel-1) is shutting down
195  [main] INFO  org.apache.camel.impl.DefaultCamelContext  - Apache Camel 2.22.0 (CamelContext: camel-1) uptime
198  [main] INFO  org.apache.camel.impl.DefaultCamelContext  - Apache Camel 2.22.0 (CamelContext: camel-1) is shutdown in 0.017 seconds
Exception in thread "main" org.apache.camel.RuntimeCamelException: java.io.FileNotFoundException: Properties file application.properties not found in classpath
        at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1830)
        at org.apache.camel.model.RouteDefinitionHelper.initRouteInputs(RouteDefinitionHelper.java:382)
        at org.apache.camel.model.RouteDefinitionHelper.prepareRouteImp(RouteDefinitionHelper.java:298)
        at org.apache.camel.model.RouteDefinitionHelper.prepareRoute(RouteDefinitionHelper.java:270)
        at org.apache.camel.model.RoutesDefinition.route(RoutesDefinition.java:205)
        at org.apache.camel.model.RoutesDefinition.from(RoutesDefinition.java:158)
        at org.apache.camel.builder.RouteBuilder.from(RouteBuilder.java:169)
        at <module>Route.configure(<module>Route.java:24)
        at org.apache.camel.builder.RouteBuilder.checkInitialized(RouteBuilder.java:462)
        at org.apache.camel.builder.RouteBuilder.configureRoutes(RouteBuilder.java:402)
        at org.apache.camel.builder.RouteBuilder.addRoutesToCamelContext(RouteBuilder.java:383)
        at org.apache.camel.impl.DefaultCamelContext.call(DefaultCamelContext.java:1029)
        at org.apache.camel.impl.DefaultCamelContext.call(DefaultCamelContext.java:1026)
        at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3272)
        at org.apache.camel.impl.DefaultCamelContext.addRoutes(DefaultCamelContext.java:1026)
        at org.apache.camel.main.MainSupport.postProcessCamelContext(MainSupport.java:612)
        at org.apache.camel.main.MainSupport.postProcessContext(MainSupport.java:550)
        at org.apache.camel.main.Main.doStart(Main.java:136)
        at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
        at org.apache.camel.main.MainSupport.run(MainSupport.java:170)
        at <module>.run(<module>.java:44)
        at <module>.main(<module>.java:20)
Caused by: java.io.FileNotFoundException: Properties file application.properties not found in classpath
        at org.apache.camel.component.properties.DefaultPropertiesResolver.loadPropertiesFromClasspath(DefaultPropertiesResolver.java:112)
        at org.apache.camel.component.properties.DefaultPropertiesResolver.resolveProperties(DefaultPropertiesResolver.java:69)
        at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:207)
        at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:178)
        at org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:2552)
        at org.apache.camel.model.ProcessorDefinitionHelper.resolvePropertyPlaceholders(ProcessorDefinitionHelper.java:735)
        at org.apache.camel.model.RouteDefinitionHelper.initRouteInputs(RouteDefinitionHelper.java:380)
        ... 20 more
204  [Camel Thread #0 - CamelHangupInterceptor] INFO  org.apache.camel.main.MainSupport$HangupInterceptor  - Received hang up - stopping the main instance.

现在我知道为什么一个模块 运行 而另一个没有了。显然,骆驼在 setPropertyPlaceholderLocations() 之后执行延迟加载,并且由于我没有使用 application.properties 的任何 属性 (还)它甚至没有尝试读取文件。

现在我正在使用它们,之前工作的模块也失败了。 (错误导致真相的罕见情况之一。;)

解决方案是使用:

String jarPath = new File( 
    this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath() )
    .getParent();
main.setPropertyPlaceholderLocations("file:" + jarPath + "/application.properties" )

正如 Claus 几个小时前评论的那样,而不是:

main.setPropertyPlaceholderLocations("classpath:...")