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

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.

Related

FileNotFoundException in the same directory [duplicate]

This question already has answers here:
Java, reading a file from current directory?
(8 answers)
FileNotFoundException even when the file is there
(3 answers)
Closed 9 days ago.
I'm not entirely sure if this is a Java issue or an IntelliJ issue, but I had a quick question about my File not being found via a path. My Main class is in the same directory as my input.txt file.
I though I should be able to do File file = new File("./input.txt"), except I get FileNotFoundException. When I do something like File file = new File("src/input.txt"), it does work.
I get that this may be a solution, but if I try to run this code outside of IntelliJ without a src directory, this will lead to an error where the File can't be found.
Is there any reason why I can't just do ./input.txt to specify that the File is in the same directory as the Main file?
That's because in IDEA, the working directory of your program is the root directory of your project, not src by default. Therefore, you can access the file by ./src/input.txt or src/input.txt, not input.txt. By the way, it's bad practice to put your resources in src, because it gets mixed with your source code, and it won't be included in the JAR file, which means you cannot access it when your project is packaged as a JAR file. If you are using maven, you can put input.txt inside the resources folder(src/main/resources), and access it by Main.class.getResource("/input.txt")(gives you a URL) or Main.class.getResourceAsStream("/input.txt")(gives you a InputStream) depending on your needs. So that the resources will always be available, no matter you are running the program in IDEA or from a JAR.
FileNotFoundException occurs when the file doesn't exist at the specified location.
File file = new File("./input.txt"), the current working directory (.) is being used as the file location.
If you are running the code from an IDE, the current working directory is usually the project root directory, not the directory where the code file is located.
So, if the input.txt file is located in the same directory as your code file, then you should use the relative path to it.
File file = new File("src/input.txt"), you are specifying the absolute path to the input.txt file, which is inside the src directory.
This is why it works, as the file is guaranteed to exist at that location.
If the file is located in the same directory as your code file, you can use a relative path, and if it's in a different directory, you should use an absolute path to it.

How to read from resource file outside jar [duplicate]

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.

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

Can't open file under custom folder on my Eclipse project [duplicate]

This question already has answers here:
Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)
(2 answers)
Closed 8 years ago.
I'm developing a custom plugin for Eclipse and I have a powerpoint file named "extract.ppt" that I want to open through my source code.
When I place the file on my projects root diercotry and use:
File file = new File("/extract.ppt");
the file opens just fine.
But since im gonna be using a few more files on my application, I thought it would be a good idea to keep them all organised under a folder. So I created a folder named "files" under my main project folder and tried to use:
File file = new File("/files/extract.ppt");
but I get an error saying the file does not exist. I checked my Eclipse project's folder and the folder "files" as long as the "extract.ppt" are there.
Any ideas?
Use this instead:
File file = new File(".\\files\\extract.ppt");
The ".\\files\\..." is like saying LOOK on current directory IN folder files IN ...
You should use relative path:
File file = new File("./files/extract.ppt");
the files folder should be under the project.
Just like .setting or bin folder

Categories

Resources