简体   繁体   English

java.io.FileNotFoundException:找不到文件configuration.yml

[英]java.io.FileNotFoundException: File configuration.yml not found

I have build a jar file of classes and configuration files. 我已经构建了一个类和配置文件的jar文件。 The configuration.yml file is located in root of the jar. configuration.yml文件位于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? 如何从命令提示符指定位于jar中的文件?

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>

It is possible to load a yaml file from a class path since Dropwizard 0.9.1 version. 从Dropwizard 0.9.1版本开始,可以从类路径加载yaml文件。

Just configure Application with ResourceConfigurationSourceProvider in the similar manner: 只需使用ResourceConfigurationSourceProvider以类似的方式配置Application

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: 对于jar的根目录中的configuration.yml

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 对于configuration.yml/com/some/configuration.yml从罐中根

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 . 请注意 - 路径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 . 看起来,这种行为将一直保持到1.1.0版本: 修复#1640:ResourceConfigurationSourceProvider - 以更复杂的方式处理资源的路径

If you use more recent Dropwizard version, you can implement your own ResourceConfigurationSourceProvider — it is pretty simple. 如果你使用更新的Dropwizard版本,你可以实现自己的ResourceConfigurationSourceProvider - 它非常简单。

configuration.yml is expected to be in the working directory ie on the filesystem, because this is how you try to read it. configuration.yml应该在工作目录中,即在文件系统上,因为这是你尝试阅读它的方式。 If you want to read it from jar file or from classpath in general you need to use getResource or getResourceAstream methods. 如果你想从jar文件或类路径中读取它,你需要使用getResourcegetResourceAstream方法。 (please see this similar question and answer ) (请看这个类似的问题答案

EIDT 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 . 如果您想从jar中的资源读取配置,那么您可能希望将应用程序配置为使用UrlConfigurationSourceProvider而不是FileConfigurationSourceProvider并将其传递给您可以从getResource获取的URL ,底层接口的open方法需要String为参数,所以你需要在getResource的结果上使用URL#toString

Try executing the command with absolute paths instead, apparently configuration.yml isn't in the run folder. 尝试使用绝对路径执行命令,显然configuration.yml不在run文件夹中。

Example when configuration.yml is in /tmp configuration.yml/tmp示例

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

I don't think it's possible to feed dropwizard a yml file within jar. 我不认为有可能在jar中输入dropwizard yml文件。 It needs to be on the file system AFAIK. 它需要在文件系统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. 如果您不想将配置详细信息公开给外部,则需要使用Configuration类通过在构造函数中设置默认值来配置它。 It gets quite ugly though since it's so nested. 它变得非常难看,因为它是如此嵌套。 I don't like how dropwizard enforces the yml dependency myself. 我不喜欢dropwizard如何自己强制执行yml依赖。

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);   } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM