Properties file in Eclipse - java

I have an application that uses a properties file that was added by hand at the /project/bin folder (Eclipse project). The application locates the file using:
this.getClass().getClassLoader().getResourceAsStream("filename.properties")
Now I want to add this file in Eclipse, so it's actually part of the project. In which directory should I create the file and how can I make sure the application will find it?
Thanks.

You can put the .properties file anywhere under a project source directory (src by default) to make it wind up in the build directory (bin by default) as a "resource", when the project is built. Since the bin directory is generated, you can't modify its contents by hand, in the way you describe.
There's no need to call getClassLoader(); just getClass().getResourceAsStream("foo") is fine.

Related

How to give relative path in a executable jar?

I have a Spring app that I am deploying as a .jar.
The app has to write to a folder located in /src (precisely /src/main/resources/patches). I have this path directly in the code.
In application.properties: PATCH_DIR = src/main/resources/patches
The app has to also read a json file from src/main/resources/myJson.json, the path also being directly written in the code.
Prior to deploying, while running from the IDE everything goes well, the app sees the file and the folder and reads and writes correctly.
After building the .jar the paths change, the file is located in myJar.jar/BOOT-if/classes/myJson.json and the folder is respectively in myJar.jar/BOOT-if/classes/patches.
How can I specify these paths in the code in a way that even after building the jar they stay relevant to my application?
Edit: I can specify the path of the file as: PatchApplication.class.getClassLoader().getResource("myJson.json").getPath();
This should solve the problem, as the path is relative to the class and not to the root of the project, but it does not improve anything.
You should specify a path in your file system,instead of the path inside a .jar.When you run your app ,it would access the given path.
generally all the classes goes to classes folder in jar.
all the classes ending after src/main/java goes to /classes/ folder
and similarly all the resources file
Eg. your source folder have src/main/java/com/mypack/ABC.java than you will find this in jar as /classes/com/mypack/ABC.class .
Try using /classes/patches and /classes/myJson.json.
This should work

hibernate.cfg.xml is not exported to executable jar

When I'am trying to export my project to a executable .jar-file then configuration files like hibernate.cfg.xml or log4j-properties are not exported into the jar. I have to add them manually to the archieve. The files are located in the /target/ folder in the root folder of the project.
How do I get Eclipse to export the config files too?
Just create a resources folder parallel to your source/src folder, and keep your configuration files in it.
If you use the Eclipse approach:
Project >> Export >> Java >>
Runnable JAR File >> Package required libraries into generated JAR
Only the class files get exported in the jar and - if this option is selected - the linked libraries.
You can, however create an additional source folder (name it resources or config or whatever fits your case) and copy your files into this one. This directory will then also be part of your generated jar file.
The problem of non-class files not showing up in the exported jar file may be due to Eclipse hiding the output folders. That is what happened to me. The problem was that the bin folder for a project my project depended on was hidden by Eclipse. Fixed that by using the Project Explorer to get Eclipse to make the output folder visible.
See what can I do to make display the bin folder on eclipse?
Using Eclipse Oxygen, fixed it by:
-- Selected the project with the hidden output folder in the Project Explorer
-- Clicked on the triangle in the Project Explorer
-- Clicked on Filters and Customization
-- Make sure the Filters Tab is selected.
-- Uncheck Java Output Folders

Where do I put the txt file that I want to read in Java?

If I am building an application in Eclipse using Java, and I want this application to read from a txt file, where should I put the txt file in the package in order to not have to specify its path (so that I can just say "file.txt"). I thought that it was supposed to go in the src folder, but it's not working.
Right click the project folder and click New -> file. The file will be in the Project folder and not in the source folder.
Put the file in the folder from where you run your Java application (your current/working folder). If you're using the default settings of Eclipse to run your application, you should put the file directly inside the Eclipse project folder. To create the file in the Eclipse project, you can simply drag-and-drop it there or right-click on the Eclipse project and create a new file.
The way this can be done is using .getResourceAsStream("file.txt")
SO thread
Downvoted for a correct answer? Wierd...
If you don't want to specify a path, and want to open a File from the file system using the java.io.File API, then put it in the working directory.
In Eclipse, the working directory defaults to the root level of your project, but you can modify it (and also review what it is set to) in an Eclipse Run Configuration. They are accessible under the "Run > Run Configurations..." menu option, and the working directory setting is under the "Arguments" tab for Java programs.
The important thing is for the directory containing your file to be on the classpath. When you're using Eclipse's run dialog for the settings of your project, there's a tab for classpath. You can use it learn what's already on the classpath, and to make additions if you want to.
As mentioned above by #tschaible, the Eclipse working directory defaults to the root of the project. Therefore, the program will look for the file from the root level.
However, if you intend to run the program from the command line, it acts differently because the working directory defaults to the folder that you are running the java file from (aka the src folder). Therefore, it is important to keep this discrepancy in mind when testing your java program from the command line.

How to build a java project with all the required files and folders?

I created a simple application which will read all the files and folders kepts inside a folder. Whenever I build the project, I only get the jar file but the folder where the file and folders are kept have to be created. Is this supposed to be like this or I have to code something, which will create the folder upon the final build.
Compilation gives you only executable file (i.e. *.jar). If you need something else to make you application works - you must do it (programmatically or not).

Access .properties file at same level as working directory

I am working on a Java project that I want to deliver to my client as a .jar file. However, I want to allow the client to be able to change the parameters of the program without having to recompile or recreate the .jar. Basically, I want to be able to load .properties files from classes inside the .jar but locate those .properties files outside of the .jar and even outside the working directory.
I have been testing my attempts inside Eclipse, which might be causing some of the problem but I don't see how at the moment. My setup is a follows. I have one project that contains a few classes that I build a .jar file from. I have a .properties file that is used to create a ResourceBundle whenever a class for the .jar is created. I specify that an additional directory, "conf/", be included in the .jar classpath within the .jar manifest.
Once the .jar file is built, it is copied to the lib/ directory of another project which I am using for testing. This test project includes the .jar file as a library ("Add External Jars..") in the Java Build Path. The .properties file is located the conf/ directory which is at the same level as lib/, src/, and bin/ but I am unable to accces it there. The only way I have been able to get it to work is to locate conf/ under src/ (and bin/) but I would like to be able to use it up one level. Is this possible?
Here's the entry in the .jar manifest file...
Class-Path: ../conf/
Here's the ResourceBundle call that I tried (didn't work)...
rb = ResourceBundle.getBundle("..conf.BaseProject");
Here's the directory structure that works now (names have been changed to protect the innocent)...
/Project
/Project/bin
/Project/bin/conf
/Project/bin/conf/BaseProject.properties
/Project/bin/TestClass.class
/Project/lib
/Project/lib/BaseProject.jar
Here's the directory layout I want (again, file names not important)...
/Project
/Project/bin
/Project/bin/TestClass.class
/Project/conf
/Project/conf/BaseProject.properties
/Project/lib
/Project/lib/BaseProject.jar
You can get it using the Classloader.
I like Spring's ClassPathResource

Categories

Resources