How to get correct path of configuration file? - java

I created a Java project that contains three configuration file
log4j.XML
QueueConfig.xml
rabbitmq.properties
I put these three files into a resource folder.
Then I created a jar file of my project. This jar file is added to another project. It isn't able to find the correct location of the configuration files.
My file structure:
Thanks

you can use it like.
ClassFromWhichYouAreACcessingTheseFiles.class.getResources("resources/log4j.properties");
than if you add this jar to another project you will be able to access it.

If your config files end up in the WEB-INF/classes folder
ClassFromWhichYouAreACcessingTheseFiles.class.getClassLoader().getResources("log4j.properties");
otherwise it include the "package-path" from the ClassFromWhichYouAreACcessingTheseFiles

Related

Java Spring: How to specify resource path's correctly in application.properties?

I am just working on a Spring project where the former developer used some full pathes for resource access in application.properties like:
java.resource=/full/path/src/main/resources/somefile
webapp.resource=/full/path/src/main/webapp/resources/somefile
I think it's no good practice to link into the sources folder, right?
As I can see files from src/main/resources/ are extracted into Tomcat's webapps/projekt-1.0.0/WEB-INF/classes directory and files from src/main/webapp/resources/ are extracted into webapps/projekt-1.0.0/resources.
How to do this the right way so that I don't need separate .properties Files for development/staging etc?
From your directory structure may I assume you're using Maven? If that true, then your statements was correct. Maven structure directory automatically scan src/main/resources path as the build path, so you only need to put your properties file in there and call the file on your applicationContext.xml.
<!-- read value from .properties file -->
<context:property-placeholder location="classpath:<your_file_name>.properties"/>

How to fix this java.io.FileNotFoundException?

I'm trying to load a .csv file in a program but for some reason, it's unable to find the file. Where should I place the file?
Console
It looks like the file is in the src directory... which almost certainly isn't the working directory you're running in.
Options:
Specify an absolute filename
Copy the file to your working directory
Change the working directory to src
Specify a relative filename, having worked out where the working directory is
Include it as a resource instead, and load it using Class.getResourceAsStream
The file is located in the src directory so in order to access it you should use
src/Elevator.csv
As long as files are located inside your project folder you can access them using relative paths.
For example if a file is located under the Elevator folder then you access the file by using only its filename.
Elevator.csv
A good principle when using additional files in your project is creating separate folders from the ones that the source files are located. So you could create a folder resources under the project folder and place your file there. You can access then the file by using
resources/Elevator.csv
the path which it is trying to read is surely not exact as the path in which that file is actually present.Try printing absolute path of that file and compare it with actual path of your file.
I tried with all the above mention solution, but it didn't work..
but i went to my project folder and delete the target and tried to compile the project again. it then worked successfully

cannot locate properties file in java path

My code cannot locate the .properties file where i have stored login information.
I have put the file in the src folder to make sure it compiles, and it does correctly.
below is the current location of the file and how i am trying to access it.
I have tried various different paths but no luck.
Change your code;
ResourceBundle bundle = ResourceBundle.getBundle("Selenium/readme");
to
ResourceBundle bundle = ResourceBundle.getBundle("readme");
You don't compile a .properties file, you use it as is.
If you use a FileInputStream it will use the working directory which is set in your Run configuration (most likely the top directory)
But you are loading it as a resource which means it must be in your class path. The simplest thing to do is to create a sub-directory for your configuration and add this to your programs class path.
Read properties file like:
ResourceBundle.getBundle("src/properties/readme.properties"); //Or simply "properties/readme.properties"
Put readme.properties under src/properties directory.
you can use this.getClass().getResourceAsStream("readme.properties");
for more information read:
I see that the .properties file is not inside the src folder. Also check the build path of your project.It will show you the src folders and the output folders location. Once you build the project using eclipse build project option, make sure your properties file is now available in the output folder.

access files and folders in executable jars

I am facing the following problem:
I use Eclipse for my Java projects and there I have the following structure:
--> src
--> package1
....
--> package2
...
-->dataFolder
--> Folder1
--> file1.xml
--> Folder2
file2.xml
In my java project I parse all files in the dataFolder directory. This is no problem because I can just use the root directory of the eclipse project, like this:
File dataStoreFolder = new File("dataFolder");
Some parsing......
But when I export my project as an executable jar file, I can not access these files anymore. Of course thats because they are not in the same folder where I put my exec.jar file. But I do not want to copy the whole folder with the data to my exec.jar file. Can I put it somehow inside my jar file? (I did it by adding the dataFolder to the Java Build Path but it still does not work as it does in Eclipse)
Can someone help me please?
When you're using a jar file, those files don't exist as files any more.
You can absolutely include the data in your jar file, but then you'll need to change the code which accesses it, too. Instead of creating a File object, you'll need something like:
InputStream input = Foo.class.getResourceAsStream("/dataFolder/Folder1/file.xml");
So you have two separable tasks:
Work out how to include the data folder within your jar file
Change your code to access the data appropriately
Yes, you can put them in your jar file. Just make dataFolder a source folder under Eclipse, and it will copy the non-Java files to the target directory, and add them to the jar file.
Once in the jar file, you would get file1.xml like this:
InputStream in = SomeClass.class.getResourceAsStream("/Folder1/file1.xml");

Adding a non-jar file to the Java class path

I'm trying to make a .txt file available to my application via the class path. In my startup script--which is co-located in the same folder as the .txt file--I've set the following:
set CLASSPATH=%CLASSPATH%;%CD%\sample.txt
java -classpath %CD%\sample.txt
In my application, I've tried the following:
getClass().getResource("sample.txt")
getClass().getResource("/sample.txt")
getClass().getResource("classpath:sample.txt")
None of the above work. Any help would be appreciated.
You must pack you txt file inside jar or place it in directory included in classpath.
You should add to your classpath the directory containing the file, and not the file itself:
set CLASSPATH=%CLASSPATH%;%CD%

Categories