How to get the path of .key file from spring application programmatically? - java

I need to get the path of a key file that i have placed in the root folder of my spring application. Everything works as expected when i run it locally. But when i deploy the application to the server i get a FileNotFoundException.
File file = new File("testfile.key");
String path = file.getAbsolutePath();
I have tried placing the file in the resource folder as well.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File file = new File(classLoader.getResource("testfile.key").getFile());
Just need to pass the file path to another method (3rd party library) which will read the content.
Any help would be much appreciated.

This should be a resource, placed in the resource folder (if using maven).
You can access it using
this.getClass().getResource("testfile.key");
The root for accessing files can change between environments but the root for resources is a directory that's specified during compilation. For maven driven projects this is:
src/main/resources

Related

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

Creating resources in a maven/java project

How do I create a resource so that it is located in the resource folder of my project?
In the following, "test.txt" is a file I want to create, but the variable:url is null, so I can't get a path to the file I want to create.
URL url= HashArray.class.getResource("test.txt");
File file = new File(url.toURI());
The resource probably needs to be located in the resources folder because I need to bundle it with the code in the packaging phase.
This can't work, as resources is not a runtime location. It's a source location. If you try to put a file, and run your program the url wouldn't be null anymore. But it would point to your target/classes folder (ok there are quite a number of possiblities, depending how exactly you start it) However, in most cases it wouldn't be resources anymore...
There is not relation between the resource folder in maven and a resource in Java.
class.getResource finds a resource in the classpath, you are fine putting your txt file in the resources folder but in order to make it accessible at runtime you have to package it.
How to package resources

How can i find the path of my file in my Java project file?

I put a file inside my Java project file and i want to read it but how can i find the path name with Java.
Here i put it in C driver but i just want to find path by just writing the name of file. Is there a function for it?
FileInputStream fstream1 = new FileInputStream("C:/en-GB.dic");
If the file is inside the jar file generated for your project (or in the classpath used by your project, generally), under the package com.foo.bar, you can load it using
SomeClassOfYourProject.class.getResourceAsStream("/com/foo/bar/en-GB.dic");
If it's not in the classpath, and you launch the application (using java.exe) from the directory c:\baz, and the file is under c:\baz\boom\, the you can load it using
new FileInputStream("boom/en-GB.dic");
Place it in your classpath, If it is a web application WEB-INF/classes/yourfile.ext, if it is a standalone application place it in bin directory of your application (default class directory is bin).
Then you could read by using one of the following ways.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourfile.ext");
Or
InputStream in = this.getClass().getResourceAsStream("/yourfile.ext");
You can read online for the differences between above two approaches.

Placement of data files for java web service

I am writing a simple web service on java, that should read an xml file and generate a string for result. I am, however, at loss as to:
a) where should xml file be placed in the file system for it to be accessible to web service?
b) what would the relative way to the file from the service
c) alternatelyt, how can i add xml file to the project, so that it would be deployed along with web service
I am using NetBeans as IDE and maven as a build system.
Usually I provide a resource-bundle (.jar) in my web application. If the file should not be modified during runtime, this should be the best way to access it. This will be c) in your question. There you can access the file with:
ClassLoader cl = this.getClass().getClassLoader();
InputStream is = cl.getResourceAsStream("resourceName");
"resourceName" will be the path inside the jar file. I you are packaging with Maven, everything in src/main/webapp/WEB-INF/classes will be available in your classpath and can be loaded with getResourceAsStream.
An alternative way with maven is to create a new module and place all required resources in src/main/resources and then add a dependency to this artifact in your webapp pom.
a) Anywhere, as long as your process has permissions to access the file.
b) The relative path depend on the App server you are using. The relative path is relative to the home of the jvm. for tomcat is is tomacat-base, for weblogic is the domain directory. I would recommend to do not depend on this, but have configuration param that contains the absolute path.
c) put it in WEB-INF (servletcontext path) or WEB-INF/classes (you can access it as classpath resource)

Categories