简体   繁体   中英

Get path of the resource folder inside spring boot jar

i am trying convert java application project to spring boot, and i am trying to deploy this spring boot project jar in gcp by using below cmd

gcloud app deploy <projectname>.jar

inside my application i am using one of the apache core dependency which needs 2 property files and it is placed under project directory

--PROJECTNAME<root>
----/.idea
----/config/bin/config.properties
----/src
------/main
------/...
------/...

and i can set only the path for the property files remaining things will be handled by the apache dependency class files. it is working fine in IDE and as a jar if those properties are available in jar location itself.

so when i deploy this project in gcp using above cmd i can see this project is copied under /workspace/ directory in gcp app engine. so either i need to copy this property files under /workspace/ directory in gcp or i need to access the property file inside the jar but i can only pass the path(String). in apache class file below code is used to read the property files.

BufferedReader reader = Files.newBufferedReader((new File( <path which i can pass> + "/bin/config.properties")).toPath(), Charset.defaultCharset());
and 
FileInputStream fis = new FileInputStream(<path which i can pass> + "/bin/config.properties");

I guess you can put your config files inside src/main/resources, as I can see your config is outside from src dir, so then in the class you need the file you can do getClass().getClassLoader().getResource("config/bin/config.properties") to get the file as a URL and open a InputStream for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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