How do I use a properties file in Java? - java

In the web application, which I am going to build, I need some SQL queries, which I can store in a properties file, and get them into my Java code, and then execute.
I have found many suggestions on the internet, but nothing helped me. Some of them are
this.getClass().getClassLoader().getResourceAsStream("resources/file.properties");
this.getClass().getResourceAsStream("resources/file.properties");
I have kept the properties file in resources, and in the same directory in which my Java file is present. Nothing worked. I am using Eclipse and Struts2.

Make sure that your properties files goes to WEB-INF\classes, where your struts.xml is. This folder is on the classpath. Then
this.getClass().getClassLoader().getResourceAsStream("file.properties");
should work as expected. Or of course, if you create a resources folder in classes, the aforementioned code should work too.
If by resources folder you mean src/main/resources, then it is managed by Maven, and it is copied directly to WEB-INF/classes. So you do not need to specify the resorces folder in the method.

Related

Can i write file located at classpath

I want to write a text file at classpath. want to make some changes in that file.
this is spring boot application and packaging it as a jar. So basically this text file is located in jar & I want to make changes to that file.
Don't know it is possible or not.
but please suggest to me how I can do that?
Files inside a jar can be read from (called resource files in general).
You cannot modify them directly though. For that, you need them to be outside the jar.
Possible duplicate of Updating resource files at runtime
If it is a .properties file though, there are ways to do it.
Following blog seems helpful (https://crunchify.com/java-properties-files-how-to-update-config-properties-file-in-java/)

hibernate.cfg.xml not found, but i wrote it [duplicate]

Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
My configuration file in src/. still i got this error. can some one spot my mistake.
You're using maven with the standard directory layout, so it'll compile the stuff it finds under src/main/java, and copy the stuff it finds under src/main/resources. Your config file is not in any type of source, resource, or test path. It's close, but it's not in any of them, and it needs to be for maven to do something with it.
In order for you to load it, it needs to be copied to your target directory, so it must be in one of the directories maven works with. In this case it needs to be in src/main/resources so that it's copied to the target/classes directory.
Now, I have no idea what that configuration object is, or where it came from, but the config file when it's in the right place will be a resource on the classpath, not a file on the filesystem, so you may have to change the way you're loading it. I'd try what you have first, and it that doesn't work try 'classpath:hibernate.cfg.xml' and see how you go. We'd need a lot more information to fix this for you if it doesn't work.

Export to JAR with external JavaPOS Files

I've currently finished my project, but can't get it to work when it is exported. I use JAXB to read and write XML Files and also have dependencies on other external Folders, which are needed to use a POS-Printer.
I've managed to link my external XML Save-Files with absolute paths, but not with relative paths. So that worked, although not the way i wanted. Yet, using the external class folder for the printer didn't work at all.
This means, that in my Eclipse Project Build Path i've added a class folder, which contains all of these needed files (which are not only jars, so adding them one by one wouldnt work). So exporting my project to a jar either includes all the files into the jar itself, or doesnt include them at all.
Everything works perfectly in Eclipse, but not when i export it.
My folder structure looks like this:
src
/model
/view
/control
data
/articles.xml
/...
JavaPOS <--- needed folder with all its files
/jpos.xml
/xerxers.jar
/swt-..-.dll
I've tried:
InputStreams is = getClass().getResourceAsStream(url);
absolute paths
manipulating the manifest file and/or jar structure
runnable and non runnable jars with nearly every combination of options
putting the files inside the library "by hand"
changing the build path of the project
My Question is:
How do i get my jar-file to know where these files are?
EDIT:
Do you think Maven or an Ant file could solve my problems? I don't have any experience with those.
The Problem was, that i had more than one JRE installed and that the one eclipse was using, had all the dll files, but the other ones didnt have it. So i had to add them manually, because reinstalling the drivers of the printer didnt change anything. Gotta fix that somehow, but right now it works and that is all i wanted.
Turns out i didn't even need that Folder, just needed one file out of it and the missing dlls.

Properly deploying a small Java project

I'm currently working on a small Java Project (~30 Classes, 5 external libs).
The code accesses resources in the folders src/resources and src/test_resources using getClass.getResouce("/resources/any.file").
Most of these resource files will probably never be touched by a user, but there are also some regular configuration files which are intended to be edited by the end users.
My question now is: How should I be deploying such an application?
Exporting everything into a runnable jar doesn't seem to be a good way, as I don't wanna torture my users and let them unzip the jar for editing the configuration files.
Should I export all of the internal stuff into the jar, and copy the resources directory into a Folder side by side with it? How can I access the resources then?
Thank you guys!
You could copy the resources folder. It doesn't necessarily need to be side-by-side with the jar file. The key is that you need to put the parent folder of the resources folder on the classpath.
For example, you could copy it someplace like:
c:\some\directory\resources
Then, when you execute, do something like:
java -cp c:\some\directory;c:\some\path\to\your.jar;... your.main.ClassName

How to correctly import files in a maven project structure?

I have a configuration file in xml format, that I need to load into my java code. While testing, I have imported it through it's absolute URL, but now I am about to compile and deploy the project as a jar, and that won't work anymore.
From previous experience, I think the right way to do this, is to use the ClassLoader, but I'm having some difficulties. Maybe because of my project setup, I do not know. I think I would be able to make this work, as I ahve done in the past, but I really want to make sure I do it the standard, conventional and/or correct way, so that I do not need to experiment every single time this comes up.
Here is the code I've tried to implement: http://www.mkyong.com/java/java-read-a-file-from-resources-folder/
However, this code:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
I could not use, due to needing the file in a static method of an abstract class. Therefor I switched it out with the following code:
ClassLoader classLoader = XmlConfigLoader.class.getClassLoader();
File file = new File(classLoader.getResource("configuration.xml").getFile());
The XmlConfigLoader is the containing Class. "configuration.xml" is located in the src/main/resources-folder, just as "file/test.xml" is in the example
I've run the code in debug-mode, and found that the file has the wrong path. Instead of looking in src/main/resources, it points to target/classes
Is there a setting option for default resource folder that I need to set?
Is src/main/resources the conventional place to store files like this?
Is my way of loading the ClassLoader correct in this setting?
As an additional info, this is a Maven project.
UPDATE:
My Current code actually works perfectly, apart from a single bug. The file is automatically transferred to the target/classes folder at compile time. However, whitespaces in the url are replaced by %20, and I have to manually change them back in order to make the system find the file. I am sure there is a better solution to this. Anyone?
It makes sense that the file has the path "target/classes", as this is the Class-Path in your jar's manifest file. If you want to get it to look somewhere else, edit the manifest file and append the resource classpath to it.
There are more details on how to alter your jar's classpath over at Setting classpath for a JAR
As you are using maven, the easiest thing to do is to put you resource file into the src/main/resources/META-INF directory, and maven will sort it out for you. See http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

Categories