Automatically search for files without knowing the filepath of the program?

Hello guys, I ran into a very annoying issue in Java and could not find a good solution for the error... Please help me. https://pastebin.com/md4m0SFm The provided code is simple and there is nothing to explain about it, but I noticed, that Java does not search for the interface.txt in the local program or local project folder... Which is horrendous to me. I also noticed, that if I create a file on my Desktop called interface.txt and run the program on my DESKTOP, it suprisingly works.... What I basically wanna do is include the interface.txt to my project to display a Logo I made for my program. But no matter what I do, I always come to the conclusion that if I sent the program to a client, it would not work as their filepath or program path would be completely different if that makes sense... HELP!
Pastebin
asdf - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
6 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @netty.bootstrap! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st9mo ago
WHen accessing relative files, Java starts from the directory you execute it in so if you execute java in your desktop folder, it will look for a directory ui with an interface.txt inside the desktop folder But instead of files, you could use resources and include these in the project (in a resource/source folder)
try(BufferedReader bufferedReader = new BufferedReader(UInterface.class.getClassLoader().getResource("ui/interface.txt"))){
//...
}
try(BufferedReader bufferedReader = new BufferedReader(UInterface.class.getClassLoader().getResource("ui/interface.txt"))){
//...
}
dan1st
dan1st9mo ago
Stack Overflow
How do I load a file from resource folder?
My project has the following structure: /src/main/java/ /src/main/resources/ /src/test/java/ /src/test/resources/ I have a file in /src/test/resources/test.csv and I want to load the file from a ...
Stack Overflow
Reading a resource file from within jar
I would like to read a resource from within my jar like so: File file; file = new File(getClass().getResource("/file.txt").toURI()); BufferedReader reader = new BufferedReader(new FileRea...
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
netty
nettyOP9mo ago
ohhh I guess that helps Ill try it out later! Thanks
JavaBot
JavaBot9mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server