How to write relative path [duplicate] - java

This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 7 years ago.
I'm having some issues writing a relative path.
I have a servlet trying to save an image under the web directory.
This is the servlet location:
onlineShop/src/main/java/control/servlets/itemManagement
And this is the directory where I want to save the image:
onlineShop/web/UploadedPhotos
What is the relative path to indicate (to the servlet) the directory in which to save the image?

try this one
getClass().getResource("").getPath() + "fileName";

Related

How to get directory of a .java file [duplicate]

This question already has answers here:
How to get the current working directory in Java?
(26 answers)
Closed 3 years ago.
Part of my (java) code needs to access a database. When opening a connection it checks if the actual database file exists (I use sqlite). I want to make my code portable, so I want to avoid hard coding the path of the database file. Is there anyway in java to get the path of the .java file? Because I know exactly where the database is from the .java file accessing it.
I've tried using current directory with File but it doesn't give me the path of the actual .java file. When I use android studio the current directory is different than when I simply use a terminal.
The best way to get the path information would be using Paths and Path class from the java.nio. You can input an absolute path or relative path to the Paths.get(String str) to get the Path.
To get the project directory, you can use:
Paths.get(System.getProperty(“user.dir”))
Paths.get(“”)
It will get the complete absolute path from where your application was initialized.
The Paths.get() method will return a Path Object, on which you can call toString() or toUri() to get the path. Hope this helps.
Javadocs - Paths

File not found in Java [duplicate]

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

Reading properties file under WEB-INF [duplicate]

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>");

How to configure jasper reports in java code [duplicate]

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.

How can I get the file directory with name in java [duplicate]

This question already has answers here:
Jar file name form java code
(2 answers)
Closed 9 years ago.
I need to get the application's directory with name. For example, if my java application is working at C:\Program Files\example1.exe I need to get it as "C:\Program Files\example1.exe". Or is it working at C:\Windows\example2.exe
I need to get it as "C:\Windows\example2.exe" . How can I do it?
You can use System.getProperty("user.dir") to find where is your working directory.

Categories

Resources