java.io.FileNotFoundException:找不到文件configuration.yml [英] java.io.FileNotFoundException: File configuration.yml not found
问题描述
我已经构建了一个类和配置文件的jar文件。 configuration.yml文件位于jar的根目录中。当我尝试使用以下命令运行应用程序时:
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
我在下面得到了例外。如何从命令提示符指定位于jar中的文件?
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>
推荐答案
configuration.yml
应该在工作目录中,即在文件系统上,因为这是你尝试阅读它的方式。如果你想从jar文件或类路径中读取它,你需要使用 getResource 或 getResourceAstream 方法。 (请参阅此类似的问题和回答)
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
如果您想阅读然后您可能希望将应用程序配置为使用 UrlConfigurationSourceProvider 而不是 FileConfigurationSourceProvider 并传递 URL ,您可以从 getResource , open 方法需要 String
作为参数,因此您需要使用< a href =https://docs.oracle.com/javase/8/docs/api/java/net/URL.html#toString-- =nofollow noreferrer> URL#toString getResource的结果。
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.
这篇关于java.io.FileNotFoundException:找不到文件configuration.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!