java - how to load a properties file from the META-INF directory? -


is correct properties files used in execution running jar best belong in meta-inf? notion being fit standard directory layout specified maven.

assuming structure desired, how properties file loaded meta-inf?

thufir@doge:~/netbeansprojects/json_parser$  thufir@doge:~/netbeansprojects/json_parser$ java -jar build/libs/json_parser-all.jaraug 18, 2017 6:44:24 net.bounceme.doge.json.main main info: starting aug 18, 2017 6:44:24 net.bounceme.doge.json.main run info: aug 18, 2017 6:44:24 net.bounceme.doge.json.propertiesreader trygetprops info: properties aug 18, 2017 6:44:24 net.bounceme.doge.json.propertiesreader getprops info: properties exception in thread "main" java.lang.nullpointerexception     @ java.util.properties$linereader.readline(properties.java:434)     @ java.util.properties.load0(properties.java:353)     @ java.util.properties.load(properties.java:341)     @ net.bounceme.doge.json.propertiesreader.getprops(propertiesreader.java:26)     @ net.bounceme.doge.json.propertiesreader.trygetprops(propertiesreader.java:16)     @ net.bounceme.doge.json.main.run(main.java:18)     @ net.bounceme.doge.json.main.main(main.java:12) thufir@doge:~/netbeansprojects/json_parser$  thufir@doge:~/netbeansprojects/json_parser$ jar tf build/libs/json_parser-all.jarmeta-inf/ meta-inf/manifest.mf meta-inf/maven/ meta-inf/maven/javax.json/ meta-inf/maven/javax.json/javax.json-api/ meta-inf/maven/javax.json/javax.json-api/pom.properties meta-inf/maven/javax.json/javax.json-api/pom.xml javax/ javax/json/ javax/json/json.class javax/json/jsonarray.class javax/json/jsonarraybuilder.class javax/json/jsonbuilderfactory.class javax/json/jsonexception.class javax/json/jsonmergepatch.class javax/json/jsonnumber.class javax/json/jsonobject.class javax/json/jsonobjectbuilder.class javax/json/jsonpatch$operation.class javax/json/jsonpatch.class javax/json/jsonpatchbuilder.class javax/json/jsonpointer.class javax/json/jsonreader.class javax/json/jsonreaderfactory.class javax/json/jsonstring.class javax/json/jsonstructure.class javax/json/jsonvalue$valuetype.class javax/json/jsonvalue.class javax/json/jsonvalueimpl.class javax/json/jsonwriter.class javax/json/jsonwriterfactory.class javax/json/spi/ javax/json/spi/jsonprovider.class javax/json/stream/ javax/json/stream/jsoncollectors.class javax/json/stream/jsongenerationexception.class javax/json/stream/jsongenerator.class javax/json/stream/jsongeneratorfactory.class javax/json/stream/jsonlocation.class javax/json/stream/jsonparser$event.class javax/json/stream/jsonparser.class javax/json/stream/jsonparserfactory.class javax/json/stream/jsonparsingexception.class module-info.class net/ net/bounceme/ net/bounceme/doge/ net/bounceme/doge/json/ net/bounceme/doge/json/main.class net/bounceme/doge/json/marshaller.class net/bounceme/doge/json/propertiesreader.class net/bounceme/doge/json/jsonreader.class json.json properties.properties meta-inf/properties.properties thufir@doge:~/netbeansprojects/json_parser$  thufir@doge:~/netbeansprojects/json_parser$ jar tf build/libs/json_parser-all.jar | grep properties.properties properties.properties meta-inf/properties.properties thufir@doge:~/netbeansprojects/json_parser$  

and relevant code:

package net.bounceme.doge.json;  import java.io.ioexception; import java.util.properties; import java.util.logging.level; import java.util.logging.logger;  public class propertiesreader {      private static final logger log = logger.getlogger(propertiesreader.class.getname());      public properties trygetprops(string propertiesfilename) {         log.info(propertiesfilename);         properties properties = new properties();         try {             properties = getprops(propertiesfilename);         } catch (ioexception ex) {             logger.getlogger(propertiesreader.class.getname()).log(level.severe, null, ex);         }         return properties;     }      private properties getprops(string propertiesfilename) throws ioexception {         log.info(propertiesfilename);         properties properties = new properties();         properties.load(propertiesreader.class.getresourceasstream("/meta-inf/" + propertiesfilename));         log.info(properties.tostring());         return properties;     } } 

this in relation using init gradle , based on general information.

i tried removing leading / suggested -- same result above.

you're not obliged store properties files in meta-inf, indeed it's not common (in experience @ least). in support of contention java jar specification doesn't include properties files amongst expected contents of meta-inf.

the code below shows several approach loading properties jar, whether in meta-inf or not.

given jar file content:

jar -tvf properties.jar    0 fri aug 18 15:35:52 ist 2017 meta-inf/   68 fri aug 18 15:35:52 ist 2017 meta-inf/manifest.mf    4 fri aug 18 15:35:32 ist 2017 meta-inf/bprops.properties    4 fri aug 18 15:35:18 ist 2017 aprops.properties 

you can load aprops.properties , bprops.properties so:

@test public void canwriteandread() throws ioexception {     classloader systemclassloader = classloader.getsystemclassloader();     // works     loadproperties(systemclassloader.getresource("aprops.properties"));     // works     loadproperties(systemclassloader.getresource("meta-inf/bprops.properties"));     // not work     loadproperties(systemclassloader.getresource("/aprops.properties"));     // not work     loadproperties(systemclassloader.getresource("/meta-inf/bprops.properties"));      classloader classloader = getclass().getclassloader();     // works     loadproperties(classloader.getresource("aprops.properties"));     // works     loadproperties(classloader.getresource("meta-inf/bprops.properties"));     // not work     loadproperties(classloader.getresource("/aprops.properties"));     // not work     loadproperties(classloader.getresource("/meta-inf/bprops.properties"));      // works     loadproperties(getclass().getresourceasstream("/aprops.properties"));     // works     loadproperties(getclass().getresourceasstream("/meta-inf/bprops.properties"));     // not work     loadproperties(getclass().getresourceasstream("aprops.properties"));     // not work     loadproperties(getclass().getresourceasstream("meta-inf/bprops.properties")); }  private void loadproperties(inputstream incoming) throws ioexception {     if (incoming != null) {         properties properties = new properties();         properties.load(incoming);         (string s : properties.stringpropertynames()) {             system.out.println(s);         }     } }  private void loadproperties(url incoming) throws ioexception {     if (incoming != null) {         properties properties = new properties();         properties.load(incoming.openstream());         (string s : properties.stringpropertynames()) {             system.out.println(s);         }     } } 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -