I'm currently writing a SpringBoot app implementing the CommandLineRunner.
I managed to successfully load configuration from a yaml file which can be on the on the resources folder or a file on the same path as the jar file.
I've also managed to modify the configuration in runtime however I need to persist these changes to the yaml file on the file system.
Does anybody know a "Spring way" to get it done ? or alternatively an easy way...
Thanks in advance.
You can use the FileOutputStream to write the changed file to the filesystem.
Related
I am developing a spring boot application which would be used as a non-executable jar by some other user application. The spring boot application(jar) reads the xsd file present in resources folder. When we run it as a standalone application it executes fine.
Code to read the xsd file from resources folder -
File xsdValue = new ClassPathResource("xsd/" + xsdFileName + ".xsd").getFile();
But the problem is, When user application calls the jar then it tries to find the xsd in its own resources folder rather than that of jar's resources folder.
Please advise !!
Any help is much appreciated.
Resources in jar-files can not be gotten as File class, use getResourceAsStream method of ClassLoader class instead.
A csv file in /resources in my java maven application needs to be read as well as written.
Accessing the file using getClass().getClassLoader().getResource(/myFile.csv) in ResourceLoader class, i am able to write and read the file but the issue is the data is not appearing in the application after the restart of my system or after some time duration.
I tried using getResourceAsStream() by getting input stream ,but could write the file with it.
What might be the reason to it? Is there anything am doing wrong?
Im trying to use the file application.yml, application-dev.yml and application-prod.yml that jhipster has in the proyect, but i dont want to use this file from the proyect i want to use those file from a local path. for example C://path
Please help me
Thanks
This is purely a spring-boot mechanism not specific to JHipster.
Look at spring-boot doc for all possibilities.
In your case, you could use:
java -jar myproject.jar --spring.config.location=file:///C:/path/application-dev.yml
but the simplest is to put your yaml file in same directory as your jar file, this is what is often used in production.
I have implemented log4j in my web application project. Project is done using net beans,using tomcat 7.0.41. At first,I created log4j.property file and placed under web page->Web-INF->classes->log4j.properties in net beans and it asks me to locate the file in my project,so I manually located that file to implement log4j in my application. After that I changed the place of the log4j.properties file to myproject->build->web->WEB_INF->classes->log4j.properties in location of my project saved, now its working fine, it did not ask me to manually locate the property file, It takes automatically when my class files executed. Now my problem is that once I committed the project and again checkout the project on some day, property file does not appear and it again ask for property file. So where can I create the log4j property file in my project so that my team mates can utilize it when they checkout project in their system.
Normally you put log4j.properties to src/main/resources/ and it will be copied to the right place by the build process.
I never use net beans, but I think put log4j.properties under Classpath will work.
Not sure how Net Beans handels this, but i think that the "build" directory is where the "compiled" project is put to.
So i would not recommend to put any files there which should be versioned because mostly those directories are ignored for versioning ( see .gitignore files for example when using git).
Resources like property files should be within the sources and your IDE should copy them to the correct place when building the project.
I'm currently setting up our project to deploy to users via java webstart instead of the current set up in which users run a .bat file from a shared network drive. When the application is run, it is passed an properties file argument which contains information such as database credentials which allows for the switching between different environments etc.
I would like to know if there is a way to specify this in the JNLP file and have webstart pull down the properties file from the webserver. I've spent a decent amount of time investigating this online and the only thing I could come up with was to simply specify the filename as an argument like so:
<application-desc main-class="Main">
<argument>example.properties</argument>
</application-desc>
and then include a separate link which let users download the properties file from the server. The issue with this is that if the JNLP file and the properties file arent downloaded to the same directory (which seems to be the default behaviour in IE) then the whole application falls over. Is there a way of bundling my properties file along with the other resources in the JNLP file or am I going about this in a completely incorrect fashion? Any help would be greatly appreciated!
You can set the properties in the .jnlp file itself instead of having a separate properties file.
http://www.coderanch.com/t/200871/JNLP-Web-Start/java/Properties-files-JNLP
Other solutions are:
Put the properties file inside the main jar or in a separate jar and add it as a resource.
Put location of the properties file as a property or as a main argument and download it from the program itself.
Looks like the jnlp is an XML file which includes a list of jar files to be placed in your classpath.
If you include a properties file in one of your jars then you can read it using this.getClass().getClassLoader().getResourceAsStream("mypropsname.properties").
Do all your users need the same properties file?