This question already has answers here:
Use properties file in Spring [closed]
(2 answers)
Closed 7 years ago.
I have a webapplication on Java spring. I need to read application specific settings when the application will be initialized. I have added app.properties under WebContent/WEB-INF but I am not able to get that file from the class.
If I provide
InputStream input = servletContext.getResourceAsStream("WEB-INF/spring.properties");
prop.load(input);
then it is showing file is not present. I can not use absolute path. What will be the path?
From the Javadoc ServletContext.getResource:
The path must begin with a / and is interpreted as relative to the
current context root, or relative to the /META-INF/resources directory
of a JAR file inside the web application's /WEB-INF/lib directory.
Therefore try
InputStream in = servletContext.getResourceAsStream("/WEB-INF/<filename>");
Related
This question already has answers here:
How to read text file from classpath in Java?
(17 answers)
Closed 3 years ago.
I want to read the configuration file outside the jar, how should I do?
I tried classloader.getResourceAsStream("config.xml") and succeeded while I am running the code inside Intellij. Now I want to build the jars to a folder and place the config.xml under the same folder, not inside the jar, but the program fails to detect the config.xml.
Is there a graceful way of reading the config.xml instead of using File with relative path in the code, which doesn't work while debugging/running inside the IDE?
Yes, turn it into a system property, and provide it to anything running any Java process/application.
Let's say you have a config.xml file located inside /some/path/down/the/line/, then you can do: java --classpath ... -Dapp.config=/some/path/down/the/line/config.xml tld.domain.Application.
Then all you have to do in your Java code is to reference that name/path: final String configFile = System.getProperty("app.config");, and use any well-known routine to read it from there.
Basically, you have to make sure the file/path/location is provided somehow to the Java classpath.
This question already has answers here:
How do I load a file from resource folder?
(21 answers)
Closed 6 years ago.
I have following project structure:
src
--Main.java
--resources
----Users.txt
And I am trying to create a file from Main.java like that:
File file = new File("resources/Users.txt");
However, I never succeed at doing so.
Why?
If it is a regular file outside a .jar, you are using a relative path. That means, the path to the file is formed from the path where you are calling the file from + the relative path. To make it work, you should invoke java within src folder
This question already has answers here:
getResourceAsStream() vs FileInputStream
(6 answers)
Closed 4 years ago.
I had created a java application which creates a jasper report whenever user clicks the print button it provides a report.
code:
String
srcfile1="C:\\Users\\VINO\\Documents\\NetBeansProjects\\rework\\src\\rework\\report1.jasper";
JasperPrint firstsecondlinked = JasperFillManager.fillReport(srcfile1,map,cons);
I copied the same to the colleague but it doesn't works fine giving an error states that file not found.
As to create a application how to name the source file in the string?
You can do the following.
Specify the paths in some properties file and read it by loading that file. While deploying your application to some other machine, change the path before deployment.
application-resources.properties
report1.path=''
If it's a web-application, you can access any resource inside your application by using request.getContextPath(). You will not need to hard code it like C:\Users\VINO.. Only the relative path will suffice.
This question already has an answer here:
How can I read file from classes directory in my WAR?
(1 answer)
Closed 10 years ago.
i am try to read the file using ServletContext `
InputStream is = servletContext.getResourceAsStream(path)
the value of path is :
path = D:\Assignments\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\HelpGuide\GeneratedReports\userDetail.pdf`
the userDetail.pdf file is exist in given path , but when i try to get the resource as stream using servlet context define as above , the value of is is null .
This is because the getResourceAsStream looks to a path relative to the context root.
Checkout javadoc of servlet context and especially the "getResource" part: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)
You should use a relative path and put your pdf in your classpath, that would be a better practice as your app will not rely on an absolute path.
This is such a bad idea, as the servlet is telling you.
Put that PDF in the CLASSPATH (e.g. WEB-INF/classes of your WAR) and read it as an InputStream from the servlet context.
Absolute paths are the wrong way to go for web apps.
This question already has answers here:
How to find the working folder of a servlet based application in order to load resources
(3 answers)
Closed 7 years ago.
I have a jsp file in the root of .war file.
and then I have a folder named STUFF.
How do I get access to the file read.txt inside STUFF?
/Name_of_war/STUFF/read.txt is the correct path?
The webapp-relative path is /STUFF/read.txt.
You could use ServletContext#getRealPath() to convert a relative web path to an absolute local disk file system path. This way you can use it further in the usual java.io stuff which actually knows nothing about the web context it is running in. E.g.
String relativeWebPath = "/STUFF/read.txt";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath);
// Do your thing with File.
This however doesn't work if the server is configured to expand the WAR in memory instead of on disk. Using getRealPath() has always this caveat and is not recommended in real world applications. If all you ultimately need is just getting an InputStream of that file, for which you would likely have used FileInputStream, you'd better use ServletContext#getResourceAsStream() to get it directly as InputStream:
String relativeWebPath = "/STUFF/read.txt";
InputStream input = getServletContext().getResourceAsStream(relativeWebPath);
// Do your thing with InputStream.
If it is located in the classpath, or you can add the folder to the classpath,
How about:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream(fileName);