How to read current project path in .properties file - java

I have abc.jks file under resources folder and i need to read that abc.jks file in test.properties file. How to read abc.jks file in test.properties file.

In application.properties (or how you name it) assuming you use maven and you execute your spring application in the default project you can refer to the file like:
my.file.name = ./src/main/resources/abs.jks
or if this is for test purpose only you can put in the test resources and refer like this:
my.file.name = ./src/test/resources/abs.jks
For me the best is to not be dependent on folder and always use classloader resource, but if you have to refer a path on the filesystem, from java code you can do this:
URL url = getClass().getClassLoader().getResource("abs.jks");
File file = Paths.get(url.toURI()).toFile().getAbsolutePath();

Related

How can I get the Url of file that is outside the jar?

I have deployed spring boot app in exploded mode using maven-assembly-plugin. I have made config folder from project’s resource folder.
The problem is that I am not able to get the url to access the file that is in the config folder.
How can I get the url of the file that I have uploaded and stored at this config folder.
File structure
Target folder:
+springdemo-0.0.1-application.zip
+config
+myfolder
-oldfile.png
+springdemo-0.0.1.jar //running this jar file
+start.sh
-springdemo-0.0.1.jar
I want URL of files stored in myfolder. This url will be accessed by 3rd party(for ex. Picasso) to get file data. I am unable to get correct url pointing to this myfolder files.
Its easy, you need to provide the full path of the external application.properties file than what is present in your source code during the startup of the spring-boot.jar application at runtime.
Example: java -jar -Dspring.config.location=file://<>
This external file takes precedence over the one in our jar file.
Note: Not a SpringBoot-specific answer, just the simple Java way.
You can only try to guess the location of the file because there's no way to know the working directory when a Java application is run.
For example, you could try a very simple approach if you control how the Java application is executed:
var configFile = new File("config/application.properties");
This will works as long as the process is started from the same directory where start.sh is (which seems like what you intend).
If it doesn't work, try printing the working directory like this first:
System.out.println("WRK DIR = " + new File(".").getAbsolutePath());
This will tell you what the relative path to your file should be.
So, if this prints <root-dir>/springdemo and you know your config file is under <root-dir>/springdemo/mydir/config/, then the file path should be:
new File("mydir/config/application.properties");
By the way, you can easily read the file with:
// read file into List of lines
Files.readAllLines(configFile);
// specifically, for properties file
var props = new Properties();
props.load(new FileInputStream(configFile));
If for whatever reason you need an URL object instead of a File, it's easy, just call:
URL url = configFile.toURI().toURL()
If you want ALL files under the config dir, you can list them first with:
File[] files = configFile.getParentFile().listFiles();
if (files != null) {
for (File file : files) {
// use file?
}
}

File relative to jar using Spring

I work on a Java console application. There is a property in my application.properties file, which contains another file name as a value of a property, like
my.file.location=file:myDir/myFileName
In the code I try to get the file like this:
#Value("${my.file.location}")
private File myfileLocation;
If I start the application from the directory, which contains jar file, the file is resolved, but when I run my application from a different location, the file location is not valid.
I can't have this file on classpath, it must be external to the jar file.
How can I make the file path to be relative to my jar file and not to the current working directory?
I believe this has nothing to do with Spring right? You just want to load configuration file, that is inside your application, unpacked, so the user can modify it, ok?
First, you may try to always setup the working directory, which I believe is more "standard" solution. In windows you can make a link, that specifies the Start in section and contains the path to your jar file (or bat or cmd, whatever).
If you insist on using the jar path, you could use How to get the path of a running JAR file solution. Note, that the jar must be loaded from filesystem:
URI path = MySpringBean.class.getProtectionDomain().getCodeSource().getLocation().toURI();
File myfileLocation = new File(new File(path).getParent(), "/myDir/jdbc.properties");

How do I get the relative path of files in my project?

I am creating a project using jsp/servlet in which I am trying to create java file and class file inside the project itself. But I am able to do this for only my system because the path I give their is like : C:\Users\MySystem\Desktop\Test\.. which works only for my system. What should I do so that if I have to run this project in another system I don't have to change path again and again.
Well if it is maven project just put your resources files under src/main/resources
and you can read them using this lines.
String path = Thread.currentThread().getContextClassLoader()
.getResource("yourFileName").getPath();
System.out.println(path);
Or even this way you can do it.
String pathOfTheFile = getServletContext().getResource("yourFile").getPath();
and don't forget to put the file under web-content or webapp folder

How to read a property file present in WEB-INF from a Jar file

I have a jar file(an api), from which i need to load a property file which exists in WEB-INF folder.
How do i load this property file from my jar.
Would appreciate any help.
You can easily do it if your properties file is in WEB-INF/classes. In this case just write something like
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/myprops.properties"));
Obviously the file may be located in sub folder of classes. In this case you have to specify the full path when calling getResourceAsStream().

Java JAR: Writing to a file

Currently, in my eclipse project, I have a file that I write to. However, I have exported my project to a JAR file and writing to that directory no longer works. I know I need to treat this file as a classpath resource, but how do I do this with a BufferedWriter?
You shouldn't have to treat it as a classpath resource to write to a file. You would only have to do that if the file was in your JAR file, but you don't want to write to a file contained within your JAR file do you?
You should still be able to create and write to a file but it will probably be relative to the working directory - the directory you execute your JAR file from (unless you use an absolute path). In eclipse, configure the working directory from within the run configuration dialog.
You're probably working in Linux. Because, in Linux, when you start your application from a JAR, the working directory is set to your home folder (/home/yourname/). When you start it from Eclipse, the working directory is set to the project folder.
To make sure you really know the files you are using are located in the project folder, or the folder where your JAR is in, you can use this piece of code to know where the JAR is located, then use the File(File parent, String name) constructor to create your files:
// Find out where the JAR is:
String path = YourClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
path = path.substring(0, path.lastIndexOf('/')+1);
// Create the project-folder-file:
File root = new File(path);
And, from now on, you can create all your File's like this:
File myFile = new File(root, "config.xml");
Of course, root has to be in your scope.
Such resources (when altered) are best stored in a sub-directory of user.home. It is a reproducible path that the user should have write access to. You might use the package name of the main class as a basis for the sub-directory. E.G.
our.com.Main -> ${user.home}/our/com/

Categories