acces a file placed in src folder on mac [duplicate] - java

This question already has answers here:
How does Java resolve a relative path in new File()?
(7 answers)
Closed 7 years ago.
I try to acces a file that i've placed on the src file using a relative path but in vain.
here is an example :
File file=new File("src/teledeclaration.xsd");
but i get the following error :
java.io.FileNotFoundException: /Users/macbookpro/Downloads/eclipse2
/Eclipse.app/Contents/MacOS/src/teledeclaration.xsd (No such file or directory)
why the below path appears before the normal path that i've written "src/teledeclation.xsd"
/Users/macbookpro/Downloads/eclipse2/Eclipse.app/Contents/MacOS/
how should i handle this problem ?

If you don't specify a full path, the path will be relative to your current working directory. If you check System.getProperty("user.dir") it should point to the Eclipse.app/... folder.

Related

Ececutable jar file can't find my txt file [duplicate]

This question already has answers here:
Reading a resource file from within jar
(15 answers)
Closed 2 years ago.
I made a javafx project, when I finished I made a executable jar file. When I run it I get this error:
java.io.FileNotFoundException: src\Radiatorlista.txt
This is how my code looks like:
File radiatorok = new File("src/Radiatorlista.txt");
I know that this path will not work. I don't know how to specify the correct relative path.
This is how my jar file looks like.
The answer is that you need to put your txt files (or whatever you wan't to open) in the folder where your jar file is. For example my jar file is here:
F:/Example/for/my/file
You need to put your files in the file folder.
Then change your code like this:
File file = new File("Mytxt.txt");
After this your program should work.

How to run .exe file within program [duplicate]

This question already has answers here:
Run exe which is packaged inside jar file
(6 answers)
Closed 5 years ago.
In eclipse, if you have a .exe file in the package how would you run it? I know if it is outside you would just add in the path, but what would the path be for something inside the project?
Lets imagine a File f = new File(Path);
If in that case we have the file inside our project (same directory),then we dont need to add the path, just the file name and extension (ex: .txt)......I guess in your case it would be something similar.

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

How to write relative path [duplicate]

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

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

Categories

Resources