4

I have build a jar file of classes and configuration files. The configuration.yml file is located in root of the jar. When I try to run the application using the following command:

java -jar target/drop-wizard-0.0.1-SNAPSHOT.jar server configuration.yml

I get the exception below. How can I specify file located in jar from command prompt?

Exception in thread "main" java.io.FileNotFoundException: File configuration.yml not found <br>
    at io.dropwizard.configuration.FileConfigurationSourceProvider.open(FileConfigurationSourceProvider.java:14)<br>
    at io.dropwizard.configuration.ConfigurationFactory.build(ConfigurationFactory.java:75)<br>
    at io.dropwizard.cli.ConfiguredCommand.parseConfiguration(ConfiguredCommand.java:114)<br>
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:63)<br>
    at io.dropwizard.cli.Cli.run(Cli.java:70)<br>
    at io.dropwizard.Application.run(Application.java:72)<br>
    at com.flightnetwork.dropwizard.example.HelloWorldApplication.main(HelloWorldApplication.java:10)<br>
Charlie
  • 978
  • 1
  • 7
  • 27
Kartik Jajal
  • 732
  • 4
  • 10
  • 16
  • First thing I will do is check file extension as they are hidden hence confusing. It might be configuration.yml.txt. Best way to check it on command prompt – Mind Peace Nov 18 '14 at 15:12
  • I have check the extension of file its .yml. – Kartik Jajal Nov 18 '14 at 15:21
  • @KartikJajal If you haven't solved your issue, please take a look at my edit for how you could do to load the file from inside the jar. – A4L Nov 25 '14 at 13:19

4 Answers4

13

It is possible to load a yaml file from a class path since Dropwizard 0.9.1 version.

Just configure Application with ResourceConfigurationSourceProvider in the similar manner:

public class MicroUsersApplication extends Application<MicroUsersConfiguration> {

    @Override
    public void initialize(Bootstrap<MicroUsersConfiguration> bootstrap) {
        bootstrap.setConfigurationSourceProvider(
            new ResourceConfigurationSourceProvider());
    }

}

And for configuration.yml in the root of a jar:

java -jar target/drop-wizard-0.0.1-SNAPSHOT.jar server configuration.yml

For configuration.yml in the /com/some/configuration.yml from the jar root

java -jar target/drop-wizard-0.0.1-SNAPSHOT.jar server com/some/configuration.yml

Please, note — there is not a leading / in the path com/some/configuration.yml. Looks like, this behaviour will be kept till 1.1.0 release: Fix #1640: ResourceConfigurationSourceProvider - process a path to the resource in the more sophisticated way.

If you use more recent Dropwizard version, you can implement your own ResourceConfigurationSourceProvider — it is pretty simple.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
3

Try executing the command with absolute paths instead, apparently configuration.yml isn't in the run folder.

Example when configuration.yml is in /tmp

java -jar target/drop-wizard-0.0.1-SNAPSHOT.jar server /tmp/configuration.yml
Charlie
  • 978
  • 1
  • 7
  • 27
Erik Schmiegelow
  • 2,739
  • 1
  • 18
  • 22
3

configuration.yml is expected to be in the working directory i.e. on the filesystem, because this is how you try to read it. If you want to read it from jar file or from classpath in general you need to use getResource or getResourceAstream methods. (please see this similar question and answer)

EIDT

If you want to read the config from a resource inside your jar then you might want to configure your application to use UrlConfigurationSourceProvider instead of FileConfigurationSourceProvider and pass it the URL which you can obtain from getResource, the open method of the underlying interface expects a String as parameter, so you will need to use URL#toString on the result of getResource.

Community
  • 1
  • 1
A4L
  • 17,353
  • 6
  • 49
  • 70
  • I don't think it's possible to feed dropwizard a yml file within jar. It needs to be on the file system AFAIK. – Natan Nov 25 '14 at 10:50
  • 1
    yep it looks like one could inject it's own SourceProvider even on init(Bootstrap) method. That's very useful actually. thanks! – Natan Nov 25 '14 at 13:53
  • 1
    I have confirm this to be working. I save a copy of args and change it with the new url.tostring() and bootstrap.setConfig...(new UrlConfig...). – pompanoSlayer Mar 14 '16 at 20:01
1

I don't think it's possible to feed dropwizard a yml file within jar. It needs to be on the file system AFAIK. UPDATE: it looks like it's possible but I'm leaving this as it's still a solution.

If you don't want to expose configuration details to outside, then you need to configure it using the Configuration class, by setting the default values in the constructor. It gets quite ugly though since it's so nested. I don't like how dropwizard enforces the yml dependency myself.

An example I was using for my tests:

public class TestConfiguration extends Configuration {

  public TestConfiguration() {
    super();
    // The following is to make sure it runs with a random port. parallel tests clash otherwise
    ((HttpConnectorFactory) ((DefaultServerFactory) getServerFactory()).getApplicationConnectors().get(0)).setPort(0);
    // this is for admin port
    ((HttpConnectorFactory) ((DefaultServerFactory) getServerFactory()).getAdminConnectors().get(0)).setPort(0);   } }
Natan
  • 2,816
  • 20
  • 37