0

I have a service where i am running a jar file by passing properties file

java -jar myjar server config.yml

In order to stop passing config.yml every time through command line I have configured below code in my main method :

String configFile = args.length > 0
  ? args[0]
  : ClassLoader.getSystemClassLoader().getResource("config.yml").toString();
new myApplication.run("server", configFile);

When I run my application with

java -jar myjar

I am getting the below exception. Please help me understand what's the cause.

Exception in thread "main" java.io.FileNotFoundException: File jar:file:/Users/abc/proj/target/analytics-1.0-SNAPSHOT.jar!/config.yml not found at io.dropwizard.configuration.FileConfigurationSourceProvider.open(FileConfigurationSourceProvider.java:14)

user304611
  • 297
  • 1
  • 4
  • 14

1 Answers1

1
ClassLoader.getSystemClassLoader().getResource("config.yml").toString();

This will create string representation of resource URL (thats inside JAR). File cannot read from because it is not accessible to file system. You should rather accept InputStream as param insteed of filename.

It could be something like that

InputStream configFile = args.length > 0
  ? new FileInputStream(args[0])
  : ClassLoader.getSystemClassLoader().getResourceAsStream("config.yml");

and read resource from that stream later on.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99