Relative paths in properties file in Eclipse - java

How to configure properties file in Eclipse java?
How can we provide relative addresses in the properties file?
These two lines are working
modelsPath=C:\\Users\\rishika.shrivastava\\workspace\\CSVWEB\\src\\com\\models
csvFilePath=c:/users/rishika.shrivastava/workspace/CSVWEB/
But when i use relative addresses like this:
modelsPath=/CSVWEB\\src\\com\\models
csvFilePath=/CSVWEB/
it doesn't work.

If your files are to be resource on the classpath, then you should read them as resources, not as files on the file system, (which is what happens when you use File or a FileXxx variant).
To read resources from the classpath, you could do
getClass().getResource("/com/models/file")
Or if you need an InputStream you can do
getClass().getResourceAsStream("/com/models/file")
Some Resources
Class API to see what those methods do
How to really read text file from classpath in Java

Related

Java FileNotFoundException when trying to read txt file from resources folder

I'm trying to read a text file located in src/main/resources/test/file.txt. I'm trying to get the path of the file using String path = getClass().getResource("/text/file.txt").getFile(); but when I try to read it I get a FileNotFoundException. I tried putting many different paths, all of which failed. How can I go about doing this?
The idea of putting something into the src/main/resources tree is that it will be copied into the JAR file that you build from your project. It will then be available to your application via the Class methods getResource(String) and getResourceAsStream(String) methods.
When you are running in your application in the development environment, it is certainly possible to use FileInputStream etcetera to access the resource. But this won't work in production. In production, the resources will then be inside your app's JAR file. FileInputStream cannot open a JAR file and its contents by name.
When you do this:
getClass().getResource("/text/file.txt");
you get a URL for the resource, which will look something like this:
jar:file:/path/to/your.jar!/text/file.txt"
It is not possible to turn that into a pathname the FileInputStream will understand. Whatever you try will give you a FileNotFoundException ... or something that is not the resource you want to read.
So what to do?
You have a few options, depending on your application's requirements.
You can use getResourceAsStream and use the resulting input stream directly.
You can copy the contents of getResourceAsStream to a temporary file, and then use the pathname of the temporary file.
You can create an application specific directory (e.g. in the user's home directory) and extract the file you need from the JAR into the directory. You might do this the first time the application runs.
You could open the JAR file as a JarFile and use that API to open an InputStream for the resource. But this assumes that that the resources are in a JAR ... and on some platforms (e.g. Windows) you may encounter problems with file locking. (And it would be a bad idea to attempt to update the resource in the JAR.)
Try giving complete path of the file from the disk.
C:\Users\MyUser\Desktop\file name with extension

Relative path in 'file' protocol for URL

I have an implementation which reads a file and uses java.net.URL to take a path to open connection to.
In production code, the link of file will be on FTP but for testing the logic I want to use a file locally which I can read using file protocol instead of ftp.
I am using Spring and changed the file path to something like below-
application-test.properties
#file location
enzyme.dat.ftp.link=file:///Users/username/project/src/test-integration/resources/input.dat
As you can see its a absolute path, which I need to make relative to my project. How can I do it?
I just need something like below-
#file location
enzyme.dat.ftp.link=file://${project.basedir}/src/test-integration/resources/input.dat
new URL("file:relative/path/to/file.txt")
The path is calculated starting from the current working dir of the application.
This answers the exact question, but actually, the recommendation to read the resource from classpath is better unless the file is really external to the project.
If the file you want to read is on the classpath then you could use something like the following:
new ClassPathResource("input.dat").getURL();
when trying to read the properties file, that way you can simply reference the file name ("input.dat") in your test properties. If the file is not on the classpath then you'll have to use a different mechanism.

How to call filepath for files listed in a .properties file

I have a .properties file that is under a source folder I made called res (using Eclipse Mars 2). I have two other folders in there called messages and schemas.
I need help in giving a filepath so it works locally and on a server (e.g. JBoss) after making the project into a .war file. This is my .properties file:
# Credentials
user=flow
password=flow
# Path to schema for validation
schemaPath=schemas/Schema1.xsd
# Path to where you want to keep incoming and outgoing messages
messagePath=messages/
The above properties file will only work if I provide the full path to the two different *Path properties (above is not full path). However, I can't do that because it needs to work on the application server and on different operating systems.
In my code, I save the filepaths to Strings and use those Strings to specify where to write or read. How can I make it so it works after deploying to the server using a .war file?
I am using a Dynamic Web Project in Eclipse Mars 2.
EDIT: Since the properties is user configurable, they might give a full path. It should work whether the path is short as shown or the full path.
You have to make sure that the properties file is part of the classpath, that is usually including it in classes/ directory.
Mark the folder with properties file as Source Folder in your eclipse. If it is in a package, then use that package name in your path while loading the file.
For example, if the file is in config/data.properties, then load the file by .getResource("config/data.properties");

How to access the content of the jar during runtime?

How can I access the content of the jar file which has been started. I want to create a big jar file which contains everything I need and then during runtime I want to copy some files of my jar into an external folder. Is this possible?
You want this.getClass().getClassLoader().getResourceAsStream. Example:
InputStream config =
this.getClass().getClassLoader().getResourceAsStream("config.txt");
The files in the JAR are not accessible as files, so you must use getResourceAsStream to read them. See access files and folders in executable jars how to access the files within the jar.
Following, you use the inputstream to write the files onto the file system.
See:
Easy way to write contents of a Java InputStream to an OutputStream
http://www.mkyong.com/java/how-to-convert-inputstream-to-file-in-java/
You can access any file via the ClassPath using the classloader. Start with Class.getResourceAsStream

Load java.util.logging.config.file for default initialization

I'm trying to load a custom log.properties file when my application is started.
My properties file is in the same package as my main class, so I assumed that the -Djava.util.logging.config.file=log.properties command line parameter should get the properties file loaded.
But the properties are only loaded when i specify a full absolute path to the properties file. Any suggestions how to use a relative path?
You can dynamically load java.util.logging properties files from a relative path very easily. This is what I put inside a static {} block in my Main class. Put your logging.properties file in the default package and you can access it very easily with the following code.
final InputStream inputStream = Main.class.getResourceAsStream("/logging.properties");
try
{
LogManager.getLogManager().readConfiguration(inputStream);
}
catch (final IOException e)
{
Logger.getAnonymousLogger().severe("Could not load default logging.properties file");
Logger.getAnonymousLogger().severe(e.getMessage());
}
Java logging doesn't search your whole hard disk for a file; there are very simple rules how files are looked up. You want Java to see that the two files belong to each other but you didn't say so anywhere. Since Java sees no connection between the properties file and your class other than that they are in the same folder on your disk, it can't find the file.
-Djava.util.logging.config.file=log.properties only works if the file log.properties is in the current directory of the Java process (which can be pretty random). So you should use an absolute path here.
An alternate solution would be to move the file logging.properties into $JAVA_HOME/lib/ (or edit the file which should be there). In that case, you don't need to set a System property.
util logging does not load from classpath, it needs an absolute path which is why other logging packages like log4j are far easier to configure and better for web apps where it's a pain to get abs paths.
this is not explained at all in the java.util.logging.LogManager doco.

Categories