1

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.
Community
  • 1
  • 1
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • Can you double check that the file are inside and in the root of the module JAR file. – Claus Ibsen Aug 14 '18 at 11:22
  • @ClausIbsen Assuming you refer to `application.properties` it's not, of course, in none of them. I don't want config files to reside inside a JAR, forcing me – or my users – to unzip/edit/rezip every time a change to the runtime config needs to be applied. – Gerold Broser Aug 14 '18 at 11:39
  • Ah okay, if you want to load that file outside the classpath, then use `file:` as prefix and point to the file where its located. – Claus Ibsen Aug 14 '18 at 15:09
  • @ClausIbsen The file is outside the JAR, yes, but is it really outside the classpath, the default of which is `.`, i.e. [the current directory](https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html#A1100592)? And note that it works with the classpath inside Eclipse, with `getResourceAsStream()` and in one module on the cmd line. The real question is: Why does it work in one module but not in the other though both use the same method `setPropertyPlaceholderLocations()`? – Gerold Broser Aug 14 '18 at 15:29
  • Do you have full stacktrace? – Gábor Lipták Aug 14 '18 at 15:43
  • https://stackoverflow.com/a/18413058/337621 – Gábor Lipták Aug 14 '18 at 15:48
  • If you use -jar, then the current directory is not on the classpath I think. – Gábor Lipták Aug 14 '18 at 15:48
  • @GáborLipták Yes. See **UPDATE 3**. – Gerold Broser Aug 14 '18 at 15:54
  • @GáborLipták OK, "_... other user class path settings are ignored._", but the default `.` is not a user setting via `-cp`. And note also: it works in one module. – Gerold Broser Aug 14 '18 at 15:56
  • Just start the thing in debug mode, add a breakpoint on FileNotFoundException and check the failure in your debugger. It never lies ;) – Gábor Lipták Aug 14 '18 at 16:03
  • @ClausIbsen You were right. See my answer. – Gerold Broser Aug 15 '18 at 01:38

1 Answers1

1

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:...")
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107