Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im trying to read a file, but I was getting the runtime error that I wrote in the title, despite the fact that the file is in the same directory as the .class,

The java program is part of a package called P3, so I have the folder practica3, and then inside of it, I have another folder called P3, where I have a copy of the .class, and the file that Im trying to read is in both folders, and yet, Im getting the same error

What I have tried:

I have tried using absolutePath and I was getting
\practicas\practica3\.
from the terminal
Posted
Updated 11-Mar-22 8:28am

1 solution

Unless specified by an absolute reference, paths are relative to the folder the executable was located in, not the source - and modern compilers generally don't put EXE files in the same folder as source code.

So when you specify a path to a file, unless you have copied it to the EXE file folder, the system won't find it.

Even if it could, it's a bad idea to assume that any file in the EXE folder is accessible - in some system (like Windows) production EXE files are located in a write protected folder for security reasons (under "Program Files", all folders re write protected) so even if your app worked in development, it could easily fail in production.

Depending on your system, there are much better places to store data files - google for "Java", "data files" and "where should I store", plus your target OS and you should get info on better places to keep it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900