Search code examples
apache-camelprogram-entry-pointproperties-file

Properties file application.properties not found in classpath


I have two standalone modules that use Camel's Main. Both of them contain:

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

and I also tried:

main.setPropertyPlaceholderLocations( "application.properties" );

If I run them from within Eclipse both work fine (having added <project>/target to the Run Configurations' Classpath).

If I run them from the cmd line:

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

with target containing both application.properties and <module>.jar, one works fine. The other results in:

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

I've seen Q: Camel properties file not found in class path but my application.properties is in src/main/resources, and copied to target during mvn package.

UPDATE 1

For clarification. In my projects' POMs I use:

    <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>

to prevent application.properties residing inside <module>.jar.

UPDATE 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$1.call(DefaultCamelContext.java:1029)
        at org.apache.camel.impl.DefaultCamelContext$1.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.

Solution

  • Now I know why one module ran while the other didn't. Apparently Camel performs lazy loading after setPropertyPlaceholderLocations() and since I didn't use any property of application.properties (yet) it didn't even try to read the file.

    Now that I'm using them the previously working module fails as well. (One of the rare cases where an error leads to truth. ;)

    The solution is to use:

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

    as commented by Claus a few hours ago, rather than:

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